File size: 2,596 Bytes
71687cf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | # Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
"""Utilities for finding executables and `conda-*` commands."""
import os
import re
import sys
import sysconfig
from functools import cache
from os.path import basename, expanduser, isfile, join
from ..common.compat import on_win
def find_executable(executable, include_others=True):
# backwards compatibility
global dir_paths
if include_others:
from ..utils import sys_prefix_unfollowed
prefixes = [sys_prefix_unfollowed()]
if sys.prefix != prefixes[0]:
prefixes.append(sys.prefix)
dir_paths = [join(p, basename(sysconfig.get_path("scripts"))) for p in prefixes]
# Is this still needed?
if on_win:
dir_paths.append("C:\\cygwin\\bin")
else:
dir_paths = []
dir_paths.extend(os.environ.get("PATH", "").split(os.pathsep))
for dir_path in dir_paths:
if on_win:
for ext in (".exe", ".bat", ""):
path = join(dir_path, executable + ext)
if isfile(path):
return path
else:
path = join(dir_path, executable)
if isfile(expanduser(path)):
return expanduser(path)
return None
@cache
def find_commands(include_others=True):
if include_others:
from ..utils import sys_prefix_unfollowed
prefixes = [sys_prefix_unfollowed()]
if sys.prefix != prefixes[0]:
prefixes.append(sys.prefix)
dir_paths = [join(p, basename(sysconfig.get_path("scripts"))) for p in prefixes]
# Is this still needed?
if on_win:
dir_paths.append("C:\\cygwin\\bin")
else:
dir_paths = []
dir_paths.extend(os.environ.get("PATH", "").split(os.pathsep))
if on_win:
pat = re.compile(r"conda-([\w\-]+)(\.(exe|bat))?$")
else:
pat = re.compile(r"conda-([\w\-]+)$")
res = set()
for dir_path in dir_paths:
try:
for entry in os.scandir(dir_path):
m = pat.match(entry.name)
if m and entry.is_file():
res.add(m.group(1))
except (FileNotFoundError, NotADirectoryError, PermissionError, OSError):
# FileNotFoundError: path doesn't exist
# NotADirectoryError: path is not a directory
# PermissionError: user doesn't have read access
# OSError: [WinError 123] The filename, directory name, or volume
# label syntax is incorrect
continue
return tuple(sorted(res))
|