|
| 1 | +import os |
| 2 | +import yaml |
| 3 | +from pathlib import Path |
| 4 | +from typing import Tuple, Optional |
| 5 | +import git |
| 6 | +from git import RemoteProgress |
| 7 | + |
| 8 | +# TODO add env var to set this |
| 9 | +REMOTES_DIR = os.path.join(os.path.dirname(__file__), "remotes") |
| 10 | +REMOTE_CONFIG = os.path.join(os.path.dirname(__file__), "modules_remote.yml") |
| 11 | + |
| 12 | + |
| 13 | +class CloneProgress(RemoteProgress): |
| 14 | + def update(self, op_code, cur_count, max_count=None, message=''): |
| 15 | + if message: |
| 16 | + print(message) |
| 17 | + |
| 18 | + |
| 19 | +def ensure_dir(d, chmod=0o777): |
| 20 | + """ |
| 21 | + Ensures a folder exists. |
| 22 | + Returns True if the folder already exists |
| 23 | + """ |
| 24 | + if not os.path.exists(d): |
| 25 | + os.makedirs(d, chmod) |
| 26 | + os.chmod(d, chmod) |
| 27 | + return False |
| 28 | + return True |
| 29 | + |
| 30 | + |
| 31 | +def read_remotes(): |
| 32 | + if not os.path.isfile(REMOTE_CONFIG): |
| 33 | + raise Exception(f"Error: Remotes config file not found: {REMOTE_CONFIG}") |
| 34 | + with open(REMOTE_CONFIG,'r') as f: |
| 35 | + output = yaml.safe_load(f) |
| 36 | + return output |
| 37 | + |
| 38 | +def get_remote_module(module: str) -> Tuple[bool, Optional[str]]: |
| 39 | + """ Gets the remote module and saves it to cache. Returns True if found, else false""" |
| 40 | + print(f"INFO: Module {module}, looking for remote module and downloading") |
| 41 | + modules_remotes = read_remotes() |
| 42 | + print(modules_remotes.keys()) |
| 43 | + |
| 44 | + if "modules" not in modules_remotes.keys() and module not in modules_remotes["modules"].keys(): |
| 45 | + return False, None |
| 46 | + |
| 47 | + ensure_dir(REMOTES_DIR) |
| 48 | + |
| 49 | + if "remotes" not in modules_remotes.keys() and module not in modules_remotes["modules"].keys(): |
| 50 | + return False, None |
| 51 | + |
| 52 | + module_config = modules_remotes["modules"][module] |
| 53 | + |
| 54 | + remote_for_module = module_config["remote"] |
| 55 | + remote_config = modules_remotes["remotes"][remote_for_module] |
| 56 | + |
| 57 | + if remote_config.get("type", "git") == "git": |
| 58 | + if "repo" not in remote_config.keys(): |
| 59 | + print(f"Error: repo field not set for remote: {remote_for_module} used by remote module {module}") |
| 60 | + return False, None |
| 61 | + |
| 62 | + if "tag" not in remote_config.keys(): |
| 63 | + print(f"Error: repo tag field not set for remote: {remote_for_module} used by remote module {module}") |
| 64 | + return False, None |
| 65 | + |
| 66 | + repo_url = remote_config["repo"] |
| 67 | + branch = remote_config["tag"] |
| 68 | + |
| 69 | + # credentials = base64.b64encode(f"{GHE_TOKEN}:".encode("latin-1")).decode("latin-1") |
| 70 | + # TODO: Handle update of remote |
| 71 | + remote_to_path = os.path.join(REMOTES_DIR, remote_for_module) |
| 72 | + if not os.path.exists(remote_to_path): |
| 73 | + git.Repo.clone_from( |
| 74 | + url=repo_url, |
| 75 | + single_branch=True, |
| 76 | + depth=1, |
| 77 | + to_path=f"{remote_to_path}", |
| 78 | + branch=branch, |
| 79 | + ) |
| 80 | + |
| 81 | + if "path" not in module_config.keys(): |
| 82 | + print(f"Error: repo tag field not set for remote: {remote_for_module} used by remote module {module}") |
| 83 | + return False, None |
| 84 | + module_path = os.path.join(remote_to_path, module_config["path"]) |
| 85 | + return True, module_path |
| 86 | + |
| 87 | + else: |
| 88 | + print(f"Error: unsupported type {modules_remotes[module]['type']} for module {module}") |
| 89 | + return False, None |
| 90 | + return False, None |
0 commit comments