Skip to content

Commit 89f2afd

Browse files
committed
Always use shutil.which()
1 parent 4484ee2 commit 89f2afd

File tree

2 files changed

+3
-71
lines changed

2 files changed

+3
-71
lines changed

pyperformance/cli.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import argparse
22
import os.path
3+
import shutil
34
import sys
45

5-
from pyperformance.venv import exec_in_virtualenv, which, cmd_venv
6+
from pyperformance.venv import exec_in_virtualenv, cmd_venv
67

78

89
def comma_separated(values):
@@ -167,7 +168,7 @@ def parse_args():
167168
# Replace "~" with the user home directory
168169
options.python = os.path.expanduser(options.python)
169170
# Try to the absolute path to the binary
170-
abs_python = which(options.python)
171+
abs_python = shutil.which(options.python)
171172
if not abs_python:
172173
print("ERROR: Unable to locate the Python executable: %r" %
173174
options.python)

pyperformance/venv.py

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -15,75 +15,6 @@
1515
REQ_OLD_PIP = 'pip==7.1.2'
1616
REQ_OLD_SETUPTOOLS = 'setuptools==18.5'
1717

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

8920

0 commit comments

Comments
 (0)