ruff/python/ruff/__main__.py
konstin acb23dce3c
Fix subprocess.run on Windows Python 3.7 (#5220)
## Summary

From the [subprocess
docs](https://docs.python.org/3/library/subprocess.html#subprocess.Popen):

> Changed in version 3.6: args parameter accepts a path-like object if
shell is False and a sequence containing path-like objects on POSIX.
>
> Changed in version 3.8: args parameter accepts a path-like object if
shell is False and a sequence containing bytes and path-like objects on
Windows.

We want to support python 3.7 on windows, so we need to convert the
`Path` into a `str`
2023-06-20 13:53:32 -04:00

38 lines
1 KiB
Python

import os
import subprocess
import sys
import sysconfig
from pathlib import Path
def find_ruff_bin() -> Path:
"""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():
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__":
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)