|
1 | | -from __future__ import absolute_import |
2 | | - |
3 | | -import os |
4 | | -import sys |
5 | | - |
6 | 1 | import numpy as np |
7 | 2 |
|
8 | 3 |
|
9 | | -# Copy-pasted from Python 3.4's shutil. |
10 | | -def which(cmd, mode=os.F_OK | os.X_OK, path=None): |
11 | | - """Given a command, mode, and a PATH string, return the path which |
12 | | - conforms to the given mode on the PATH, or None if there is no such |
13 | | - file. |
14 | | -
|
15 | | - `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result |
16 | | - of os.environ.get("PATH"), or can be overridden with a custom search |
17 | | - path. |
18 | | -
|
19 | | - """ |
20 | | - # Check that a given file can be accessed with the correct mode. |
21 | | - # Additionally check that `file` is not a directory, as on Windows |
22 | | - # directories pass the os.access check. |
23 | | - def _access_check(fn, mode): |
24 | | - return (os.path.exists(fn) and os.access(fn, mode) |
25 | | - and not os.path.isdir(fn)) |
26 | | - |
27 | | - # If we're given a path with a directory part, look it up directly rather |
28 | | - # than referring to PATH directories. This includes checking relative to the |
29 | | - # current directory, e.g. ./script |
30 | | - if os.path.dirname(cmd): |
31 | | - if _access_check(cmd, mode): |
32 | | - return cmd |
33 | | - return None |
34 | | - |
35 | | - if path is None: |
36 | | - path = os.environ.get("PATH", os.defpath) |
37 | | - if not path: |
38 | | - return None |
39 | | - path = path.split(os.pathsep) |
40 | | - |
41 | | - if sys.platform == "win32": |
42 | | - # The current directory takes precedence on Windows. |
43 | | - if not os.curdir in path: |
44 | | - path.insert(0, os.curdir) |
45 | | - |
46 | | - # PATHEXT is necessary to check on Windows. |
47 | | - pathext = os.environ.get("PATHEXT", "").split(os.pathsep) |
48 | | - # See if the given file matches any of the expected path extensions. |
49 | | - # This will allow us to short circuit when given "python.exe". |
50 | | - # If it does match, only test that one, otherwise we have to try |
51 | | - # others. |
52 | | - if any(cmd.lower().endswith(ext.lower()) for ext in pathext): |
53 | | - files = [cmd] |
54 | | - else: |
55 | | - files = [cmd + ext for ext in pathext] |
56 | | - else: |
57 | | - # On other platforms you don't have things like PATHEXT to tell you |
58 | | - # what file suffixes are executable, so just pass on cmd as-is. |
59 | | - files = [cmd] |
60 | | - |
61 | | - seen = set() |
62 | | - for dir in path: |
63 | | - normdir = os.path.normcase(dir) |
64 | | - if not normdir in seen: |
65 | | - seen.add(normdir) |
66 | | - for thefile in files: |
67 | | - name = os.path.join(dir, thefile) |
68 | | - if _access_check(name, mode): |
69 | | - return name |
70 | | - return None |
71 | | - |
72 | | - |
73 | 4 | # Copy-pasted from numpy.lib.stride_tricks 1.11.2. |
74 | 5 | def _maybe_view_as_subclass(original_array, new_array): |
75 | 6 | if type(original_array) is not type(new_array): |
|
0 commit comments