mirror of
https://github.com/esphome/esphome.git
synced 2025-01-18 12:05:41 +00:00
Fix running pre-commit on Windows (#8095)
This commit is contained in:
parent
49c01c26f1
commit
16bf56b0f9
@ -53,6 +53,6 @@ repos:
|
|||||||
hooks:
|
hooks:
|
||||||
- id: pylint
|
- id: pylint
|
||||||
name: pylint
|
name: pylint
|
||||||
entry: script/run-in-env.sh pylint
|
entry: python script/run-in-env pylint
|
||||||
language: script
|
language: system
|
||||||
types: [python]
|
types: [python]
|
||||||
|
53
script/run-in-env
Normal file
53
script/run-in-env
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def find_and_activate_virtualenv():
|
||||||
|
try:
|
||||||
|
# Get the top-level directory of the git repository
|
||||||
|
my_path = subprocess.check_output(
|
||||||
|
["git", "rev-parse", "--show-toplevel"], text=True
|
||||||
|
).strip()
|
||||||
|
except subprocess.CalledProcessError:
|
||||||
|
print(
|
||||||
|
"Error: Not a git repository or unable to determine the top-level directory.",
|
||||||
|
file=sys.stderr,
|
||||||
|
)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# Check for virtual environments
|
||||||
|
for venv in ["venv", ".venv", "."]:
|
||||||
|
activate_path = (
|
||||||
|
Path(my_path)
|
||||||
|
/ venv
|
||||||
|
/ ("Scripts" if os.name == "nt" else "bin")
|
||||||
|
/ "activate"
|
||||||
|
)
|
||||||
|
if activate_path.exists():
|
||||||
|
# Activate the virtual environment by updating PATH
|
||||||
|
env = os.environ.copy()
|
||||||
|
venv_bin_dir = activate_path.parent
|
||||||
|
env["PATH"] = f"{venv_bin_dir}{os.pathsep}{env['PATH']}"
|
||||||
|
env["VIRTUAL_ENV"] = str(venv_bin_dir.parent)
|
||||||
|
print(f"Activated virtual environment: {venv_bin_dir.parent}")
|
||||||
|
|
||||||
|
# Execute the remaining arguments in the new environment
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
subprocess.run(sys.argv[1:], env=env, check=False)
|
||||||
|
else:
|
||||||
|
print(
|
||||||
|
"No command provided to run in the virtual environment.",
|
||||||
|
file=sys.stderr,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
print("No virtual environment found.", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
find_and_activate_virtualenv()
|
@ -1,13 +0,0 @@
|
|||||||
#!/usr/bin/env sh
|
|
||||||
set -eu
|
|
||||||
|
|
||||||
my_path=$(git rev-parse --show-toplevel)
|
|
||||||
|
|
||||||
for venv in venv .venv .; do
|
|
||||||
if [ -f "${my_path}/${venv}/bin/activate" ]; then
|
|
||||||
. "${my_path}/${venv}/bin/activate"
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
exec "$@"
|
|
Loading…
Reference in New Issue
Block a user