-
Notifications
You must be signed in to change notification settings - Fork 6
/
sync-with-gerrit.py
executable file
·110 lines (83 loc) · 2.7 KB
/
sync-with-gerrit.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python
import logging
import os.path
import subprocess
import json
import shutil
# Configuration for this script
basepath = "mediawiki/skins/"
gerrit_conf = {
'host': 'gerrit.wikimedia.org',
'port': '29418',
'url': 'https://{host}/r/{project}'
}
# Global logger
logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger()
def main():
log.info("Fetching projects from gerrit (prefix: %s)" % basepath)
projects = gerrit('ls-projects', ['-p', basepath, '-d', '--format', 'json'])
# strip out subprojects in extensions
projects = json.loads(projects)
log.info("Checking modules")
gitmodules = []
for p in sorted(projects.keys()):
basename = project_basename(p)
if '/' in basename:
continue
if projects[p].get('state') == 'READ_ONLY':
log.info(" ".join([p, 'skipping, repo is readonly']))
if os.path.isdir(basename):
log.info("Removing directory for %s" % p)
shutil.rmtree(basename)
continue
gitmodules.append(p)
if not os.path.isdir(basename):
log.info("Adding submodule for %s" % p)
try:
submodule_add(p)
except subprocess.CalledProcessError:
log.error("Git reported an issue adding module %s" % p)
log.info("Rewriting .gitmodules")
f = open('.gitmodules', 'w')
f.write(generate_gitmodules(gitmodules))
f.close()
log.info("Review change and submit!\nDone")
def submodule_add(project):
cmd = 'git submodule add'.split(' ') + [
gerrit_url(project),
project_basename(project)
]
subprocess.check_call(cmd)
def generate_gitmodules(projects):
"Create a .gitmodules file with branch=."
gitmodules = ''
for project in projects:
p_url = gerrit_url(project)
gitmodules += (
"[submodule \"{name}\"]\n"
"\tpath = {name}\n"
"\turl = {url}\n"
"\tbranch = .\n"
).format(
name=project_basename(project),
url=p_url
)
return gitmodules
def gerrit(gerrit_cmd, args=[], gerrit_conf=gerrit_conf):
"Helper to execute a gerrit command."
ssh = [
'/usr/bin/ssh',
'-p', gerrit_conf['port'],
gerrit_conf['host'],
]
cmd = ssh + ['gerrit {gerrit_cmd} {args}\''.format(
ssh=ssh, gerrit_cmd=gerrit_cmd, args=' '.join(args))]
return subprocess.check_output(cmd)
def gerrit_url(project):
return gerrit_conf['url'].format(
host=gerrit_conf['host'], project=project)
def project_basename(project):
return project[len(basepath):]
if __name__ == '__main__':
main()