forked from ethz-asl/linter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init-git-hooks.py
executable file
·110 lines (82 loc) · 3.61 KB
/
init-git-hooks.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
103
104
105
106
107
108
109
110
#!/usr/bin/env python
# Disable pylint filename and missing module member complaints.
# pylint: disable=C0103,E1101
"""
Initializes git hooks for the parent git repository.
TODO(mfehr): fix this script such that we can use the new cpplint. It currently
results in warnings that are not correct IMO. Also ideally find a way to use
the cpplint file online directly, however that would require a different way
of parsing the linter output.
Set this variable to download the cpplint file instead of using the
local !modified! copy:
cpplint_url = "https://raw.githubusercontent.com/google/styleguide/gh-pages/cpplint/cpplint.py"
Set this variable to use the local modified copy of the newest cpplint script:
default_cpplint = "cpplint.py"
"""
import os
import requests
import shutil
import subprocess
import sys
default_cpplint = "modified_cpplint.py"
cpplint_url = ""
pylint_url = "https://raw.githubusercontent.com/vinitkumar/googlecl/6dc04b489dba709c53d2f4944473709617506589/googlecl-pylint.rc"
clang_format_diff_executable = "clang-format-diff-3.8"
def download_file_from_url(url, file_path):
"""Download a file from a HTTPS URL. Verification is enabled."""
request = requests.get(url, verify=True, stream=True)
request.raw.decode_content = True
with open(file_path, 'w') as downloaded_file:
shutil.copyfileobj(request.raw, downloaded_file)
def get_git_repo_root(some_folder_in_root_repo='./'):
"""Get the root folder of the git repository."""
get_repo_call = subprocess.Popen("git rev-parse --show-toplevel",
shell=True,
cwd=some_folder_in_root_repo,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
stdout, _ = get_repo_call.communicate()
repo_root = stdout.rstrip()
return repo_root
def command_exists(cmd):
"""Check if a bash command exists."""
return subprocess.call("type " + cmd, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0
def main():
""" Download cpplint.py and pylint.py and installs the git hooks"""
script_directory = os.path.dirname(sys.argv[0])
script_directory = os.path.abspath(script_directory)
if cpplint_url != "":
# Download linter files.
download_file_from_url(cpplint_url, script_directory + "/cpplint.py")
if not os.path.isfile(script_directory + "/cpplint.py"):
print("ERROR: Could not download cpplint.py file!")
exit(1)
else:
cp_params = (script_directory + "/default/" + default_cpplint + " " +
script_directory + "/cpplint.py")
if subprocess.call("cp " + cp_params, shell=True) != 0:
print("Failed to copy default cpplint.")
exit(1)
download_file_from_url(pylint_url, script_directory + "/pylint.rc")
if not os.path.isfile(script_directory + "/pylint.rc"):
print("ERROR: Could not download pylint.rc file!")
exit(1)
if not command_exists(clang_format_diff_executable):
print("ERROR: " + clang_format_diff_executable + " is not installed!")
exit(1)
if not command_exists("autopep8"):
print("ERROR: autopep8 is not installed! Try: pip install autopep8")
exit(1)
# Get git root folder of parent repository.
repo_root = get_git_repo_root(script_directory + '/../')
# Copy git hooks.
cp_params = script_directory + "/git-hooks.py " + \
repo_root + "/.git/hooks/pre-commit"
if subprocess.call("cp " + cp_params, shell=True) != 0:
print("Failed to copy githooks to "
"{}...".format((repo_root + "/.git/hooks/")))
exit(1)
print("Success, githooks initialized!")
if __name__ == "__main__":
main()