-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathhubmirror.py
313 lines (280 loc) · 11 KB
/
hubmirror.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import argparse
import functools
import json
import re
import shutil
import sys
import yaml
import os
import git
import requests
from tenacity import retry, stop_after_attempt, wait, wait_exponential
class Progress(git.remote.RemoteProgress):
def __init__(self, name):
super(Progress, self).__init__()
self.name = name
def update(self, op_code, cur_count, max_count=None, message=''):
print('Process %s, %s' % (self.name, self._cur_line))
# TODO: move to utils
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
# TODO: move to utils
def cov2sec(s):
_h = {"s": 1, "m": 60, "h": 3600, "d": 86400, "w": 604800}
if _h.get(s[-1]):
return int(s[:-1]) * _h.get(s[-1], 1)
else:
return int(s)
# class for repo git related work
class Mirror(object):
def __init__(self, hub, name, cache='.', timeout='0', force_update=False):
self.hub = hub
self.name = name
self.src_url = hub.src_repo_base + '/' +name + ".git"
self.dst_url = hub.dst_repo_base + '/' +name + ".git"
self.repo_path = cache + '/' + name
if re.match("^\d+[dhms]?$", timeout):
self.timeout = cov2sec(timeout)
else:
self.timeout = 0
self.force_update = force_update
@retry(wait=wait_exponential(), reraise=True, stop=stop_after_attempt(3))
def _clone(self):
# TODO: process empty repo
print("Starting git clone " + self.src_url)
mygit = git.cmd.Git(os.getcwd())
mygit.clone(
git.cmd.Git.polish_url(self.src_url), self.repo_path,
kill_after_timeout=self.timeout
)
print("Clone completed: %s" % os.getcwd() + self.repo_path)
@retry(wait=wait_exponential(), reraise=True, stop=stop_after_attempt(3))
def _update(self, local_repo):
try:
local_repo.git.pull(kill_after_timeout=self.timeout)
except git.exc.GitCommandError:
# Cleanup local repo and re-clone
print('Updating failed, re-clone %s' % self.name)
shutil.rmtree(local_repo.working_dir)
self._clone()
@retry(wait=wait_exponential(), reraise=True, stop=stop_after_attempt(3))
def download(self):
print("(1/3) Downloading...")
try:
local_repo = git.Repo(self.repo_path)
except git.exc.NoSuchPathError:
self._clone()
else:
print("Updating repo...")
self._update(local_repo)
def create(self):
print("(2/3) Creating...")
self.hub.create_dst_repo(self.name)
@retry(wait=wait_exponential(), reraise=True, stop=stop_after_attempt(3))
def push(self, force=False):
local_repo = git.Repo(self.repo_path)
cmd = ['set-head', 'origin', '-d']
local_repo.git.remote(*cmd)
try:
local_repo.create_remote(self.hub.dst_type, self.dst_url)
except git.exc.GitCommandError as e:
print("Remote exsits, re-create: set %s to %s" % (
self.hub.dst_type, self.dst_url))
local_repo.delete_remote(self.hub.dst_type)
local_repo.create_remote(self.hub.dst_type, self.dst_url)
cmd = [self.hub.dst_type, 'refs/remotes/origin/*:refs/heads/*', '--tags', '--prune']
if not self.force_update:
print("(3/3) Pushing...")
local_repo.git.push(*cmd, kill_after_timeout=self.timeout)
else:
print("(3/3) Force pushing...")
cmd = ['-f'] + cmd
local_repo.git.push(*cmd, kill_after_timeout=self.timeout)
# class for hub api related work
class Hub(object):
def __init__(
self, src, dst, dst_token, account_type="user",
clone_style="https"
):
# TODO: check invalid type
self.account_type = account_type
self.src_type, self.src_account = src.split('/')
self.dst_type, self.dst_account = dst.split('/')
self.dst_token = dst_token
self.session = requests.Session()
if self.dst_type == "gitee":
self.dst_base = 'https://gitee.com/api/v5'
elif self.dst_type == "github":
self.dst_base = 'https://api.github.com'
prefix = "https://" if clone_style == 'https' else 'git@'
suffix = "/" if clone_style == 'https' else ':'
if self.src_type == "gitee":
self.src_base = 'https://gitee.com/api/v5'
self.src_repo_base = prefix + 'gitee.com' + suffix
elif self.src_type == "github":
self.src_base = 'https://api.github.com'
self.src_repo_base = prefix + 'github.com' + suffix
self.src_repo_base = self.src_repo_base + self.src_account
# TODO: toekn push support
self.dst_repo_base = "git@" + self.dst_type + ".com:" + self.dst_account
def has_dst_repo(self, repo_name):
url = '/'.join(
[self.dst_base, self.account_type+'s', self.dst_account, 'repos']
)
repo_names = self._get_all_repo_names(url)
if not repo_names:
print("Warning: destination repos is []")
return False
return repo_name in repo_names
def create_dst_repo(self, repo_name):
suffix = 'user/repos'
if self.account_type == "org":
suffix = 'orgs/%s/repos' % self.dst_account
url = '/'.join(
[self.dst_base, suffix]
)
if self.dst_type == 'gitee':
data = {'name': repo_name}
elif self.dst_type == 'github':
data = json.dumps({'name': repo_name})
if not self.has_dst_repo(repo_name):
print(repo_name + " doesn't exist, create it...")
if self.dst_type == "github":
response = self.session.post(
url,
data=data,
headers={'Authorization': 'token ' + self.dst_token}
)
if response.status_code == 201:
print("Destination repo creating accepted.")
else:
print("Destination repo creating failed: " + response.text)
elif self.dst_type == "gitee":
response = requests.post(
url,
headers={'Content-Type': 'application/json;charset=UTF-8'},
params={"name": repo_name, "access_token": self.dst_token}
)
if response.status_code == 201:
print("Destination repo creating accepted.")
else:
print("Destination repo creating failed: " + response.text)
else:
print(repo_name + " repo exist, skip creating...")
def dynamic_list(self):
url = '/'.join(
[self.src_base, self.account_type+'s', self.src_account, 'repos']
)
return self._get_all_repo_names(url)
@functools.lru_cache
def _get_all_repo_names(self, url):
page, total, per_page = 1, 0, 60
api = url + "?page=0&per_page=" + str(per_page)
# TODO: src_token support
response = self.session.get(api)
# TODO: DRY
if response.status_code != 200:
print("Repo getting failed: " + response.text)
return []
items = response.json()
all_items = []
while items:
names = [i['name'] for i in items]
all_items += names
items = None
if 'next' in response.links:
url_next = response.links['next']['url']
response = self.session.get(url_next)
# TODO: DRY
if response.status_code != 200:
print("Repo getting failed: " + response.text)
return []
page += 1
items = response.json()
return all_items
class HubMirror(object):
def __init__(self):
self.parser = self._create_parser()
self.args = self.parser.parse_args()
# Change "a, b" to ['a', 'b']
_cov = lambda x: x.replace(' ', '').split(',') if x else []
self.white_list = _cov(self.args.white_list)
self.black_list = _cov(self.args.black_list)
self.static_list = _cov(self.args.static_list)
def _create_parser(self):
with open('/action.yml', 'r') as f:
action = yaml.safe_load(f)
parser = argparse.ArgumentParser(
description=action['description'])
inputs = action['inputs']
for key in inputs:
if key in ['dst_key']:
continue
input_args = inputs[key]
dft = input_args.get('default', '')
parser.add_argument(
"--" + key.replace('_', '-'),
# Autofill the `type` according `default`, str by default
type=str2bool if isinstance(dft, bool) else str,
required=input_args.get('required', False),
default=dft,
help=input_args.get('description', '')
)
return parser
def test_black_white_list(self, repo):
if repo in self.black_list:
print("Skip, %s in black list: %s" % (repo, self.black_list))
return False
if self.white_list and repo not in self.white_list:
print("Skip, %s not in white list: %s" % (repo, self.white_list))
return False
return True
def run(self):
hub = Hub(
self.args.src,
self.args.dst,
self.args.dst_token,
account_type=self.args.account_type,
clone_style=self.args.clone_style
)
src_type, src_account = self.args.src.split('/')
# Using static list when static_list is set
repos = self.args.static_list
src_repos = repos.split(',') if repos else hub.dynamic_list()
total, success, skip = len(src_repos), 0, 0
failed_list = []
for repo in src_repos:
if self.test_black_white_list(repo):
print("Backup %s" % repo)
try:
mirror = Mirror(
hub, repo,
cache=self.args.cache_path,
timeout=self.args.timeout,
force_update=self.args.force_update,
)
mirror.download()
mirror.create()
mirror.push()
success += 1
except Exception as e:
print(e)
failed_list.append(repo)
else:
skip += 1
failed = total - success - skip
res = (total, skip, success, failed)
print("Total: %s, skip: %s, successed: %s, failed: %s." % res)
print("Failed: %s" % failed_list)
if failed_list:
sys.exit(1)
if __name__ == '__main__':
mirror = HubMirror()
mirror.run()