2 from shutil
import which
# Python >= 3.3
6 # This is copied from Python 3.4.1
7 def which(cmd
, mode
=os
.F_OK | os
.X_OK
, path
=None):
8 """Given a command, mode, and a PATH string, return the path which
9 conforms to the given mode on the PATH, or None if there is no such
12 `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result
13 of os.environ.get("PATH"), or can be overridden with a custom search
17 # Check that a given file can be accessed with the correct mode.
18 # Additionally check that `file` is not a directory, as on Windows
19 # directories pass the os.access check.
20 def _access_check(fn
, mode
):
21 return (os
.path
.exists(fn
) and os
.access(fn
, mode
)
22 and not os
.path
.isdir(fn
))
24 # If we're given a path with a directory part, look it up directly rather
25 # than referring to PATH directories. This includes checking relative to the
26 # current directory, e.g. ./script
27 if os
.path
.dirname(cmd
):
28 if _access_check(cmd
, mode
):
33 path
= os
.environ
.get("PATH", os
.defpath
)
36 path
= path
.split(os
.pathsep
)
38 if sys
.platform
== "win32":
39 # The current directory takes precedence on Windows.
40 if not os
.curdir
in path
:
41 path
.insert(0, os
.curdir
)
43 # PATHEXT is necessary to check on Windows.
44 pathext
= os
.environ
.get("PATHEXT", "").split(os
.pathsep
)
45 # See if the given file matches any of the expected path extensions.
46 # This will allow us to short circuit when given "python.exe".
47 # If it does match, only test that one, otherwise we have to try
49 if any(cmd
.lower().endswith(ext
.lower()) for ext
in pathext
):
52 files
= [cmd
+ ext
for ext
in pathext
]
54 # On other platforms you don't have things like PATHEXT to tell you
55 # what file suffixes are executable, so just pass on cmd as-is.
60 normdir
= os
.path
.normcase(dir)
61 if not normdir
in seen
:
64 name
= os
.path
.join(dir, thefile
)
65 if _access_check(name
, mode
):
70 class PtyProcessError(Exception):
71 """Generic error class for this package."""