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