|
2 | 2 | # Python 2/3 compatability |
3 | 3 | # Always try Py3 first |
4 | 4 | import os |
| 5 | +import sys |
5 | 6 | from sys import version_info |
6 | 7 |
|
7 | 8 | ustr = str |
@@ -51,3 +52,67 @@ def makedirs(name, mode=0o777, exist_ok=False): |
51 | 52 | except OSError: |
52 | 53 | if not os.path.isdir(name) or not exist_ok: |
53 | 54 | raise |
| 55 | + |
| 56 | + |
| 57 | +def which(cmd, mode=os.F_OK | os.X_OK, path=None): |
| 58 | + """Given a command, mode, and a PATH string, return the path which |
| 59 | + conforms to the given mode on the PATH, or None if there is no such |
| 60 | + file. |
| 61 | +
|
| 62 | + `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result |
| 63 | + of os.environ.get("PATH"), or can be overridden with a custom search |
| 64 | + path. |
| 65 | +
|
| 66 | + Copied from https://hg.python.org/cpython/file/default/Lib/shutil.py |
| 67 | + """ |
| 68 | + # Check that a given file can be accessed with the correct mode. |
| 69 | + # Additionally check that `file` is not a directory, as on Windows |
| 70 | + # directories pass the os.access check. |
| 71 | + def _access_check(fn, mode): |
| 72 | + return (os.path.exists(fn) and os.access(fn, mode) |
| 73 | + and not os.path.isdir(fn)) |
| 74 | + |
| 75 | + # If we're given a path with a directory part, look it up directly rather |
| 76 | + # than referring to PATH directories. This includes checking relative to the |
| 77 | + # current directory, e.g. ./script |
| 78 | + if os.path.dirname(cmd): |
| 79 | + if _access_check(cmd, mode): |
| 80 | + return cmd |
| 81 | + return None |
| 82 | + |
| 83 | + if path is None: |
| 84 | + path = os.environ.get("PATH", os.defpath) |
| 85 | + if not path: |
| 86 | + return None |
| 87 | + path = path.split(os.pathsep) |
| 88 | + |
| 89 | + if sys.platform == "win32": |
| 90 | + # The current directory takes precedence on Windows. |
| 91 | + if not os.curdir in path: |
| 92 | + path.insert(0, os.curdir) |
| 93 | + |
| 94 | + # PATHEXT is necessary to check on Windows. |
| 95 | + pathext = os.environ.get("PATHEXT", "").split(os.pathsep) |
| 96 | + # See if the given file matches any of the expected path extensions. |
| 97 | + # This will allow us to short circuit when given "python.exe". |
| 98 | + # If it does match, only test that one, otherwise we have to try |
| 99 | + # others. |
| 100 | + if any(cmd.lower().endswith(ext.lower()) for ext in pathext): |
| 101 | + files = [cmd] |
| 102 | + else: |
| 103 | + files = [cmd + ext for ext in pathext] |
| 104 | + else: |
| 105 | + # On other platforms you don't have things like PATHEXT to tell you |
| 106 | + # what file suffixes are executable, so just pass on cmd as-is. |
| 107 | + files = [cmd] |
| 108 | + |
| 109 | + seen = set() |
| 110 | + for dir in path: |
| 111 | + normdir = os.path.normcase(dir) |
| 112 | + if not normdir in seen: |
| 113 | + seen.add(normdir) |
| 114 | + for thefile in files: |
| 115 | + name = os.path.join(dir, thefile) |
| 116 | + if _access_check(name, mode): |
| 117 | + return name |
| 118 | + return None |
0 commit comments