blob: 2657b6eb0c4e355431c08aba3c3cd14c217f1a9d [file] [log] [blame]
Sean Callananefe5d5f2017-02-23 02:21:34 +00001import json
2import os
3import re
4import shutil
5import subprocess
Lang Hamesa088f2f2017-05-09 20:37:01 +00006import sys
Sean Callananefe5d5f2017-02-23 02:21:34 +00007
8def 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 Callanan56435c92017-03-03 23:13:30 +000015 git_remote_and_branch = subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"], stderr=subprocess.STDOUT).rstrip()
Sean Callananefe5d5f2017-02-23 02:21:34 +000016 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 Callanan5805d9e2017-06-27 20:35:53 +000024def 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 Callananefe5d5f2017-02-23 02:21:34 +000035def 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)