summaryrefslogtreecommitdiff
path: root/scripts/git-hooks/pre-commit-python.hook
diff options
context:
space:
mode:
authorNicolas Dufresne <nicolas.dufresne@collabora.com>2021-09-24 14:11:13 -0400
committerThibault Saunier <tsaunier@igalia.com>2021-09-24 17:47:01 -0300
commit3037fde5ebddf3d755d29b4f06f21e6ca122d484 (patch)
treefa88032ccbf34d03c5cbd56b52073cfa6ef9cb8b /scripts/git-hooks/pre-commit-python.hook
parent776d8a661720b05861ab797cca01ef37fdaf6b78 (diff)
downloadgstreamer-3037fde5ebddf3d755d29b4f06f21e6ca122d484.tar.gz
Move commit gst-indent hook to the rootmonorepo-start
This renable at meson setup time the installation of the gst-indent commit hook. The hooks were kept from gst-devtools as this set supports both C checks and Python checks. Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/904>
Diffstat (limited to 'scripts/git-hooks/pre-commit-python.hook')
-rwxr-xr-xscripts/git-hooks/pre-commit-python.hook81
1 files changed, 81 insertions, 0 deletions
diff --git a/scripts/git-hooks/pre-commit-python.hook b/scripts/git-hooks/pre-commit-python.hook
new file mode 100755
index 0000000000..14fbc63bfd
--- /dev/null
+++ b/scripts/git-hooks/pre-commit-python.hook
@@ -0,0 +1,81 @@
+#!/usr/bin/env python3
+import os
+import subprocess
+import sys
+import tempfile
+
+NOT_PYCODESTYLE_COMPLIANT_MESSAGE_PRE = \
+ "Your code is not fully pycodestyle compliant and contains"\
+ " the following coding style issues:\n\n"
+
+NOT_PYCODESTYLE_COMPLIANT_MESSAGE_POST = \
+ "Please fix these errors and commit again, you can do so "\
+ "from the root directory automatically like this, assuming the whole "\
+ "file is to be commited:"
+
+NO_PYCODESTYLE_MESSAGE = \
+ "You should install the pycodestyle style checker to be able"\
+ " to commit in this repo.\nIt allows us to garantee that "\
+ "anything that is commited respects the pycodestyle coding style "\
+ "standard.\nYou can install it:\n"\
+ " * on ubuntu, debian: $sudo apt-get install pycodestyle \n"\
+ " * on fedora: #yum install python3-pycodestyle \n"\
+ " * on arch: #pacman -S python-pycodestyle \n"\
+ " * or `pip install --user pycodestyle`"
+
+
+def system(*args, **kwargs):
+ kwargs.setdefault('stdout', subprocess.PIPE)
+ proc = subprocess.Popen(args, **kwargs)
+ out, err = proc.communicate()
+ if isinstance(out, bytes):
+ out = out.decode()
+ return out
+
+
+def copy_files_to_tmp_dir(files):
+ tempdir = tempfile.mkdtemp()
+ for name in files:
+ filename = os.path.join(tempdir, name)
+ filepath = os.path.dirname(filename)
+ if not os.path.exists(filepath):
+ os.makedirs(filepath)
+ with open(filename, 'w') as f:
+ system('git', 'show', ':' + name, stdout=f)
+
+ return tempdir
+
+
+def main():
+ modified_files = system('git', 'diff-index', '--cached',
+ '--name-only', 'HEAD', '--diff-filter=ACMR').split("\n")[:-1]
+ non_compliant_files = []
+ output_message = None
+
+ for modified_file in modified_files:
+ try:
+ if not modified_file.endswith(".py"):
+ continue
+ pycodestyle_errors = system('pycodestyle', '--repeat', '--ignore', 'E402,E501,E128,W605,W503', modified_file)
+ if pycodestyle_errors:
+ if output_message is None:
+ output_message = NOT_PYCODESTYLE_COMPLIANT_MESSAGE_PRE
+ output_message += pycodestyle_errors
+ non_compliant_files.append(modified_file)
+ except OSError as e:
+ output_message = NO_PYCODESTYLE_MESSAGE
+ break
+
+ if output_message:
+ print(output_message)
+ if non_compliant_files:
+ print(NOT_PYCODESTYLE_COMPLIANT_MESSAGE_POST)
+ for non_compliant_file in non_compliant_files:
+ print("autopep8 -i ", non_compliant_file, "; git add ",
+ non_compliant_file)
+ print("git commit")
+ sys.exit(1)
+
+
+if __name__ == '__main__':
+ main()