Yigit Boyar | 7dcad2b | 2022-02-04 16:49:03 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
| 2 | import sys |
| 3 | import getopt |
| 4 | import json |
| 5 | import os |
| 6 | |
| 7 | |
| 8 | def usage(exitCode): |
| 9 | print(""" |
| 10 | Usage: |
| 11 | ./should_run_project.py --project <project-name> --branch <current branch name>\n\ |
| 12 | project-name: the name of the project from the workflow file\n\ |
| 13 | branch: the target branch on the build\n\ |
| 14 | Branches whose name starts with 'compose' use the compose configuration whereas\n\ |
| 15 | everything else uses the main configuration\n\ |
| 16 | See ci-config.json for the configuration. |
| 17 | """) |
| 18 | sys.exit(exitCode) |
| 19 | |
| 20 | # finds the configuration we should use based on the branch |
| 21 | |
| 22 | |
| 23 | def getCurrentConfig(branch: str): |
| 24 | configFilePath = os.path.join(os.path.dirname( |
| 25 | os.path.realpath(__file__)), "ci-config.json") |
| 26 | configFile = open(configFilePath) |
| 27 | ciConfig = json.load(configFile) |
| 28 | configFile.close() |
| 29 | if "compose-compiler" in branch: |
| 30 | return ciConfig["compose"] |
| 31 | else: |
| 32 | return ciConfig["main"] |
| 33 | |
| 34 | |
| 35 | def main(argv): |
| 36 | projectName = '' |
| 37 | branch = '' |
| 38 | try: |
| 39 | opts, args = getopt.getopt(argv, "h", ["project=", "branch="]) |
| 40 | except getopt.GetoptError as err: |
| 41 | print(err) |
| 42 | usage(2) |
| 43 | for opt, arg in opts: |
| 44 | if opt == '-h': |
| 45 | usage(0) |
| 46 | elif opt in ("--project"): |
| 47 | projectName = arg |
| 48 | elif opt in ("--branch"): |
| 49 | branch = arg |
| 50 | else: |
| 51 | print("invalid argument ", opt, "-", arg) |
| 52 | usage(2) |
| 53 | |
| 54 | currentConfig = getCurrentConfig(branch) |
| 55 | result = currentConfig["default"] |
| 56 | if "include" in currentConfig: |
| 57 | result = projectName in currentConfig["include"] |
| 58 | # run exclude after include to give it priority |
| 59 | if "exclude" in currentConfig: |
| 60 | result = not (projectName in currentConfig["exclude"]) |
| 61 | print(str(result).lower()) |
| 62 | |
| 63 | |
| 64 | if __name__ == "__main__": |
| 65 | main(sys.argv[1:]) |