/
post-commit
executable file
·71 lines (59 loc) · 2.98 KB
/
post-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python
from __future__ import print_function
import os
import subprocess
import sys
from automation_tools import get_branchname, run_pylint, get_output, directory
from hooksettings import generates_pylint_report, updates_git_pages
if __name__ == '__main__':
branchname = get_branchname()
if branchname in generates_pylint_report:
run_pylint(['./lint.py', '--score'])
# Only reindent if there are no unstaged files
numChanged = len(get_output(['git', 'diff', '--name-only']).splitlines())
if numChanged == 0:
# Reindent
try:
subprocess.call(['reindent', '-r', '-n', 'pygsti', 'test'])
# Check if anything changed during our reindent
numChanged = len(get_output(['git', 'diff', '--name-only']).splitlines())
if numChanged > 0:
subprocess.call(['git', 'add', '-u'])
subprocess.call(['git', 'commit', '--no-verify', '--amend', '--no-edit'])
print('Reindentation finished, commit amended')
except Exception as e:
print(e)
print('Is reindent installed alongside python?\n' +
'On ubuntu/debian, try: apt-get install python-examples')
else:
print('Some files are still unstaged, automatic reindent (whitespace fix) aborted')
with open(os.devnull, 'w') as FNULL:
with directory('test'):
print('Generating pylint report in background process. Checkout test/output/ in about a minute')
subprocess.Popen(['./lint.py', '--score'], stdout=FNULL, stderr=FNULL) # Generate score, hope it doesn't hang
if branchname in updates_git_pages:
print('Updating github pages!')
subprocess.call(['bash', '.git/hooks/create_git_html'])
subprocess.call(['git', 'checkout', 'gh-pages'])
os.system('cp temp_html/*.html tutorials/')
subprocess.call(['git', 'add', 'tutorials/*.html'])
subprocess.call(['git', 'commit', '--no-verify', '-m', '"Updates html in tutorials"'])
if 'PUSHKEY' in os.environ and 'USER' in os.environ:
USER = os.environ['USER']
PUSHKEY = os.environ['PUSHKEY']
push_uri = 'https://%s:%s@github.com/pyGSTio/pyGSTi.git' % (USER, PUSHKEY) # yikes
try:
print('Pushing. If this fails, there shouldn\'t be any useful output, since it would contain my access token')
with open(os.devnull, 'w') as FNULL:
result = subprocess.call(['git', 'push', '--no-verify', push_uri, 'gh-pages'], stdout=FNULL, stderr=FNULL)
if result != 0:
print('Secure push failed')
else:
print('Secure push worked')
except:
print('Secure push failed')
else:
subprocess.call(['git', 'push']) # Will prompt user
os.system('rm -rf temp_html') # yikes!
subprocess.call(['git', 'checkout', branchname])
sys.exit(0)