| 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 4 | # |
| 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | # |
| 7 | """ Checks that a patch has been signed-off """ |
| 8 | import argparse |
| 9 | import logging |
| 10 | import os |
| 11 | import subprocess |
| 12 | import sys |
| 13 | |
| 14 | logger = logging.getLogger("SignedOff Checker") |
| 15 | |
| 16 | |
| 17 | class SignedOffByChecker: |
| 18 | """ |
| 19 | A class that checks if a patch has been signed-off. |
| 20 | |
| 21 | Parameters |
| 22 | ---------- |
| 23 | dir : 'str' (optional) |
| 24 | the directory to run the checker on and check the latest patch |
| 25 | Default: . |
| 26 | """ |
| 27 | |
| 28 | def __init__(self, dir=".") -> None: |
| 29 | self.dir = os.path.abspath(dir) |
| 30 | logger.debug(f"SignedOffBy checker is set up to run on {self.dir}") |
| 31 | |
| 32 | def run(self) -> None: |
| 33 | """Runs the checker. |
| 34 | |
| 35 | Raises |
| 36 | ------ |
| 37 | ValueError |
| 38 | If the checker fails to execute |
| 39 | """ |
| 40 | retval = 0 |
| 41 | logger.debug(f"Running SignOff checker on '{self.dir}'") |
| 42 | try: |
| 43 | os.chdir(self.dir) |
| 44 | cmd = ["git", "show", "-s", "--format=%B", "HEAD"] |
| 45 | commit_msg = subprocess.check_output(cmd).decode("utf-8") |
| 46 | if "Signed-off-by:" not in commit_msg: |
| 47 | logger.error( |
| 48 | f"Patch not signed-off (Could not find 'Signed-off-by:' in the commit message). Use: git commit -s" |
| 49 | ) |
| 50 | retval = -1 |
| 51 | else: |
| 52 | logger.info("Patch has been signed-off!") |
| 53 | except (subprocess.CalledProcessError, FileNotFoundError) as e: |
| 54 | logger.error(f"SignedOff checker failed with {e}") |
| 55 | retval = -1 |
| 56 | |
| 57 | if retval != 0: |
| 58 | raise ValueError("SignedOffBy checker failed!") |
| 59 | |
| 60 | |
| 61 | def parse_arguments(): |
| 62 | parser = argparse.ArgumentParser() |
| 63 | parser.add_argument( |
| 64 | "--dir", |
| 65 | "-d", |
| 66 | help="Folder to execute the checker in. Default: .", |
| 67 | type=str, |
| 68 | default=".", |
| 69 | ) |
| 70 | parser.add_argument( |
| 71 | "--debug", |
| 72 | "-D", |
| 73 | help="Enable debug information. Default: False", |
| 74 | action="store_true", |
| 75 | default=False, |
| 76 | ) |
| 77 | args = parser.parse_args() |
| 78 | |
| 79 | return args |
| 80 | |
| 81 | |
| 82 | def run_signedoff_checker(args): |
| 83 | logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO) |
| 84 | logger.debug(f"Arguments passed: {str(args.__dict__)}") |
| 85 | |
| 86 | retval = 0 |
| 87 | try: |
| 88 | checker = SignedOffByChecker(args.dir) |
| 89 | checker.run() |
| 90 | except ValueError as e: |
| 91 | logger.error("Exception caught in SignedOffBy checker: %s" % e) |
| 92 | retval = 1 |
| 93 | |
| 94 | return retval |
| 95 | |
| 96 | |
| 97 | if __name__ == "__main__": |
| 98 | sys.exit(run_signedoff_checker(parse_arguments())) |