From e69debfd6d48ed158699ba814c1c23459e121a20 Mon Sep 17 00:00:00 2001 From: Steven Knight Date: Sun, 4 Apr 2004 10:40:32 +0000 Subject: Add a reject argument to the env.Whereis() method. (sam th) --- doc/man/scons.1 | 9 +++++++-- src/CHANGES.txt | 3 +++ src/engine/SCons/Environment.py | 4 ++-- src/engine/SCons/EnvironmentTests.py | 5 +++++ src/engine/SCons/Util.py | 30 ++++++++++++++++++++++++------ src/engine/SCons/UtilTests.py | 5 +++++ test/WhereIs.py | 5 +++++ 7 files changed, 51 insertions(+), 10 deletions(-) diff --git a/doc/man/scons.1 b/doc/man/scons.1 index 232048b..357b829 100644 --- a/doc/man/scons.1 +++ b/doc/man/scons.1 @@ -3709,9 +3709,9 @@ env.Config(target = 'package-config', source = Value(prefix)) '\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" .TP -.RI WhereIs( program ", [" path ", [" pathext ]]) +.RI WhereIs( program ", [" path ", " pathext ", " reject ]) .TP -.RI env.WhereIs( program ", [" path ", [" pathext ]]) +.RI env.WhereIs( program ", [" path ", " pathext ", " reject ]) Searches for the specified executable .I program, @@ -3734,6 +3734,11 @@ the calling environment's PATHEXT or the user's current PATHEXT (os.environ['PATHEXT']) by default. +Will not select any +path name or names +in the specified +.I reject +list, if any. .SS SConscript Variables In addition to the global functions and methods, diff --git a/src/CHANGES.txt b/src/CHANGES.txt index 8b9c32a..e306bd9 100644 --- a/src/CHANGES.txt +++ b/src/CHANGES.txt @@ -77,6 +77,9 @@ RELEASE 0.96 - XXX - Allow SConf.CheckLib() to search a list of libraries, like the Autoconf AC_SEARCH_LIBS macro. + - Allow the env.WhereIs() method to take a "reject" argument to + let it weed out specific path names. + RELEASE 0.95 - Mon, 08 Mar 2004 06:43:20 -0600 diff --git a/src/engine/SCons/Environment.py b/src/engine/SCons/Environment.py index e75ac38..1b85fe8 100644 --- a/src/engine/SCons/Environment.py +++ b/src/engine/SCons/Environment.py @@ -833,7 +833,7 @@ class Base: tool = self.subst(tool) return SCons.Tool.Tool(tool, map(self.subst, toolpath))(self) - def WhereIs(self, prog, path=None, pathext=None): + def WhereIs(self, prog, path=None, pathext=None, reject=[]): """Find prog in the path. """ if path is None: @@ -850,7 +850,7 @@ class Base: pass elif SCons.Util.is_String(pathext): pathext = self.subst(pathext) - path = SCons.Util.WhereIs(prog, path, pathext) + path = SCons.Util.WhereIs(prog, path, pathext, reject) if path: return path return None diff --git a/src/engine/SCons/EnvironmentTests.py b/src/engine/SCons/EnvironmentTests.py index 53a6905..3f0aab6 100644 --- a/src/engine/SCons/EnvironmentTests.py +++ b/src/engine/SCons/EnvironmentTests.py @@ -1590,6 +1590,11 @@ class EnvironmentTestCase(unittest.TestCase): wi = env.WhereIs('xxx.exe', string.join(pathdirs_1243, os.pathsep)) assert wi == test.workpath(sub4_xxx_exe), wi + wi = env.WhereIs('xxx.exe', reject = sub3_xxx_exe) + assert wi == test.workpath(sub4_xxx_exe), wi + wi = env.WhereIs('xxx.exe', pathdirs_1243, reject = sub3_xxx_exe) + assert wi == test.workpath(sub4_xxx_exe), wi + path = string.join(pathdirs_1243, os.pathsep) env = Environment(ENV = {'PATH' : path}) wi = env.WhereIs('xxx.exe') diff --git a/src/engine/SCons/Util.py b/src/engine/SCons/Util.py index e07675c..d2e1d73 100644 --- a/src/engine/SCons/Util.py +++ b/src/engine/SCons/Util.py @@ -980,7 +980,7 @@ if can_read_reg: if sys.platform == 'win32': - def WhereIs(file, path=None, pathext=None): + def WhereIs(file, path=None, pathext=None, reject=[]): if path is None: path = os.environ['PATH'] if is_String(path): @@ -996,17 +996,23 @@ if sys.platform == 'win32': if string.lower(ext) == string.lower(file[-len(ext):]): pathext = [''] break + if not is_List(reject): + reject = [reject] for dir in path: f = os.path.join(dir, file) for ext in pathext: fext = f + ext if os.path.isfile(fext): - return os.path.normpath(fext) + try: + reject.index(fext) + except ValueError: + return os.path.normpath(fext) + continue return None elif os.name == 'os2': - def WhereIs(file, path=None, pathext=None): + def WhereIs(file, path=None, pathext=None, reject=[]): if path is None: path = os.environ['PATH'] if is_String(path): @@ -1017,21 +1023,29 @@ elif os.name == 'os2': if string.lower(ext) == string.lower(file[-len(ext):]): pathext = [''] break + if not is_List(reject): + reject = [reject] for dir in path: f = os.path.join(dir, file) for ext in pathext: fext = f + ext if os.path.isfile(fext): - return os.path.normpath(fext) + try: + reject.index(fext) + except ValueError: + return os.path.normpath(fext) + continue return None else: - def WhereIs(file, path=None, pathext=None): + def WhereIs(file, path=None, pathext=None, reject=[]): if path is None: path = os.environ['PATH'] if is_String(path): path = string.split(path, os.pathsep) + if not is_List(reject): + reject = [reject] for dir in path: f = os.path.join(dir, file) if os.path.isfile(f): @@ -1040,7 +1054,11 @@ else: except OSError: continue if stat.S_IMODE(st[stat.ST_MODE]) & 0111: - return os.path.normpath(f) + try: + reject.index(f) + except ValueError: + return os.path.normpath(f) + continue return None def PrependPath(oldpath, newpath, sep = os.pathsep): diff --git a/src/engine/SCons/UtilTests.py b/src/engine/SCons/UtilTests.py index 098c2d9..5825406 100644 --- a/src/engine/SCons/UtilTests.py +++ b/src/engine/SCons/UtilTests.py @@ -1050,6 +1050,11 @@ class UtilTestCase(unittest.TestCase): wi = WhereIs('xxx.exe', string.join(pathdirs_1243, os.pathsep)) assert wi == test.workpath(sub4_xxx_exe), wi + wi = WhereIs('xxx.exe',reject = sub3_xxx_exe) + assert wi == test.workpath(sub4_xxx_exe), wi + wi = WhereIs('xxx.exe', pathdirs_1243, reject = sub3_xxx_exe) + assert wi == test.workpath(sub4_xxx_exe), wi + os.environ['PATH'] = string.join(pathdirs_1243, os.pathsep) wi = WhereIs('xxx.exe') assert wi == test.workpath(sub4_xxx_exe), wi diff --git a/test/WhereIs.py b/test/WhereIs.py index 8347acc..15f4442 100644 --- a/test/WhereIs.py +++ b/test/WhereIs.py @@ -73,11 +73,14 @@ print WhereIs('xxx.exe', %s) print env.WhereIs('xxx.exe', %s) print WhereIs('xxx.exe', %s) print WhereIs('xxx.exe', %s) +print WhereIs('xxx.exe', %s, reject=%s) """ % (subdir_SConscript, repr(string.join(pathdirs_1234, os.pathsep)), repr(string.join(pathdirs_1243, os.pathsep)), repr(pathdirs_1234), repr(pathdirs_1243), + repr(pathdirs_1243), + repr(sub4_xxx_exe) )) test.write(subdir_SConscript, """ @@ -105,6 +108,7 @@ expect = [ test.workpath(sub3_xxx_exe), test.workpath(sub4_xxx_exe), test.workpath(sub3_xxx_exe), test.workpath(sub4_xxx_exe), + test.workpath(sub3_xxx_exe), ] test.run(arguments = ".", @@ -123,6 +127,7 @@ expect = [ test.workpath(sub4_xxx_exe), test.workpath(sub4_xxx_exe), test.workpath(sub3_xxx_exe), test.workpath(sub4_xxx_exe), + test.workpath(sub3_xxx_exe), ] test.run(arguments = ".", -- cgit v0.12