diff options
author | Steven Knight <knight@baldmt.com> | 2002-03-27 02:49:41 (GMT) |
---|---|---|
committer | Steven Knight <knight@baldmt.com> | 2002-03-27 02:49:41 (GMT) |
commit | 1ffd7687fe1aa4c9b438a17126052ef3d3234bfe (patch) | |
tree | c469582d0abfb3bad67d6dfd3c4b1279cc8e97a9 /src | |
parent | df70fbb73a1b644373002ebe3c9bc206b256744b (diff) | |
download | SCons-1ffd7687fe1aa4c9b438a17126052ef3d3234bfe.zip SCons-1ffd7687fe1aa4c9b438a17126052ef3d3234bfe.tar.gz SCons-1ffd7687fe1aa4c9b438a17126052ef3d3234bfe.tar.bz2 |
Add WhereIs() functionality.
Diffstat (limited to 'src')
-rw-r--r-- | src/CHANGES.txt | 2 | ||||
-rw-r--r-- | src/engine/SCons/Defaults.py | 16 | ||||
-rw-r--r-- | src/engine/SCons/Script/SConscript.py | 3 | ||||
-rw-r--r-- | src/engine/SCons/Util.py | 45 | ||||
-rw-r--r-- | src/engine/SCons/UtilTests.py | 61 |
5 files changed, 111 insertions, 16 deletions
diff --git a/src/CHANGES.txt b/src/CHANGES.txt index 1ffca34..9225510 100644 --- a/src/CHANGES.txt +++ b/src/CHANGES.txt @@ -57,6 +57,8 @@ RELEASE 0.06 - - Add support for Aliases (phony targets). + - Add a WhereIs() method for searching for path names to executables. + From Steve Leblanc: - Add support for the -U option. diff --git a/src/engine/SCons/Defaults.py b/src/engine/SCons/Defaults.py index 36a6aa1..6dedb52 100644 --- a/src/engine/SCons/Defaults.py +++ b/src/engine/SCons/Defaults.py @@ -52,20 +52,6 @@ import SCons.Util -def whereis(file): - for dir in string.split(os.environ['PATH'], os.pathsep): - f = os.path.join(dir, file) - if os.path.isfile(f): - try: - st = os.stat(f) - except: - continue - if stat.S_IMODE(st[stat.ST_MODE]) & 0111: - return f - return None - - - CFile = SCons.Builder.Builder(name = 'CFile', action = { '.l' : '$LEXCOM', '.y' : '$YACCCOM', @@ -295,7 +281,7 @@ if os.name == 'posix': arcom = '$AR $ARFLAGS $TARGET $SOURCES' ranlib = 'ranlib' - if whereis(ranlib): + if SCons.Util.WhereIs(ranlib): arcom = arcom + '\n$RANLIB $RANLIBFLAGS $TARGET' ConstructionEnvironment = { diff --git a/src/engine/SCons/Script/SConscript.py b/src/engine/SCons/Script/SConscript.py index 2cfc64b..5552015 100644 --- a/src/engine/SCons/Script/SConscript.py +++ b/src/engine/SCons/Script/SConscript.py @@ -179,6 +179,7 @@ def BuildDefaultGlobals(): globals['Export'] = Export globals['File'] = SCons.Node.FS.default_fs.File globals['GetBuildPath'] = GetBuildPath + globals['GetCommandHandler'] = SCons.Action.GetCommandHandler globals['Help'] = Help globals['Import'] = Import globals['Library'] = SCons.Defaults.Library @@ -188,5 +189,5 @@ def BuildDefaultGlobals(): globals['Scanner'] = SCons.Scanner.Base globals['SConscript'] = SConscript globals['SetCommandHandler'] = SCons.Action.SetCommandHandler - globals['GetCommandHandler'] = SCons.Action.GetCommandHandler + globals['WhereIs'] = SCons.Util.WhereIs return globals diff --git a/src/engine/SCons/Util.py b/src/engine/SCons/Util.py index f60e981..ef998a9 100644 --- a/src/engine/SCons/Util.py +++ b/src/engine/SCons/Util.py @@ -31,9 +31,12 @@ __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" import copy +import os import os.path import re +import stat import string +import sys import types import UserDict import UserList @@ -279,3 +282,45 @@ if can_read_reg: HKEY_LOCAL_MACHINE = hkey_mod.HKEY_LOCAL_MACHINE HKEY_CURRENT_USER = hkey_mod.HKEY_CURRENT_USER HKEY_USERS = hkey_mod.HKEY_USERS + + +if sys.platform == 'win32': + + def WhereIs(file, path=None, pathext=None): + if path is None: + path = os.environ['PATH'] + if is_String(path): + path = string.split(path, os.pathsep) + if pathext is None: + pathext = os.environ['PATHEXT'] + if is_String(pathext): + pathext = string.split(pathext, os.pathsep) + for ext in pathext: + if string.lower(ext) == string.lower(file[-len(ext):]): + pathext = [''] + break + for dir in path: + f = os.path.join(dir, file) + for ext in pathext: + fext = f + ext + if os.path.isfile(fext): + return fext + return None + +else: + + def WhereIs(file, path=None, pathext=None): + if path is None: + path = os.environ['PATH'] + if is_String(path): + path = string.split(path, os.pathsep) + for dir in path: + f = os.path.join(dir, file) + if os.path.isfile(f): + try: + st = os.stat(f) + except: + continue + if stat.S_IMODE(st[stat.ST_MODE]) & 0111: + return f + return None diff --git a/src/engine/SCons/UtilTests.py b/src/engine/SCons/UtilTests.py index 67bdbb4..d230a08 100644 --- a/src/engine/SCons/UtilTests.py +++ b/src/engine/SCons/UtilTests.py @@ -221,6 +221,67 @@ class UtilTestCase(unittest.TestCase): assert not is_String({}) assert not is_String([]) + def test_WhereIs(self): + test = TestCmd.TestCmd(workdir = '') + + sub1_xxx_exe = test.workpath('sub1', 'xxx.exe') + sub2_xxx_exe = test.workpath('sub2', 'xxx.exe') + sub3_xxx_exe = test.workpath('sub3', 'xxx.exe') + sub4_xxx_exe = test.workpath('sub4', 'xxx.exe') + + test.subdir('subdir', 'sub1', 'sub2', 'sub3', 'sub4') + + if sys.platform != 'win32': + test.write(sub1_xxx_exe, "\n") + + os.mkdir(sub2_xxx_exe) + + test.write(sub3_xxx_exe, "\n") + os.chmod(sub3_xxx_exe, 0777) + + test.write(sub4_xxx_exe, "\n") + os.chmod(sub4_xxx_exe, 0777) + + env_path = os.environ['PATH'] + + pathdirs_1234 = [ test.workpath('sub1'), + test.workpath('sub2'), + test.workpath('sub3'), + test.workpath('sub4'), + ] + string.split(env_path, os.pathsep) + + pathdirs_1243 = [ test.workpath('sub1'), + test.workpath('sub2'), + test.workpath('sub4'), + test.workpath('sub3'), + ] + string.split(env_path, os.pathsep) + + os.environ['PATH'] = string.join(pathdirs_1234, os.pathsep) + wi = WhereIs('xxx.exe') + assert wi == test.workpath(sub3_xxx_exe), wi + wi = WhereIs('xxx.exe', pathdirs_1243) + assert wi == test.workpath(sub4_xxx_exe), wi + wi = WhereIs('xxx.exe', string.join(pathdirs_1243, os.pathsep)) + 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 + wi = WhereIs('xxx.exe', pathdirs_1234) + assert wi == test.workpath(sub3_xxx_exe), wi + wi = WhereIs('xxx.exe', string.join(pathdirs_1234, os.pathsep)) + assert wi == test.workpath(sub3_xxx_exe), wi + + if sys.platform == 'win32': + wi = WhereIs('xxx', pathext = '') + assert wi is None, wi + + wi = WhereIs('xxx', pathext = '.exe') + assert wi == test.workpath(sub4_xxx_exe), wi + + wi = WhereIs('xxx', path = pathdirs_1234, pathext = '.BAT;.EXE') + assert string.lower(wi) == string.lower(test.workpath(sub3_xxx_exe)), wi + if __name__ == "__main__": suite = unittest.makeSuite(UtilTestCase, 'test_') if not unittest.TextTestRunner().run(suite).wasSuccessful(): |