Sean Callanan | efe5d5f | 2017-02-23 02:21:34 +0000 | [diff] [blame] | 1 | import json |
| 2 | import os |
| 3 | import re |
| 4 | import shutil |
| 5 | import subprocess |
Lang Hames | a088f2f | 2017-05-09 20:37:01 +0000 | [diff] [blame] | 6 | import sys |
Sean Callanan | efe5d5f | 2017-02-23 02:21:34 +0000 | [diff] [blame] | 7 | |
| 8 | def identifier(): |
| 9 | try: |
| 10 | svn_output = subprocess.check_output(["svn", "info", "--show-item", "url"], stderr=subprocess.STDOUT).rstrip() |
| 11 | return svn_output |
| 12 | except: |
| 13 | pass |
| 14 | try: |
Sean Callanan | 56435c9 | 2017-03-03 23:13:30 +0000 | [diff] [blame] | 15 | git_remote_and_branch = subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"], stderr=subprocess.STDOUT).rstrip() |
Sean Callanan | efe5d5f | 2017-02-23 02:21:34 +0000 | [diff] [blame] | 16 | git_remote = git_remote_and_branch.split("/")[0] |
| 17 | git_branch = "/".join(git_remote_and_branch.split("/")[1:]) |
| 18 | git_url = subprocess.check_output(["git", "remote", "get-url", git_remote]).rstrip() |
| 19 | return git_url + ":" + git_branch |
| 20 | except: |
| 21 | pass |
| 22 | return None |
| 23 | |
Sean Callanan | 5805d9e | 2017-06-27 20:35:53 +0000 | [diff] [blame] | 24 | def get_override(): |
| 25 | dir = os.path.dirname(os.path.realpath(__file__)) |
| 26 | repos_dir = os.path.join(dir, "repos") |
| 27 | json_regex = re.compile(r"^.*.json$") |
| 28 | override_path = os.path.join(repos_dir, "OVERRIDE") |
| 29 | if os.path.isfile(override_path): |
| 30 | override_set = json.load(open(override_path)) |
| 31 | return override_set["repos"] |
| 32 | else: |
| 33 | return None |
| 34 | |
Sean Callanan | efe5d5f | 2017-02-23 02:21:34 +0000 | [diff] [blame] | 35 | def find(identifier): |
| 36 | dir = os.path.dirname(os.path.realpath(__file__)) |
| 37 | repos_dir = os.path.join(dir, "repos") |
| 38 | json_regex = re.compile(r"^.*.json$") |
| 39 | override_path = os.path.join(repos_dir, "OVERRIDE") |
| 40 | if os.path.isfile(override_path): |
| 41 | override_set = json.load(open(override_path)) |
| 42 | return override_set["repos"] |
| 43 | fallback_path = os.path.join(repos_dir, "FALLBACK") |
| 44 | for path in [os.path.join(repos_dir, f) for f in filter(json_regex.match, os.listdir(repos_dir))]: |
| 45 | fd = open(path) |
| 46 | set = json.load(fd) |
| 47 | fd.close() |
| 48 | if any(re.match(set_regex, identifier) for set_regex in set["regexs"]): |
| 49 | shutil.copyfile(path, fallback_path) |
| 50 | return set["repos"] |
| 51 | if os.path.isfile(fallback_path): |
| 52 | fallback_set = json.load(open(fallback_path)) |
| 53 | return fallback_set["repos"] |
| 54 | sys.exit("Couldn't find a branch configuration for " + identifier + " and there was no " + fallback_path) |