-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.py
executable file
·76 lines (58 loc) · 1.85 KB
/
sync.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
#!/bin/python3
import os
import sys
import re
from typing import Optional, List
class bcolors:
OK = '\033[94m'
ENDC = '\033[0m'
class Module:
def __init__(self, name: str) -> None:
self.name: str = name
self.path: Optional[str] = None
self.url: Optional[str] = None
self.shallow: Optional[str] = None
def __repr__(self) -> str:
return f'Module(name={self.name}, ' + \
f'path={self.path}, ' + \
f'url={self.url}, ' + \
f'shallow={self.shallow})'
def load_config(filename) -> List[Module]:
modules: List[Module] = []
with open(os.path.expandvars(filename)) as f:
if not f.readable:
return []
module: Optional[Module] = None
for line in f.readlines():
if line.startswith('[submodule '):
name = re.findall('\[submodule "(.*)"\]', line)[0]
module = Module(name)
modules.append(module)
else:
item = line.strip().split(' ')
if len(item) != 3:
continue
key = item[0]
val = item[2]
setattr(module, key, val)
return modules
def sync(module: Module, skip: bool = False):
print(f'{bcolors.OK}SYNC: {module.name}{bcolors.ENDC}')
if skip:
print('SKIPPED')
return
if module.shallow:
os.system(
f'git submodule update --recursive --remote --depth 1 {module.name}'
)
else:
os.system(f'git submodule update --recursive --remote {module.name}')
def main():
all = False
if len(sys.argv) > 1 and sys.argv[1] == 'all':
all = True
modules: List[Module] = load_config(f'./.gitmodules')
for module in modules:
sync(module, not all and not module.shallow)
if __name__ == "__main__":
main()