-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
78 lines (61 loc) · 1.96 KB
/
__main__.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
from pathlib import Path
from yaml import safe_load
from utils import gitutils as git
from utils.pathutils import get_git_folders, make_dir_tree
from utils.termutils import color_state, print_header
CONFIG_PATH = 'config.yml'
def load_config() -> dict | None:
try:
with open(CONFIG_PATH, 'r') as f:
return safe_load(f.read())
except:
return None
def backup() -> int:
# Load config
print('Loading config...')
conf = load_config()
if ( conf is None ):
print('Could not load config.')
return 1
if ( isinstance(conf['repositories'], type(list[str])) ):
print('Invalid repository list in config.')
return 2
reps = conf['repositories']
target = conf['target']
flatten = conf['flatten']
method = conf['method']
count = len(reps)
states = []
success_count = 0
print(f'Cloning {count} repositories...')
# Start backups
for i, url in enumerate(reps):
# Prepare
folders, repo = get_git_folders(url)
print_header(f'Cloning {i+1}/{count} [{repo}]')
path = Path(target)
if not flatten:
path /= Path('/'.join(folders))
make_dir_tree(path)
# Run
if method == 'clone':
success = git.mirror(path, url)
elif method == 'mirror':
success = git.mirror(path, url)
else:
print('Invalid method defined in configuration.')
return 3
# Save results
states += [ (repo, success) ]
if (success):
success_count += 1
# Display results
for i,repo in enumerate(states):
if repo[1]:
state = color_state(' OK ', ok=True)
else:
state = color_state('FAIL', ok=False)
print(f'[{state}] {repo[0]}')
print(f'Success: {success_count} / {count}')
if __name__ == '__main__':
exit(backup())