|
| 1 | +""" |
| 2 | +Functions for config downloading and maintaining |
| 3 | +""" |
| 4 | +import json |
| 5 | +from json import dump as jdump |
| 6 | +from pathlib import Path |
| 7 | +from shutil import rmtree |
| 8 | +from tempfile import gettempdir |
| 9 | +from typing import cast, Tuple |
| 10 | + |
| 11 | +from .utils import Request |
| 12 | + |
| 13 | +# note: apply change after rename the config repo |
| 14 | +META_URL = "https://raw.githubusercontent.com/sfu-db/APIConnectors/{}/api-connectors/{}/_meta.json" |
| 15 | +TABLE_URL = "https://raw.githubusercontent.com/sfu-db/APIConnectors/{}/api-connectors/{}/{}.json" |
| 16 | +GIT_REF_URL = "https://api.github.com/repos/sfu-db/APIConnectors/git/refs/heads" |
| 17 | + |
| 18 | + |
| 19 | +def separate_branch(config_path: str) -> Tuple[str, str]: |
| 20 | + """Separate the config path into db name and branch""" |
| 21 | + segments = config_path.split("@") |
| 22 | + if len(segments) == 1: |
| 23 | + return segments[0], "master" |
| 24 | + elif len(segments) == 2: |
| 25 | + return segments[0], segments[1] |
| 26 | + else: |
| 27 | + raise ValueError(f"Multiple branches in the config path {config_path}") |
| 28 | + |
| 29 | + |
| 30 | +def initialize_path(config_path: str, update: bool) -> Path: |
| 31 | + """Determines if the given config_path is local or in GitHub. |
| 32 | + Fetches the full path.""" |
| 33 | + if config_path.startswith(".") or config_path.startswith("/") or config_path.startswith("~"): |
| 34 | + path = Path(config_path).resolve() |
| 35 | + else: |
| 36 | + # From GitHub! |
| 37 | + impdb, branch = separate_branch(config_path) |
| 38 | + ensure_config(impdb, branch, update) |
| 39 | + path = config_directory() / branch / impdb |
| 40 | + return path |
| 41 | + |
| 42 | + |
| 43 | +def config_directory() -> Path: |
| 44 | + """ |
| 45 | + Returns the config directory path |
| 46 | + """ |
| 47 | + tmp = gettempdir() |
| 48 | + return Path(tmp) / "dataprep" / "connector" |
| 49 | + |
| 50 | + |
| 51 | +def ensure_config(impdb: str, branch: str, update: bool) -> bool: |
| 52 | + """Ensure the config for `impdb` is downloaded""" |
| 53 | + path = config_directory() |
| 54 | + |
| 55 | + if (path / branch / impdb / "_meta.json").exists() and not update: |
| 56 | + return True |
| 57 | + |
| 58 | + obsolete = is_obsolete(impdb, branch) |
| 59 | + |
| 60 | + if (path / branch / impdb / "_meta.json").exists() and not obsolete: |
| 61 | + return True |
| 62 | + else: |
| 63 | + download_config(impdb, branch) |
| 64 | + return False |
| 65 | + |
| 66 | + |
| 67 | +def is_obsolete(impdb: str, branch: str) -> bool: |
| 68 | + """Test if the implicit db config files are obsolete and need to be re-downloaded.""" |
| 69 | + |
| 70 | + path = config_directory() |
| 71 | + if not (path / branch / impdb / "_meta.json").exists(): |
| 72 | + return True |
| 73 | + elif not (path / branch / impdb / "_hash").exists(): |
| 74 | + return True |
| 75 | + else: |
| 76 | + with open(path / branch / impdb / "_hash", "r") as f: |
| 77 | + githash = f.read() |
| 78 | + |
| 79 | + sha = get_git_branch_hash(branch) |
| 80 | + |
| 81 | + return githash != sha |
| 82 | + |
| 83 | + |
| 84 | +def get_git_branch_hash(branch: str) -> str: |
| 85 | + """Get current config files repo's hash""" |
| 86 | + requests = Request(GIT_REF_URL) |
| 87 | + response = requests.get() |
| 88 | + refs = json.loads(response.read()) |
| 89 | + |
| 90 | + (sha,) = [ref["object"]["sha"] for ref in refs if ref["ref"] == f"refs/heads/{branch}"] |
| 91 | + return cast(str, sha) |
| 92 | + |
| 93 | + |
| 94 | +def download_config(impdb: str, branch: str) -> None: |
| 95 | + """Download the config from Github into the temp directory.""" |
| 96 | + requests = Request(META_URL.format(branch, impdb)) |
| 97 | + response = requests.get() |
| 98 | + meta = json.loads(response.read()) |
| 99 | + tables = meta["tables"] |
| 100 | + |
| 101 | + sha = get_git_branch_hash(branch) |
| 102 | + # In case we push a new config version to github when the user is downloading |
| 103 | + while True: |
| 104 | + configs = {"_meta": meta} |
| 105 | + for table in tables: |
| 106 | + requests = Request(TABLE_URL.format(branch, impdb, table)) |
| 107 | + response = requests.get() |
| 108 | + config = json.loads(response.read()) |
| 109 | + configs[table] = config |
| 110 | + sha_check = get_git_branch_hash(branch) |
| 111 | + |
| 112 | + if sha_check == sha: |
| 113 | + break |
| 114 | + |
| 115 | + sha = sha_check |
| 116 | + |
| 117 | + path = config_directory() |
| 118 | + |
| 119 | + if (path / branch / impdb).exists(): |
| 120 | + rmtree(path / branch / impdb) |
| 121 | + |
| 122 | + (path / branch / impdb).mkdir(parents=True) |
| 123 | + for fname, val in configs.items(): |
| 124 | + with (path / branch / impdb / f"{fname}.json").open("w") as f: |
| 125 | + jdump(val, f) |
| 126 | + |
| 127 | + with (path / branch / impdb / "_hash").open("w") as f: |
| 128 | + f.write(sha) |
0 commit comments