-
Notifications
You must be signed in to change notification settings - Fork 16
/
git_hooks.py
executable file
·56 lines (40 loc) · 1.56 KB
/
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
#!/usr/bin/env python
from __future__ import print_function
import os
import subprocess
import sys
def run_command_in_folder(command, folder):
"""Run a bash command in a specific folder."""
run_command = subprocess.Popen(command,
shell=True,
cwd=folder,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
universal_newlines=True)
stdout, _ = run_command.communicate()
command_output = stdout.rstrip()
return command_output
def get_git_repo_root(some_folder_in_root_repo='./'):
"""Get the root folder of the current git repository."""
return run_command_in_folder('git rev-parse --show-toplevel',
some_folder_in_root_repo)
def get_linter_folder(root_repo_folder):
"""Find the folder where this linter is stored."""
try:
return os.environ['LINTER_PATH']
except KeyError:
print("Cannot find linter because the environment variable "
"LINTER_PATH doesn't exist.")
sys.exit(1)
def main():
# Get git root folder.
repo_root = get_git_repo_root()
# Get linter subfolder
linter_folder = get_linter_folder(repo_root)
# Append linter folder to the path so that we can import the linter module.
linter_folder = os.path.join(repo_root, linter_folder)
sys.path.append(linter_folder)
import linter
linter.linter_check(repo_root, linter_folder)
if __name__ == "__main__":
main()