Move commit gst-indent hook to the root
[platform/upstream/gstreamer.git] / scripts / git-hooks / pre-commit-python.hook
1 #!/usr/bin/env python3
2 import os
3 import subprocess
4 import sys
5 import tempfile
6
7 NOT_PYCODESTYLE_COMPLIANT_MESSAGE_PRE = \
8     "Your code is not fully pycodestyle compliant and contains"\
9     " the following coding style issues:\n\n"
10
11 NOT_PYCODESTYLE_COMPLIANT_MESSAGE_POST = \
12     "Please fix these errors and commit again, you can do so "\
13     "from the root directory automatically like this, assuming the whole "\
14     "file is to be commited:"
15
16 NO_PYCODESTYLE_MESSAGE = \
17     "You should install the pycodestyle style checker to be able"\
18     " to commit in this repo.\nIt allows us to garantee that "\
19     "anything that is commited respects the pycodestyle coding style "\
20     "standard.\nYou can install it:\n"\
21     "  * on ubuntu, debian: $sudo apt-get install pycodestyle \n"\
22     "  * on fedora: #yum install python3-pycodestyle \n"\
23     "  * on arch: #pacman -S python-pycodestyle \n"\
24     "  * or `pip install --user pycodestyle`"
25
26
27 def system(*args, **kwargs):
28     kwargs.setdefault('stdout', subprocess.PIPE)
29     proc = subprocess.Popen(args, **kwargs)
30     out, err = proc.communicate()
31     if isinstance(out, bytes):
32         out = out.decode()
33     return out
34
35
36 def copy_files_to_tmp_dir(files):
37     tempdir = tempfile.mkdtemp()
38     for name in files:
39         filename = os.path.join(tempdir, name)
40         filepath = os.path.dirname(filename)
41         if not os.path.exists(filepath):
42             os.makedirs(filepath)
43         with open(filename, 'w') as f:
44             system('git', 'show', ':' + name, stdout=f)
45
46     return tempdir
47
48
49 def main():
50     modified_files = system('git', 'diff-index', '--cached',
51                             '--name-only', 'HEAD', '--diff-filter=ACMR').split("\n")[:-1]
52     non_compliant_files = []
53     output_message = None
54
55     for modified_file in modified_files:
56         try:
57             if not modified_file.endswith(".py"):
58                 continue
59             pycodestyle_errors = system('pycodestyle', '--repeat', '--ignore', 'E402,E501,E128,W605,W503', modified_file)
60             if pycodestyle_errors:
61                 if output_message is None:
62                     output_message = NOT_PYCODESTYLE_COMPLIANT_MESSAGE_PRE
63                 output_message += pycodestyle_errors
64                 non_compliant_files.append(modified_file)
65         except OSError as e:
66             output_message = NO_PYCODESTYLE_MESSAGE
67             break
68
69     if output_message:
70         print(output_message)
71         if non_compliant_files:
72             print(NOT_PYCODESTYLE_COMPLIANT_MESSAGE_POST)
73             for non_compliant_file in non_compliant_files:
74                 print("autopep8 -i ", non_compliant_file, "; git add ",
75                       non_compliant_file)
76             print("git commit")
77         sys.exit(1)
78
79
80 if __name__ == '__main__':
81     main()