-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitmirror.py
130 lines (118 loc) · 3.43 KB
/
gitmirror.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import argparse
from github import Github
import os.path
import toml
import subprocess
import sys
MIRROR_BASEDIR = '/srv/sources'
def mirror(configfile, apitoken):
with open(configfile, 'r') as fp:
config = toml.load(fp)
clone_actions = []
update_actions = []
for host in config.values():
hostname = host['hostname']
if hostname == 'github.com':
gh = Github(apitoken)
for repo_owner, repos in host['repos'].items():
if repos == '*':
# Find all public repos via github API
# Note that this can't possibly work for non-github
# repos...but we don't really support them properly
# anyway at the moment.
user = gh.get_user(repo_owner)
repos = [ repo.name for repo in user.get_repos() ]
if isinstance(repos, str):
repos = [ repos ]
for repo_name in repos:
target_dir = os.path.join(
MIRROR_BASEDIR,
hostname,
repo_owner,
repo_name,
)
repo_url = 'https://%s/%s/%s.git' % (
hostname,
repo_owner,
repo_name,
)
if not os.path.exists(target_dir):
# First, ensure all parents exist
os.makedirs(
os.path.dirname(target_dir),
exist_ok=True,
)
# First time clone!
clone_actions.append(
[
target_dir,
repo_url,
]
)
else:
update_actions.append(
[
target_dir,
repo_url,
]
)
clones = []
for target, url in clone_actions:
p = subprocess.Popen(
[
'git',
'clone',
'--shared',
'--bare',
url,
target,
]
)
clones.append(p)
others = []
for target, url in update_actions:
p = subprocess.Popen(
[
'git',
'fetch',
'--tags',
'origin',
'master:master',
],
cwd=target,
)
others.append(p)
for clone in clones:
clone.wait()
for target, url in clone_actions:
p = subprocess.Popen(
[
'git',
'update-server-info',
],
cwd=target,
)
others.append(p)
for other in others:
other.wait()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
"Clone lots of git repositories, for fortly purposes.",
)
parser.add_argument(
'configfile',
metavar='configfile',
type=str,
nargs=1,
help='Configuration file location',
)
parser.add_argument(
'apitoken',
metavar='apitoken',
type=str,
nargs='?',
default=os.environ.get('GITHUB_TOKEN'),
help='Github API token',
)
args = parser.parse_args()
mirror(args.configfile[0], args.apitoken)