From bd7f4ee7815016071601e7e95c3abadc608afca5 Mon Sep 17 00:00:00 2001 From: purhan Date: Mon, 30 Nov 2020 09:14:04 +0530 Subject: [PATCH] [qa] Add pre-push hook #161 Closes #161 --- pre_push_hook.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 3 ++ 2 files changed, 81 insertions(+) create mode 100755 pre_push_hook.py diff --git a/pre_push_hook.py b/pre_push_hook.py new file mode 100755 index 00000000..c1062d23 --- /dev/null +++ b/pre_push_hook.py @@ -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() diff --git a/setup.py b/setup.py index 2411771e..52f140e7 100644 --- a/setup.py +++ b/setup.py @@ -2,6 +2,7 @@ import os import sys +import pre_push_hook from openwisp_utils import get_version from setuptools import find_packages, setup @@ -68,3 +69,5 @@ 'Programming Language :: Python :: 3', ], ) + +pre_push_hook.main(args=['--install']) \ No newline at end of file