mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:09:22 +00:00
Portably find ruff binary path from Python (#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>
This commit is contained in:
parent
ced55084db
commit
6683ed49bc
1 changed files with 23 additions and 14 deletions
|
@ -3,23 +3,32 @@ import sys
|
|||
import sysconfig
|
||||
from pathlib import Path
|
||||
|
||||
RUFF_PATHS = [
|
||||
Path(sysconfig.get_config_var("userbase")) / "bin" / "ruff",
|
||||
Path(sysconfig.get_path("scripts")) / "ruff",
|
||||
]
|
||||
|
||||
|
||||
def find_ruff_bin() -> Path:
|
||||
"""Return the ruff binary path."""
|
||||
for ruff_path in RUFF_PATHS:
|
||||
if ruff_path.is_file():
|
||||
return ruff_path
|
||||
raise FileNotFoundError(ruff_path)
|
||||
|
||||
ruff_exe = "ruff" + sysconfig.get_config_var("EXE")
|
||||
|
||||
path = Path(sysconfig.get_path("scripts")) / ruff_exe
|
||||
if path.is_file():
|
||||
return path
|
||||
|
||||
if sys.version_info >= (3, 10):
|
||||
user_scheme = sysconfig.get_preferred_scheme("user")
|
||||
elif os.name == "nt":
|
||||
user_scheme = "nt_user"
|
||||
elif sys.platform == "darwin" and sys._framework:
|
||||
user_scheme = "osx_framework_user"
|
||||
else:
|
||||
user_scheme = "posix_user"
|
||||
|
||||
path = Path(sysconfig.get_path("scripts", scheme=user_scheme)) / ruff_exe
|
||||
if path.is_file():
|
||||
return path
|
||||
|
||||
raise FileNotFoundError(path)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
ruff = find_ruff_bin()
|
||||
except FileNotFoundError as e:
|
||||
raise FileNotFoundError(e) from e
|
||||
sys.exit(os.spawnv(os.P_WAIT, ruff, [ruff, *sys.argv[1:]]))
|
||||
ruff = find_ruff_bin()
|
||||
sys.exit(os.spawnv(os.P_WAIT, ruff, ["ruff", *sys.argv[1:]]))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue