MCPcopy Index your code
hub / github.com/RustPython/RustPython / which

Function which

Lib/shutil.py:1575–1655  ·  view source on GitHub ↗

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

(cmd, mode=os.F_OK | os.X_OK, path=None)

Source from the content-addressed store, hash-verified

1573
1574
1575def which(cmd, mode=os.F_OK | os.X_OK, path=None):
1576 """Given a command, mode, and a PATH string, return the path which
1577 conforms to the given mode on the PATH, or None if there is no such
1578 file.
1579
1580 `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result
1581 of os.environ.get("PATH"), or can be overridden with a custom search
1582 path.
1583
1584 """
1585 use_bytes = isinstance(cmd, bytes)
1586
1587 # If we're given a path with a directory part, look it up directly rather
1588 # than referring to PATH directories. This includes checking relative to
1589 # the current directory, e.g. ./script
1590 dirname, cmd = os.path.split(cmd)
1591 if dirname:
1592 path = [dirname]
1593 else:
1594 if path is None:
1595 path = os.environ.get("PATH", None)
1596 if path is None:
1597 try:
1598 path = os.confstr("CS_PATH")
1599 except (AttributeError, ValueError):
1600 # os.confstr() or CS_PATH is not available
1601 path = os.defpath
1602 # bpo-35755: Don't use os.defpath if the PATH environment variable
1603 # is set to an empty string
1604
1605 # PATH='' doesn't match, whereas PATH=':' looks in the current
1606 # directory
1607 if not path:
1608 return None
1609
1610 if use_bytes:
1611 path = os.fsencode(path)
1612 path = path.split(os.fsencode(os.pathsep))
1613 else:
1614 path = os.fsdecode(path)
1615 path = path.split(os.pathsep)
1616
1617 if sys.platform == "win32" and _win_path_needs_curdir(cmd, mode):
1618 curdir = os.curdir
1619 if use_bytes:
1620 curdir = os.fsencode(curdir)
1621 path.insert(0, curdir)
1622
1623 if sys.platform == "win32":
1624 # PATHEXT is necessary to check on Windows.
1625 pathext_source = os.getenv("PATHEXT") or _WIN_DEFAULT_PATHEXT
1626 pathext = pathext_source.split(os.pathsep)
1627 pathext = [ext.rstrip('.') for ext in pathext if ext]
1628
1629 if use_bytes:
1630 pathext = [os.fsencode(ext) for ext in pathext]
1631
1632 files = [cmd + ext for ext in pathext]

Callers 1

get_executable_pathFunction · 0.85

Calls 15

isinstanceFunction · 0.85
_win_path_needs_curdirFunction · 0.85
setFunction · 0.85
_access_checkFunction · 0.85
fsencodeMethod · 0.80
fsdecodeMethod · 0.80
normcaseMethod · 0.80
anyFunction · 0.70
splitMethod · 0.45
getMethod · 0.45
insertMethod · 0.45
rstripMethod · 0.45

Tested by

no test coverage detected