Skip to content

Commit

Permalink
[qa] Add pre-push hook openwisp#161
Browse files Browse the repository at this point in the history
  • Loading branch information
purhan committed Nov 30, 2020
1 parent 30ad216 commit bd7f4ee
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
78 changes: 78 additions & 0 deletions pre_push_hook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env python

import argparse
import collections
import os
import pprint
import shutil
import subprocess
import sys


def run_checks():
checks = [
'run-qa-checks',
'runtests.py'
]
MAIN_DIR = os.getcwd()
failed_checks = []
for check in checks:
task = subprocess.Popen(
f'{MAIN_DIR}/{check}'
)
task.communicate()
if task.returncode:
failed_checks.append(check)
if len(failed_checks):
print("The following checks failed")
print("---------------------------")
for check in failed_checks:
print(check)
sys.exit(1)


def make_executable(f):
task = subprocess.Popen(
['chmod', '+x', f], stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
res, err = task.communicate()
return res, err


def install_hook():
MAIN_DIR = os.getcwd()
HOOKS_DIR = os.path.join(MAIN_DIR, '.git', 'hooks')
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')

# Makes the hook file executable
res, err = make_executable(hook_file)
if err:
raise ValueError(err)


def main(args=None):
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:
install_hook()
return
run_checks()


if __name__ == '__main__':
main()
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import sys

import pre_push_hook
from openwisp_utils import get_version
from setuptools import find_packages, setup

Expand Down Expand Up @@ -68,3 +69,5 @@
'Programming Language :: Python :: 3',
],
)

pre_push_hook.main(args=['--install'])

0 comments on commit bd7f4ee

Please sign in to comment.