Debian Codegraph @

plyer/tools/pep8checker/pre-commit.githook

raw
1 #!/usr/bin/env python
2
3 '''
4 Kivy Git Pre-Commit Hook to Enforce Styleguide
5 ==============================================
6
7 This script is not supposed to be run directly.
8 Instead, copy it to your kivy/.git/hooks/ directory, call it 'pre-commit'
9 and make it executable.
10
11 If you attempt to commit, git will run this script, which in turn will run
12 the styleguide checker over your code and abort the commit if there are any
13 errors. If that happens, please fix & retry.
14
15 To install::
16
17 cp kivy/tools/pep8checker/pre-commit.githook .git/hooks/pre-commit
18 chmod +x .git/hooks/pre-commit
19 '''
20
21 import sys, os
22 from os.path import dirname, abspath, sep, join
23 from subprocess import call, Popen, PIPE
24
25 curdir = dirname(abspath(__file__))
26 kivydir = sep.join(curdir.split(sep)[:-2])
27 srcdir = join(kivydir, 'kivy')
28 script = join(srcdir, 'tools', 'pep8checker', 'pep8kivy.py')
29 try:
30 with open(script): pass
31 except IOError:
32 # if this not the kivy project, find the script file in the kivy project
33 os.environ['KIVY_NO_CONSOLELOG'] = '1'
34 import kivy
35 script = join(dirname(kivy.__file__), 'tools', 'pep8checker', 'pep8kivy.py')
36 srcdir = ''
37
38 # Only check the files that were staged
39 #proc = Popen(['git', 'diff', '--cached', '--name-only', 'HEAD'], stdout=PIPE)
40 #targets = [join(kivydir, target) for target in proc.stdout]
41
42 # Correction: only check the files that were staged, but do not include
43 # deleted files.
44 proc = Popen(['git', 'diff', '--cached', '--name-status', 'HEAD'], stdout=PIPE)
45 proc.wait()
46
47 # This gives output like the following:
48 #
49 # A examples/widgets/lists/list_simple_in_kv.py
50 # A examples/widgets/lists/list_simple_in_kv_2.py
51 # D kivy/uix/observerview.py
52 #
53 # So check for D entries and remove them from targets.
54 #
55 targets = []
56 for target in proc.stdout:
57 parts = [p.strip() for p in target.split()]
58 if parts[0] != 'D':
59 targets.append(join(kivydir, target.decode(encoding='UTF-8')))
60
61 # Untested possibility: After making the changes above for removing deleted
62 # files from targets, saw also where the git diff call could be:
63 #
64 # git diff --cached --name-only --diff-filter=ACM
65 # (leaving off D)
66 #
67 # and we could then remove the special handling in python for targets above.
68
69 call(['git', 'stash', 'save', '--keep-index', '--quiet'])
70 retval = call([sys.executable, script, srcdir] + targets)
71 call(['git', 'stash', 'pop', '--quiet'])
72
73 if retval:
74 # There are styleguide violations
75 print("Error:", retval, "styleguide violation(s) encountered!")
76 print("Your commit has been aborted. Please fix the violations and retry.")
77 sys.exit(retval)
78