Replace bash scripts with python scripts

This commit is contained in:
Sonny Bakker 2020-07-30 09:10:47 +02:00
parent e382a52cf1
commit b31fe98ad7
12 changed files with 233 additions and 101 deletions

47
pre-commit.d/autoflake.py Executable file
View file

@ -0,0 +1,47 @@
#!/usr/bin/env python
import os
import subprocess
import sys
from shutil import which
if not os.getenv("VIRTUAL_ENV"):
sys.exit("Ensure your virtualenv is activated.")
file_extensions = (".py",)
autoflake = os.getenv("AUTOFLAKE_BIN", "autoflake")
options = [
"--in-place",
"--remove-unused-variables",
"--remove-all-unused-imports",
"--ignore-init-module-imports"
]
if not which(autoflake):
sys.exit("Autoflake binary not found")
diff = subprocess.check_output(
["git", "diff", "--cached", "--name-only", "--diff-filter", "ACM"],
universal_newlines=True
)
python_files = [
item for item in diff.splitlines() if item.endswith(file_extensions)
]
if not python_files:
sys.exit(0)
try:
output = subprocess.check_output(
[autoflake, *options, *python_files],
universal_newlines=True,
stderr=subprocess.STDOUT
)
except subprocess.CalledProcessError as e:
print("Formatting was unsuccessful:")
sys.exit(e.stdout)
subprocess.run(["git", "add", "-u", *python_files])
sys.exit(0)

View file

@ -1,14 +0,0 @@
#!/bin/bash
FILES=$(git diff --cached --name-only --diff-filter=ACM "*.py" | sed 's| |\\ |g')
if [ ! -z "$FILES" ]; then
# Format all selected files
echo "$FILES" | xargs -I {} /bin/bash -c "/home/sonny/.local/bin/autoflake --in-place --remove-unused-variables {}"
if [ $? -ne 0 ]; then
exit 1
fi
# Add back the modified files to staging
echo "$FILES" | xargs git add
fi

43
pre-commit.d/black.py Executable file
View file

@ -0,0 +1,43 @@
#!/usr/bin/env python
import os
import subprocess
import sys
from shutil import which
if not os.getenv("VIRTUAL_ENV"):
sys.exit("Ensure your virtualenv is activated.")
file_extensions = (".py",)
line_length = os.getenv("BLACK_LINE_LENGTH", 88)
black = os.getenv("BLACK_BIN", "black")
options = ["-l", str(line_length),]
if not which(black):
sys.exit("Black binary not found")
diff = subprocess.check_output(
["git", "diff", "--cached", "--name-only", "--diff-filter", "ACM"],
universal_newlines=True
)
python_files = [
item for item in diff.splitlines() if item.endswith(file_extensions)
]
if not python_files:
sys.exit(0)
try:
output = subprocess.check_output(
[black, *options, *python_files],
universal_newlines=True,
stderr=subprocess.STDOUT
)
except subprocess.CalledProcessError as e:
print("Formatting was unsuccessful:")
sys.exit(e.stdout)
subprocess.run(["git", "add", "-u", *python_files])
sys.exit(0)

39
pre-commit.d/isort.py Executable file
View file

@ -0,0 +1,39 @@
#!/usr/bin/env python
import os
import subprocess
import sys
from shutil import which
if not os.getenv("VIRTUAL_ENV"):
sys.exit("Ensure your virtualenv is activated.")
file_extensions = (".py",)
isort = os.getenv("ISORT_BIN", "isort")
if not which(isort):
sys.exit("Isort binary not found")
diff = subprocess.check_output(
["git", "diff", "--cached", "--name-only", "--diff-filter", "ACM"],
universal_newlines=True
)
python_files = [
item for item in diff.splitlines() if item.endswith(file_extensions)
]
if not python_files:
sys.exit(0)
try:
output = subprocess.check_output(
[isort, *python_files], universal_newlines=True, stderr=subprocess.STDOUT
)
except subprocess.CalledProcessError as e:
print("Formatting was unsuccessful:")
sys.exit(e.stdout)
subprocess.run(["git", "add", "-u", *python_files])
sys.exit(0)

View file

@ -1,14 +0,0 @@
#!/bin/bash
FILES=$(git diff --cached --name-only --diff-filter=ACM "*.py" | sed 's| |\\ |g')
if [ ! -z "$FILES" ]; then
# Format all selected files
echo "$FILES" | xargs ./env/bin/isort
if [ $? -ne 0 ]; then
exit 1
fi
# Add back the modified/prettified files to staging
echo "$FILES" | xargs git add
fi

42
pre-commit.d/prettier.py Executable file
View file

@ -0,0 +1,42 @@
#!/usr/bin/env python
import os
import subprocess
import sys
from shutil import which
if not os.getenv("VIRTUAL_ENV"):
sys.exit("Ensure your virtualenv is activated.")
file_extensions = (".js", ".jsx",)
prettier = os.getenv("PRETTIER_BIN", "./node_modules/.bin/prettier")
options = ["--write"]
if not which(prettier):
sys.exit("Prettier binary not found")
diff = subprocess.check_output(
["git", "diff", "--cached", "--name-only", "--diff-filter", "ACM"],
universal_newlines=True
)
javascript_files = [
item for item in diff.splitlines() if item.endswith(file_extensions)
]
if not javascript_files:
sys.exit(0)
try:
output = subprocess.check_output(
[prettier, *options, *javascript_files],
universal_newlines=True,
stderr=subprocess.STDOUT
)
except subprocess.CalledProcessError as e:
print("Formatting was unsuccessful:")
sys.exit(e.stdout)
subprocess.run(["git", "add", "-u", *javascript_files])
sys.exit(0)

View file

@ -1,14 +0,0 @@
#!/bin/sh
FILES=$(git diff --cached --name-only --diff-filter=ACM "*.js" | sed 's| |\\ |g')
if [ ! -z "$FILES" ]; then
# Prettify all selected files
echo "$FILES" | xargs ./node_modules/.bin/prettier --write
if [ $? -ne 0 ]; then
exit 1
fi
# Add back the modified/prettified files to staging
echo "$FILES" | xargs git add
fi

View file

@ -1,14 +0,0 @@
#!/bin/bash
FILES=$(git diff --cached --name-only --diff-filter=ACM "*.py" | sed 's| |\\ |g')
if [ ! -z "$FILES" ]; then
# Format all selected files
echo "$FILES" | xargs ./env/bin/yapf --in-place --verbose --style=./yapf.conf
if [ $? -ne 0 ]; then
exit 1
fi
# Add back the modified files to staging
echo "$FILES" | xargs git add
fi