Skip to content

Commit 6683ed4

Browse files
authored
Portably find ruff binary path from Python (astral-sh#2574)
Prefer the version from a currently active virtualenv over a version from `pip install --user`. Add the .exe extension on Windows, and find the path for `pip install --user` correctly on Windows. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
1 parent ced5508 commit 6683ed4

1 file changed

Lines changed: 23 additions & 14 deletions

File tree

‎python/ruff/__main__.py‎

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,32 @@
33
import sysconfig
44
from pathlib import Path
55

6-
RUFF_PATHS = [
7-
Path(sysconfig.get_config_var("userbase")) / "bin" / "ruff",
8-
Path(sysconfig.get_path("scripts")) / "ruff",
9-
]
10-
116

127
def find_ruff_bin() -> Path:
138
"""Return the ruff binary path."""
14-
for ruff_path in RUFF_PATHS:
15-
if ruff_path.is_file():
16-
return ruff_path
17-
raise FileNotFoundError(ruff_path)
9+
10+
ruff_exe = "ruff" + sysconfig.get_config_var("EXE")
11+
12+
path = Path(sysconfig.get_path("scripts")) / ruff_exe
13+
if path.is_file():
14+
return path
15+
16+
if sys.version_info >= (3, 10):
17+
user_scheme = sysconfig.get_preferred_scheme("user")
18+
elif os.name == "nt":
19+
user_scheme = "nt_user"
20+
elif sys.platform == "darwin" and sys._framework:
21+
user_scheme = "osx_framework_user"
22+
else:
23+
user_scheme = "posix_user"
24+
25+
path = Path(sysconfig.get_path("scripts", scheme=user_scheme)) / ruff_exe
26+
if path.is_file():
27+
return path
28+
29+
raise FileNotFoundError(path)
1830

1931

2032
if __name__ == "__main__":
21-
try:
22-
ruff = find_ruff_bin()
23-
except FileNotFoundError as e:
24-
raise FileNotFoundError(e) from e
25-
sys.exit(os.spawnv(os.P_WAIT, ruff, [ruff, *sys.argv[1:]]))
33+
ruff = find_ruff_bin()
34+
sys.exit(os.spawnv(os.P_WAIT, ruff, ["ruff", *sys.argv[1:]]))

0 commit comments

Comments
 (0)