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

Improve responsiveness when invoked via Python #9315

Merged
merged 2 commits into from
Dec 31, 2023
Merged
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
23 changes: 12 additions & 11 deletions python/ruff/__main__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import os
import subprocess
import sys
import sysconfig
from pathlib import Path

ofek marked this conversation as resolved.
Show resolved Hide resolved

def find_ruff_bin() -> Path:
def find_ruff_bin() -> str:
"""Return the ruff binary path."""

ruff_exe = "ruff" + sysconfig.get_config_var("EXE")

path = Path(sysconfig.get_path("scripts")) / ruff_exe
if path.is_file():
path = os.path.join(sysconfig.get_path("scripts"), ruff_exe)
if os.path.isfile(path):
return path

if sys.version_info >= (3, 10):
Expand All @@ -23,16 +21,19 @@ def find_ruff_bin() -> Path:
else:
user_scheme = "posix_user"

path = Path(sysconfig.get_path("scripts", scheme=user_scheme)) / ruff_exe
path = os.path.join(sysconfig.get_path("scripts", scheme=user_scheme), ruff_exe)
if path.is_file():
return path

raise FileNotFoundError(path)


if __name__ == "__main__":
ruff = find_ruff_bin()
# Passing a path-like to `subprocess.run()` on windows is only supported in 3.8+,
# but we also support 3.7
completed_process = subprocess.run([os.fsdecode(ruff), *sys.argv[1:]])
sys.exit(completed_process.returncode)
ruff = os.fsdecode(find_ruff_bin())
if sys.platform == "win32":
import subprocess

completed_process = subprocess.run([ruff, *sys.argv[1:]])
sys.exit(completed_process.returncode)
else:
os.execvp(ruff, [ruff, *sys.argv[1:]])
Loading