Debian Codegraph @

setup/git_pre_commit_hook.py

raw
1 #!/usr/bin/env python
2
3 import os
4 import runpy
5 import subprocess
6 import sys
7
8 base = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
9 os.chdir(base)
10 setup_py = os.path.realpath('./setup.py')
11
12
13 def testfile(file):
14 def t(f, start, end, exclude_end=None):
15 return f.startswith(start) and f.endswith(end) and not (f.endswith(exclude_end) if exclude_end else False)
16
17 if t(file, ('src/odf', 'src/calibre'), '.py', exclude_end='_ui.py'):
18 return True
19 if t(file, 'recipes', '.recipe'):
20 return True
21 if t(file, 'src/pyj', '.pyj'):
22 return True
23 return False
24
25
26 output = subprocess.check_output((
27 'git',
28 'diff',
29 '--staged',
30 '--name-only',
31 '--no-ext-diff',
32 '-z',
33 # Everything except for D
34 '--diff-filter=ACMRTUXB',
35 )).decode('utf-8')
36
37 output = output.strip('\0')
38 if not output:
39 output = []
40 else:
41 output = output.split('\0')
42
43 filenames = list(filter(testfile, output))
44 if not filenames:
45 sys.exit(0)
46
47 check_args = ['./setup.py', 'check_all', '--no-editor', '--check-only']
48
49 before = sys.argv
50 try:
51 sys.argv = check_args + filenames
52 runpy.run_path('setup.py', run_name='__main__')
53 finally:
54 sys.argv = before