-
Notifications
You must be signed in to change notification settings - Fork 3
/
pre-commit.py
103 lines (91 loc) · 3.79 KB
/
pre-commit.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import pip
import os
import subprocess
REQUIRED_PACKAGES = ['git-lint', 'requests', 'pylint', 'pep8', 'html-linter', 'closure-linter']
RUNNABLE_CODE_ON_PRE_COMMIT = """#!/usr/bin/env python
import subprocess
import os
try:
import urllib.request as urllib2
except ImportError:
import urllib2
def internet_on():
try:
urllib2.urlopen('https://www.google.co.in', timeout=3)
return True
except urllib2.URLError as err:
return False
def send_data(data):
import requests
try:
r = requests.post("http://<server-ip>/commit/", json=data, timeout=(5, 15))
print(r.status_code, r.reason)
except:
print ('Error sending commit statistics, commit will proceed. Please inform support')
def process_total_changes(total_changes_report):
file_changes_status = []
for line in total_changes_report.splitlines():
report = line.split('\t')
file_changes_status.append({
'lines_added': int(report[0]) if report[0].isdigit() else report[0],
'lines_removed': int(report[1]) if report[1].isdigit() else report[1],
'file_path': report[2]
})
return file_changes_status
def send_code_diff_status():
if os.name == 'nt':
proc = subprocess.Popen(["git", "lint", "-t", "--json"], shell=True, stdout=subprocess.PIPE)
email = subprocess.Popen(["git", "config", "user.email"], shell=True, stdout=subprocess.PIPE)
username = subprocess.Popen(["git", "config", "user.name"], shell=True, stdout=subprocess.PIPE)
total_changes = subprocess.Popen(["git", "diff", "--staged", "--numstat"], shell=True, stdout=subprocess.PIPE)
project = subprocess.Popen(["git", "config", "--get", "remote.origin.url"], shell=True,
stdout=subprocess.PIPE)
else:
proc = subprocess.Popen(["git-lint -t --json"], shell=True, stdout=subprocess.PIPE)
email = subprocess.Popen(["git config user.email"], shell=True, stdout=subprocess.PIPE)
username = subprocess.Popen(["git config user.name"], shell=True, stdout=subprocess.PIPE)
total_changes = subprocess.Popen(["git diff --staged --numstat"], shell=True, stdout=subprocess.PIPE)
project = subprocess.Popen(["basename -s .git `git config --get remote.origin.url`"], shell=True,
stdout=subprocess.PIPE)
total_changes = process_total_changes(total_changes.stdout.read().decode("utf-8").strip())
data = {
'lint_report': proc.stdout.read().decode("utf-8"),
'email': email.stdout.read().decode("utf-8").strip(),
'username': username.stdout.read().decode("utf-8").strip(),
'total_changes': total_changes,
'project': project.stdout.read().decode("utf-8")
}
if internet_on():
send_data(data)
else:
print('Internet Not Working, Please enable')
def main():
send_code_diff_status()
if __name__ == "__main__":
main()
"""
def checks_required_package_installation():
"""Checks packages which are required for working of modules and installs them if missing"""
installed_packages = sorted(["%s==%s" % (i.key, i.version) for i in pip.get_installed_distributions()])
for package in REQUIRED_PACKAGES:
if package not in installed_packages:
pip.main(['install', package])
def install_git_hook():
"""
"""
try:
os.remove('.git/hooks/pre-commit')
except:
print('No existing pre commit hook found')
pass
f = open('.git/hooks/pre-commit', 'w+')
f.write(RUNNABLE_CODE_ON_PRE_COMMIT)
f.close()
subprocess.call(['chmod', '0755', '.git/hooks/pre-commit'])
def main():
"""main function executor, call package installation and git hooks setup methods
"""
checks_required_package_installation()
install_git_hook()
if __name__ == '__main__':
main()