forked from openwisp/openwisp-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openwisp-pre-push-hook
executable file
·82 lines (69 loc) · 2.32 KB
/
openwisp-pre-push-hook
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
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
import argparse
import json
import os
import subprocess
import sys
def get_top_level_dir():
top_level_dir = subprocess.Popen(
['git', 'rev-parse', '--show-toplevel'], stdout=subprocess.PIPE
).communicate()[0].decode('utf-8').strip()
return top_level_dir
def make_executable(f):
task = subprocess.Popen(
['chmod', '+x', f], stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
err = task.communicate()[1]
return err
def run_checks():
print("Initializing tests before pushing...")
try:
import openwisp_utils
except:
raise ImportError(
"'openwisp_utils' failed to import. Make sure all dependencies "
"are installed and virtual environment is activated."
)
# Trigger checks and report if they failed
try:
task = subprocess.Popen([f'{REPO_ROOT_DIR}/run-qa-checks', '--pre-push'])
task.communicate()
if task.returncode:
print("---------------------------")
print(
"Some checks failed. Please make sure they pass, "
"or use '--no-verify' to skip them"
)
sys.exit(1)
except FileNotFoundError:
print("Could not access 'run-qa-checks'")
def install_hook():
hook_file = os.path.join(HOOKS_DIR, 'pre-push')
symlink = os.path.islink(hook_file)
if symlink and os.path.exists(hook_file):
print('Symlink already exists')
else:
if symlink:
os.unlink(hook_file)
os.symlink(os.path.abspath(__file__), hook_file)
print("Symlink created, 'pre-push-hook' has been set up!")
# Make the hook file executable
err = make_executable(hook_file)
if err:
raise ValueError(err)
def main(args=None):
global REPO_ROOT_DIR, HOOKS_DIR
REPO_ROOT_DIR = get_top_level_dir()
HOOKS_DIR = os.path.join(REPO_ROOT_DIR, '.git', 'hooks')
parser = argparse.ArgumentParser()
parser.add_argument('remote', nargs='?', help='provided by git before push')
parser.add_argument('url', nargs='?', help='provided by git before push')
parser.add_argument(
'--install', action='store_true', default=False,
)
args = parser.parse_args(args=args)
if args.install:
return install_hook()
run_checks()
if __name__ == '__main__':
main()