diff options
Diffstat (limited to 'scripts/py/setup_hooks.py')
-rw-r--r-- | scripts/py/setup_hooks.py | 109 |
1 files changed, 40 insertions, 69 deletions
diff --git a/scripts/py/setup_hooks.py b/scripts/py/setup_hooks.py index ead5e1f..dc3156c 100644 --- a/scripts/py/setup_hooks.py +++ b/scripts/py/setup_hooks.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 -# SPDX-FileCopyrightText: Copyright 2022 Arm Limited and/or its affiliates <open-source-office@arm.com> -# SPDX-License-Identifier: Apache-2.0 +# SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com> # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,84 +12,56 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -import os -import sys +""" +Adds the git hooks script into the appropriate location +""" import argparse +import os +import shutil import subprocess -import stat +import sys +from pathlib import Path + +HOOKS_SCRIPT = "git_pre_push_hooks.sh" + -def set_hooks_dir(hooks_dir): - command = 'git config core.hooksPath {}'.format(hooks_dir) - subprocess.Popen(command.split(), stdout=subprocess.PIPE) +def set_hooks_dir(hooks_dir: str): + """ + Set the hooks path in the git configuration + @param hooks_dir: The hooks directory + """ + command = f'git config core.hooksPath {hooks_dir}' + with subprocess.Popen(command.split(), stdout=subprocess.PIPE) as process: + process.communicate() + return_code = process.returncode -def add_pre_push_hooks(hooks_dir): + if return_code != 0: + raise RuntimeError(f"Could not configure git hooks path, exited with code {return_code}") + + +def add_pre_push_hooks(hooks_dir: str): + """ + Copies the git hooks scripts into the specified location + @param hooks_dir: The specified git hooks directory + """ pre_push = "pre-push" file_path = os.path.join(hooks_dir, pre_push) file_exists = os.path.exists(file_path) if file_exists: os.remove(file_path) - f = open(file_path, "a") - f.write( -'''#!/bin/sh -# SPDX-FileCopyrightText: Copyright 2022 Arm Limited and/or its affiliates <open-source-office@arm.com> -# SPDX-License-Identifier: Apache-2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# Called by "git push" with no arguments. The hook should -# exit with non-zero status after issuing an appropriate message if -# it wants to stop the push. - -while read local_ref local_sha remote_ref remote_sha -do - # We should pass only added or modified C/C++ source files to cppcheck. - changed_files=$(git diff --name-only HEAD~1 HEAD | grep -iE "\.(c|cpp|cxx|cc|h|hpp|hxx)$" | cut -f 2) - if [ -n "$changed_files" ]; then - clang-format -style=file --dry-run --Werror $changed_files - - exitcode1=$? - if [ $exitcode1 -ne 0 ]; then - echo "Formatting errors found in file: $changed_files. - \nPlease run:\n\ \"clang-format -style=file -i $changed_files\" - \nto correct these errors" - exit $exitcode1 - fi - - cppcheck --enable=performance,portability --error-exitcode=1 --suppress=*:tests* $changed_files - exitcode2=$? - if [ $exitcode2 -ne 0 ]; then - exit $exitcode2 - fi - fi - exit 0 -done -exit 0''' -) + script_path = Path(__file__).resolve().parent / HOOKS_SCRIPT + shutil.copy(script_path, hooks_dir) - f.close() - s = os.stat(file_path) - os.chmod(file_path, s.st_mode | stat.S_IEXEC) -parser = argparse.ArgumentParser() -parser.add_argument("git_hooks_path") -args = parser.parse_args() +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("git_hooks_path") + args = parser.parse_args() -dir_exists = os.path.exists(args.git_hooks_path) -if not dir_exists: - print('Error! The Git hooks directory you supplied does not exist.') - sys.exit() + if not os.path.exists(args.git_hooks_path): + print('Error! The Git hooks directory you supplied does not exist.') + sys.exit() -add_pre_push_hooks(args.git_hooks_path) -set_hooks_dir(args.git_hooks_path) + add_pre_push_hooks(args.git_hooks_path) + set_hooks_dir(args.git_hooks_path) |