From b31fe98ad75283e89b762d66b6cba5ea82212b60 Mon Sep 17 00:00:00 2001 From: Sonny Bakker Date: Thu, 30 Jul 2020 09:10:47 +0200 Subject: [PATCH] Replace bash scripts with python scripts --- post-commit | 29 +++++++++++++++--------- post-commit.d/ctags.py | 28 +++++++++++++++++++++++ post-commit.d/ctags.sh | 20 ----------------- pre-commit | 30 +++++++++++++------------ pre-commit.d/autoflake.py | 47 +++++++++++++++++++++++++++++++++++++++ pre-commit.d/autoflake.sh | 14 ------------ pre-commit.d/black.py | 43 +++++++++++++++++++++++++++++++++++ pre-commit.d/isort.py | 39 ++++++++++++++++++++++++++++++++ pre-commit.d/isort.sh | 14 ------------ pre-commit.d/prettier.py | 42 ++++++++++++++++++++++++++++++++++ pre-commit.d/prettier.sh | 14 ------------ pre-commit.d/yapf.sh | 14 ------------ 12 files changed, 233 insertions(+), 101 deletions(-) create mode 100755 post-commit.d/ctags.py delete mode 100755 post-commit.d/ctags.sh create mode 100755 pre-commit.d/autoflake.py delete mode 100755 pre-commit.d/autoflake.sh create mode 100755 pre-commit.d/black.py create mode 100755 pre-commit.d/isort.py delete mode 100755 pre-commit.d/isort.sh create mode 100755 pre-commit.d/prettier.py delete mode 100755 pre-commit.d/prettier.sh delete mode 100755 pre-commit.d/yapf.sh diff --git a/post-commit b/post-commit index d6bbe53..56cbb74 100755 --- a/post-commit +++ b/post-commit @@ -1,13 +1,20 @@ -#!/bin/bash +#!/usr/bin/env python +# Use this script in combination with the post-commit.d directory inside .git/hooks/ +# to run multiple scripts after commiting -# Check if the directory is the root directory -if [ ! -d ".git/" ]; then - echo "Please commit from within the root directory" - exit 1 -fi +import os +import subprocess +import sys -# Run every file inside the post-commit.d directory -for file in .git/hooks/post-commit.d/* -do - . $file -done +if not os.path.isdir(".git"): + sys.exit("Please commit from the project root directory") + +files = os.scandir(".git/hooks/post-commit.d/") + +for executable in files: + try: + subprocess.check_output( + [executable.path], universal_newlines=True, stderr=subprocess.STDOUT + ) + except subprocess.CalledProcessError as e: + sys.exit(e.stdout) diff --git a/post-commit.d/ctags.py b/post-commit.d/ctags.py new file mode 100755 index 0000000..5ab2148 --- /dev/null +++ b/post-commit.d/ctags.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python + +import os +import subprocess +import sys + +from shutil import which + +homedir = os.path.expanduser("~") +tag_dir = os.path.join(homedir, ".tags") +tag_file = "newsreader.tags" +options = ["-f", f"{tag_dir}/{tag_file}", "-R", os.getcwd()] + +if not os.getenv("CREATE_TAGS"): + sys.exit(0) + +if not which("ctags"): + sys.exit("ctags binary is not executable or not present!") + +if not os.path.exists(tag_dir): + os.mkdir(tag_dir) + +try: + output = subprocess.check_output( + ["ctags", *options], universal_newlines=True, stderr=subprocess.STDOUT + ) +except subprocess.CalledProcessError as e: + sys.exit(e.stdout) diff --git a/post-commit.d/ctags.sh b/post-commit.d/ctags.sh deleted file mode 100755 index 24746a0..0000000 --- a/post-commit.d/ctags.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/bash - -TAG_DIR="$HOME/.tags" -PROJECT_TAG_FILE="omx-gui.tags" - -if [ "$CREATE_TAGS" != true ]; then - exit 0 -fi - -if [ ! -x /usr/bin/ctags ]; then - echo "ctags binary is not executable or not present!" - exit 1 -fi - -if [ ! -d $HOME/.tags ]; then - echo "tags directory not created, creating it for you.." - /bin/mkdir $HOME/.tags -fi - -/usr/bin/ctags -f "$TAG_DIR/$PROJECT_TAG_FILE" -R "$PWD" --languages=Python,Javascript > /dev/null 2>&1 diff --git a/pre-commit b/pre-commit index cee781b..f671a04 100755 --- a/pre-commit +++ b/pre-commit @@ -1,18 +1,20 @@ -#!/bin/bash -STASH_NAME="pre-commit-$(date %d-%m-%Y-%H-%M-%S)" +#!/usr/bin/env python +# Use this script in combination with the pre-commit.d directory inside .git/hooks/ +# to run multiple scripts before commiting -# Check if the directory is the root directory -if [ ! -d ".git/" ]; then - echo "Please commit from within the root directory" - exit 1 -fi +import os +import subprocess +import sys -git stash push --quiet --keep-index --message $STASH_NAME +if not os.path.isdir(".git"): + sys.exit("Please commit from the project root directory") -# Run every file inside the pre-commit.d directory -for file in .git/hooks/pre-commit.d/* -do - . $file -done +files = os.scandir(".git/hooks/pre-commit.d/") -git stash pop --quiet +for executable in files: + try: + subprocess.check_output( + [executable.path], universal_newlines=True, stderr=subprocess.STDOUT + ) + except subprocess.CalledProcessError as e: + sys.exit(e.stdout) diff --git a/pre-commit.d/autoflake.py b/pre-commit.d/autoflake.py new file mode 100755 index 0000000..ef443eb --- /dev/null +++ b/pre-commit.d/autoflake.py @@ -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) diff --git a/pre-commit.d/autoflake.sh b/pre-commit.d/autoflake.sh deleted file mode 100755 index 7e4ec3f..0000000 --- a/pre-commit.d/autoflake.sh +++ /dev/null @@ -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 diff --git a/pre-commit.d/black.py b/pre-commit.d/black.py new file mode 100755 index 0000000..2ea9088 --- /dev/null +++ b/pre-commit.d/black.py @@ -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) diff --git a/pre-commit.d/isort.py b/pre-commit.d/isort.py new file mode 100755 index 0000000..89ecd64 --- /dev/null +++ b/pre-commit.d/isort.py @@ -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) diff --git a/pre-commit.d/isort.sh b/pre-commit.d/isort.sh deleted file mode 100755 index 09757c0..0000000 --- a/pre-commit.d/isort.sh +++ /dev/null @@ -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 diff --git a/pre-commit.d/prettier.py b/pre-commit.d/prettier.py new file mode 100755 index 0000000..6d3df70 --- /dev/null +++ b/pre-commit.d/prettier.py @@ -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) diff --git a/pre-commit.d/prettier.sh b/pre-commit.d/prettier.sh deleted file mode 100755 index 86670c5..0000000 --- a/pre-commit.d/prettier.sh +++ /dev/null @@ -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 diff --git a/pre-commit.d/yapf.sh b/pre-commit.d/yapf.sh deleted file mode 100755 index df06a90..0000000 --- a/pre-commit.d/yapf.sh +++ /dev/null @@ -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