where.py

Go to the documentation of this file.
00001 ##
00002 # 
00003 # Given one or more regular expressions on the command line, searches
00004 # the PATH for all files that match.
00005 # 
00006 # Copyright (C) 2002 GDS Software
00007 # 
00008 # This program is free software; you can redistribute it and/or
00009 # modify it under the terms of the GNU General Public License as
00010 # published by the Free Software Foundation; either version 2 of
00011 # the License, or (at your option) any later version.
00012 # 
00013 # This program is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 # GNU General Public License for more details.
00017 # 
00018 # You should have received a copy of the GNU General Public
00019 # License along with this program; if not, write to the Free
00020 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00021 # MA  02111-1307  USA
00022 # 
00023 # See http://www.gnu.org/licenses/licenses.html for more details.
00024 # 
00025 
00026 import sys, getopt, re, os, string
00027 
00028 __version__ = "$Id: where.py,v 1.4 2002/08/22 02:25:57 donp Exp $"
00029 
00030 ignore_caseG = 0
00031 matches = {}  # They'll get stored in here by filename so that there are
00032               # no duplicates.
00033  
00034 
00035 ##
00036 # dir is a directory name, regexps is a list of compiled
00037 #     regular expressions.
00038 #     
00039 def CheckDirectory(dir, regexps):
00040     global matches
00041     currdir = os.getcwd()
00042     try:
00043         os.chdir(dir)
00044         tmp = os.listdir(dir)
00045         files = []
00046         for f in tmp:
00047             if os.path.isfile(f):
00048                 files.append(f)
00049         for file in files:
00050             for regexp in regexps:
00051                 if regexp.search(file) != None:
00052                     matches[dir + "/" + file] = ""
00053     except:
00054         sys.stderr.write("Warning:  directory '%s' in PATH not found\n" % dir)
00055         os.chdir(currdir)
00056 
00057 def main():
00058     global ignore_caseG
00059     try:
00060         optlist, regexps = getopt.getopt(sys.argv[1:], "i")
00061     except getopt.error, str:
00062         print str
00063         sys.exit(1)
00064     for opt in optlist:
00065         if opt[0] == "-i":
00066             ignore_caseG = 1
00067     if len(regexps) == 0:
00068         print "Usage:  where [-i] regexp1 [regexp2...]"
00069         print "  regexps are python re style"
00070         sys.exit(1)
00071     # Get a list of the directories in the path
00072     sep = ":"
00073     key = "PATH"
00074     if sys.platform == "win32":
00075         sep = ";"
00076     if key in os.environ.keys():
00077         PATH = os.environ[key]
00078         path = re.split(sep, os.environ[key]) 
00079     else:
00080         print "No PATH variable in environment"
00081         sys.exit(1)
00082     # Make a list of compiled regular expressions
00083     regexp_list = []
00084     for regex in regexps:
00085         if ignore_caseG:
00086             regexp_list.append(re.compile(regex, re.I))
00087         else:
00088             regexp_list.append(re.compile(regex))
00089     # Now check each command line regexp in each directory
00090     for dir in path:
00091         CheckDirectory(dir, regexp_list)
00092     list = []
00093     for key in matches.keys():
00094         list.append(key)
00095     list.sort()
00096     for file in list:
00097         print string.replace(file, "\\", "/")
00098 
00099 main()
00100 
00101 

© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved; If you reference this document or any part of this document you must use the citation verbatim (including the link) "© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved."

Notice: This source code contained in this document is NOT open source and is NOT being distributed as open source.

122,241 lines of code and growing...