diff options
author | Kali Kaneko <kali@leap.se> | 2013-03-15 01:15:21 +0900 |
---|---|---|
committer | Kali Kaneko <kali@leap.se> | 2013-03-15 01:15:21 +0900 |
commit | 2e26c6c4b1368717681b46e141e2146a17d4e646 (patch) | |
tree | 16b6cc9e4e288e65edbebe864150259774f23d38 /src | |
parent | 41fbcc7b0168e336adf397b394fddf07bcba3f9f (diff) |
add 'which' implementation
Diffstat (limited to 'src')
-rw-r--r-- | src/leap/common/files.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/leap/common/files.py b/src/leap/common/files.py index 7c878e1..9b80236 100644 --- a/src/leap/common/files.py +++ b/src/leap/common/files.py @@ -83,3 +83,45 @@ def mkdir_p(path): pass else: raise + + +# Twisted implementation of which +def which(name, flags=os.X_OK, path_extension="/usr/sbin:/sbin"): + """ + Search PATH for executable files with the given name. + + On newer versions of MS-Windows, the PATHEXT environment variable will be + set to the list of file extensions for files considered executable. This + will normally include things like ".EXE". This fuction will also find files + with the given name ending with any of these extensions. + + On MS-Windows the only flag that has any meaning is os.F_OK. Any other + flags will be ignored. + + @type name: C{str} + @param name: The name for which to search. + + @type flags: C{int} + @param flags: Arguments to L{os.access}. + + @rtype: C{list} + @param: A list of the full paths to files found, in the + order in which they were found. + """ + + result = [] + exts = filter(None, os.environ.get('PATHEXT', '').split(os.pathsep)) + path = os.environ.get('PATH', None) + path += ":" + path_extension + if path is None: + return [] + parts = path.split(os.pathsep) + for p in parts: + p = os.path.join(p, name) + if os.access(p, flags): + result.append(p) + for e in exts: + pext = p + e + if os.access(pext, flags): + result.append(pext) + return result |