Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reimplement which #1182

Merged
merged 1 commit into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion install.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
# though rez is not yet built.
#
from rez.utils._version import _rez_version # noqa: E402
from rez.utils.which import which # noqa: E402
from rez.cli._entry_points import get_specifications # noqa: E402
from rez.backport.shutilwhich import which # noqa: E402
from rez.vendor.distlib.scripts import ScriptMaker # noqa: E402

# switch to builtin venv in python 3.7+
Expand Down
7 changes: 0 additions & 7 deletions src/rez/backport/shutilwhich.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/rez/resolved_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from rez.utils.filesystem import TempDirs, is_subdirectory, canonical_path
from rez.utils.memcached import pool_memcached_connections
from rez.utils.logging_ import print_error, print_warning
from rez.backport.shutilwhich import which
from rez.utils.which import which
from rez.rex import RexExecutor, Python, OutputStyle
from rez.rex_bindings import VersionBinding, VariantBinding, \
VariantsBinding, RequirementsBinding, EphemeralsBinding, intersects
Expand Down
2 changes: 1 addition & 1 deletion src/rez/shells.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"""
from rez.rex import RexExecutor, ActionInterpreter, OutputStyle
from rez.util import shlex_join, is_non_string_iterable
from rez.backport.shutilwhich import which
from rez.utils.which import which
from rez.utils.logging_ import print_warning
from rez.utils.execution import Popen
from rez.system import system
Expand Down
2 changes: 1 addition & 1 deletion src/rez/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from rez.wrapper import Wrapper
from rez.utils.colorize import warning, critical, Printer
from rez.utils.formatting import print_colored_columns, PackageRequest
from rez.backport.shutilwhich import which
from rez.utils.which import which


class Status(object):
Expand Down
3 changes: 2 additions & 1 deletion src/rez/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ def escape_word(s):

# returns path to first program in the list to be successfully found
def which(*programs, **shutilwhich_kwargs):
from rez.backport.shutilwhich import which as which_
from rez.utils.which import which as which_

for prog in programs:
path = which_(prog, **shutilwhich_kwargs)
if path:
Expand Down
85 changes: 85 additions & 0 deletions src/rez/utils/which.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import os
import sys


_default_pathext = '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC'


def which(cmd, mode=os.F_OK | os.X_OK, path=None, env=None):
"""A replacement for shutil.which.

Things we do that shutil.which does not:

* Support specifying `env`
* Take into account '%systemroot%' possible presence in `path` (windows)
* Take into account symlinks to executables (windows)
"""
iswin = (sys.platform == "win32")
pathext = []
if env is None:
env = os.environ

# 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))

# If we're given a path with a directory part, look it up directly rather
# than referring to PATH directories. This includes checking relative to the
# current directory, e.g. ./script. Note that `path` is ignored in this case.
#
dirname, filename = os.path.split(cmd)
if dirname:
path = dirname
cmd = filename

if path is None:
path = env.get("PATH", os.defpath)
if not path:
return None
path = path.split(os.pathsep)

if iswin:
# The current directory takes precedence on Windows
if not dirname and os.curdir not in path:
path.insert(0, os.curdir)

# PATHEXT is necessary to check on Windows
pathext = env.get("PATHEXT", _default_pathext).split(os.pathsep)
pathext = [x.lower() for x in pathext]

# iterate over paths
seen = set()
for dir_ in path:
normdir = os.path.normcase(dir_)

# On windows the system paths might contain %systemroot%
normdir = os.path.expandvars(normdir)

if normdir in seen:
continue
seen.add(normdir)

# search for matching cmd
if iswin:
# Account for cmd possibly being a symlink. A symlink can be an
# executable on windows without an extension. If it is, see if its
# target's extension matches any of the expected path extensions.
#
realfile = os.path.realpath(os.path.join(normdir, cmd)).lower()
if any(realfile.endswith(x) for x in pathext):
files = [cmd]
else:
files = [(cmd + ext) for ext in pathext]
else:
files = [cmd]

for thefile in files:
name = os.path.join(normdir, thefile)
if _access_check(name, mode):
return name

return None
12 changes: 0 additions & 12 deletions src/rez/vendor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,6 @@ Updated (July 2019) to coincide with packaging lib addition that depends on.
Also now required to support py2/3 interoperability.
</td></tr>

<!-- ######################################################### -->
<tr><td>
whichcraft
</td><td>
0.6.1
</td><td>
BSD 3-Clause
</td><td>
https://github.com/cookiecutter/whichcraft<br>
unmodified version added in commit f5b0ea0
</td></tr>

<!-- ######################################################### -->
<tr><td>
yaml lib (PyYAML)
Expand Down
12 changes: 0 additions & 12 deletions src/rez/vendor/whichcraft/LICENSE

This file was deleted.

Empty file.
86 changes: 0 additions & 86 deletions src/rez/vendor/whichcraft/whichcraft.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/rezplugins/build_system/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from rez.packages import get_developer_package
from rez.utils.platform_ import platform_
from rez.config import config
from rez.backport.shutilwhich import which
from rez.utils.which import which
from rez.vendor.schema.schema import Or
from rez.vendor.six import six
from rez.shells import create_shell
Expand Down