Chromium Code Reviews| Index: scripts/common/chromium_utils.py |
| diff --git a/scripts/common/chromium_utils.py b/scripts/common/chromium_utils.py |
| index 89bee7a154060c32dca5725109bb8ff01f004618..4ea602a22ea066ffff6345b94627d89633ecbd61 100644 |
| --- a/scripts/common/chromium_utils.py |
| +++ b/scripts/common/chromium_utils.py |
| @@ -2039,3 +2039,55 @@ def IsClangWinBuild(build_dir, target): |
| if m: |
| return 'clang' in m.group('compiler_path') |
| return False |
| + |
| +# Everything below this point has been copied from the Python-3.3 sources. |
| +def Which(cmd, mode=os.F_OK | os.X_OK, path=None): |
| + """Given a command, mode, and a PATH string, return the path which |
| + conforms to the given mode on the PATH, or None if there is no such |
| + file. |
| + `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result |
| + of os.environ.get("PATH"), or can be overridden with a custom search |
| + path. |
| + """ |
| + # Check that a given file can be accessed with the correct mode. |
| + # Additionally check that `file` is not a directory, as on Windows |
| + # directories pass the os.access check. |
| + def _access_check(fn, mode): |
| + return (os.path.exists(fn) and os.access(fn, mode) |
| + and not os.path.isdir(fn)) |
| + |
| + # Short circuit. If we're given a full path which matches the mode |
| + # and it exists, we're done here. |
| + if _access_check(cmd, mode): |
| + return cmd |
|
hinoka
2016/06/24 18:47:33
This returns "powershell.exe" if it's in cwd
|
| + |
| + path = (path or os.environ.get("PATH", os.defpath)).split(os.pathsep) |
| + |
| + if sys.platform == "win32": |
| + # The current directory takes precedence on Windows. |
| + if not os.curdir in path: |
| + path.insert(0, os.curdir) |
| + |
| + # PATHEXT is necessary to check on Windows. |
| + pathext = os.environ.get("PATHEXT", "").split(os.pathsep) |
| + # See if the given file matches any of the expected path extensions. |
| + # This will allow us to short circuit when given "python.exe". |
| + matches = [cmd for ext in pathext if cmd.lower().endswith(ext.lower())] |
| + # If it does match, only test that one, otherwise we have to try |
| + # others. |
| + files = [cmd] if matches else [cmd + ext.lower() for ext in pathext] |
| + else: |
| + # On other platforms you don't have things like PATHEXT to tell you |
| + # what file suffixes are executable, so just pass on cmd as-is. |
| + files = [cmd] |
| + |
| + seen = set() |
| + for dir in path: |
|
hinoka
2016/06/24 18:47:33
funky choice of varname...
|
| + dir = os.path.normcase(dir) |
| + if not dir in seen: |
| + seen.add(dir) |
| + for thefile in files: |
| + name = os.path.join(dir, thefile) |
| + if _access_check(name, mode): |
| + return name |
| + return None |