forked from alibaba/EasyRec
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_lfs.py
More file actions
534 lines (490 loc) · 17.5 KB
/
git_lfs.py
File metadata and controls
534 lines (490 loc) · 17.5 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# -*- encoding:utf-8 -*-
# two config files are used: .git_bin_path .git_bin_url
import hashlib
import json
import logging
import os
import re
import subprocess
import sys
import traceback
blank_split = re.compile('[\t ]')
logging.basicConfig(
format='[%(levelname)s] %(asctime)s %(filename)s[%(lineno)d] : %(message)s',
level=logging.INFO)
try:
import oss2
except ImportError:
logging.error(
'please install python_oss from https://github.com/aliyun/aliyun-oss-python-sdk.git'
)
sys.exit(1)
git_bin_path = '.git_bin_path'
git_bin_url_path = '.git_bin_url'
# temporary storage path
git_oss_cache_dir = '.git_oss_cache'
# get project name by using git remote -v
def get_proj_name():
proj_name = subprocess.check_output(['git', 'remote', '-v'])
proj_name = proj_name.decode('utf-8')
proj_name = proj_name.split('\n')[0]
proj_name = blank_split.split(proj_name)[1]
proj_name = proj_name.split('/')[-1]
proj_name = proj_name.replace('.git', '')
return proj_name
# load .git_bin_url
# local_path md5 remote_path
def load_git_url():
git_bin_url_map = {}
try:
with open(git_bin_url_path) as fin:
for line_str in fin:
line_str = line_str.strip()
line_json = json.loads(line_str)
git_bin_url_map[line_json['leaf_path']] = (line_json['sig'],
line_json['remote_path'])
except Exception as ex:
logging.warning('exception: %s' % str(ex))
pass
return git_bin_url_map
def save_git_url(git_bin_url_map):
with open(git_bin_url_path, 'w') as fout:
keys = list(git_bin_url_map.keys())
keys.sort()
for key in keys:
val = git_bin_url_map[key]
tmp_str = '{"leaf_path": "%s", "sig": "%s", "remote_path": "%s"}' % (
key, val[0], val[1])
fout.write('%s\n' % tmp_str)
def path2name(path):
name = path.replace('//', '/')
name = path.replace('/', '_')
if name[-1] == '_':
return name[:-1]
elif name == '.':
return 'curr_dir'
else:
return name
def get_file_arr(path):
archive_files = []
if os.path.isdir(path):
for one_file in os.listdir(path):
one_path = path + '/' + one_file
if not os.path.isdir(one_path):
archive_files.append(one_path)
return archive_files
else: # just a file
archive_files.append(path)
return archive_files
def load_git_bin():
file_arr = {}
if not os.path.exists(git_bin_path):
return file_arr
with open(git_bin_path, 'r') as fin:
for line_str in fin:
line_str = line_str.strip()
try:
line_json = json.loads(line_str)
file_arr[line_json['leaf_name']] = line_json['leaf_file']
except Exception as ex:
logging.warning('%s is corrupted : %s' %
(git_bin_path, traceback.format_exc(ex)))
return file_arr
def save_git_bin(git_arr):
leaf_paths = list(git_arr.keys())
leaf_paths.sort()
with open(git_bin_path, 'w') as fout:
for leaf_path in leaf_paths:
leaf_files = git_arr[leaf_path]
leaf_files.sort()
# make sure that leaf_name is in front of leaf_file
tmp_str = '{"leaf_name": "%s", "leaf_file": %s}' % (
leaf_path, json.dumps(leaf_files))
fout.write('%s\n' % tmp_str)
def recheck_git_bin():
file_arr = load_git_bin()
update = False
del_arr = []
for leaf_path in file_arr:
leaf_files = file_arr[leaf_path]
good_leaf_files = [x for x in leaf_files if os.path.exists(x)]
if not os.path.exists(leaf_path):
del_arr.append(leaf_path)
update = True
elif len(good_leaf_files) != len(leaf_files):
file_arr[leaf_path] = good_leaf_files
update = True
for leaf_path in del_arr:
del file_arr[leaf_path]
if update:
save_git_bin(file_arr)
return file_arr
# check whether a folder changes by check md5 of the tar file of the folder
# note -z option is not used, because the file has random effects
# the md5files are saved in .git_bin_url
def get_local_sig(leaf_files):
if len(leaf_files) == 0:
logging.warning('no leaf files')
return None
leaf_files = sorted(leaf_files)
m = hashlib.md5()
block_size = 1024 * 1024 * 8
for one_file in leaf_files:
with open(one_file, 'rb') as fin:
for chunk in iter(lambda: fin.read(block_size), b''):
m.update(chunk)
return m.hexdigest()
def list_leafs(curr_path):
bottom_dir = []
if os.path.isdir(curr_path):
for root, dirs, files in os.walk(curr_path, topdown=True):
if len(dirs) == 0 or len(files) > 0:
if root[-1] == '/':
root = root[:-1]
file_arr = get_file_arr(root)
bottom_dir.append((root, file_arr))
else: # a single file
curr_dir = os.path.dirname(curr_path)
if curr_dir == '':
curr_dir = '.'
bottom_dir.append((curr_dir, [curr_path]))
return bottom_dir
# check whether lst0 and lst1 contain the same string elements
def lst_eq(lst0, lst1):
if len(lst0) != len(lst1):
return False
for x in lst1:
if x not in lst0:
return False
return True
def merge_lst(lst0, lst1):
for a in lst1:
if a not in lst0:
lst0.append(a)
return lst0
def has_conflict(leaf_path, leaf_files):
if not os.path.exists(leaf_path):
return False
for leaf_file in leaf_files:
if os.path.exists(leaf_file):
return True
return False
def get_yes_no(msg):
while True:
logging.info(msg)
tmp_op = sys.stdin.readline()
tmp_op = tmp_op.strip()
if len(tmp_op) == 0:
break
elif tmp_op[0] == 'Y' or tmp_op[0] == 'y':
update = True
break
elif tmp_op[0] == 'N' or tmp_op[0] == 'n':
update = False
break
return update
if __name__ == '__main__':
if len(sys.argv) < 2:
logging.error(
'usage: python git_lfs.py [pull] [push] [add filename] [resolve_conflict]'
)
sys.exit(1)
home_directory = os.path.expanduser('~')
with open('.git_oss_config_pub', 'r') as fin:
git_oss_data_dir = None
host = None
bucket_name = None
git_oss_private_path = None
enable_accelerate = 0
accl_endpoint = None
for line_str in fin:
line_str = line_str.strip()
if len(line_str) == 0:
continue
if line_str.startswith('#'):
continue
line_str = line_str.replace('~/', home_directory + '/')
line_str = line_str.replace('${TMPDIR}/',
os.environ.get('TMPDIR', '/tmp/'))
line_str = line_str.replace('${PROJECT_NAME}', get_proj_name())
line_tok = [x.strip() for x in line_str.split('=') if x != '']
if line_tok[0] == 'host':
host = line_tok[1]
elif line_tok[0] == 'git_oss_data_dir':
git_oss_data_dir = line_tok[1].strip('/')
elif line_tok[0] == 'bucket_name':
bucket_name = line_tok[1]
elif line_tok[0] == 'git_oss_private_config':
git_oss_private_path = line_tok[1]
if git_oss_private_path.startswith('~/'):
git_oss_private_path = os.path.join(home_directory,
git_oss_private_path[2:])
elif line_tok[0] == 'git_oss_cache_dir':
git_oss_cache_dir = line_tok[1]
elif line_tok[0] == 'accl_endpoint':
accl_endpoint = line_tok[1]
logging.info('git_oss_data_dir=%s, host=%s, bucket_name=%s' %
(git_oss_data_dir, host, bucket_name))
logging.info('git_oss_cache_dir: %s' % git_oss_cache_dir)
if not os.path.exists(git_oss_cache_dir):
os.makedirs(git_oss_cache_dir)
logging.info('git_oss_private_config=%s' % git_oss_private_path)
if git_oss_private_path is not None and os.path.exists(git_oss_private_path):
# load oss configs
with open(git_oss_private_path, 'r') as fin:
for line_str in fin:
line_str = line_str.strip()
line_tok = [x.strip() for x in line_str.split('=') if x != '']
if line_tok[0] in ['accessid', 'accessKeyID']:
accessid = line_tok[1]
elif line_tok[0] in ['accesskey', 'accessKeySecret']:
accesskey = line_tok[1]
oss_auth = oss2.Auth(accessid, accesskey)
oss_bucket = oss2.Bucket(oss_auth, host, bucket_name)
else:
logging.info('git_oss_private_path[%s] is not found, read-only mode' %
git_oss_private_path)
# pull only mode
oss_auth = None
oss_bucket = None
if sys.argv[1] == 'push':
updated = False
git_bin_arr = recheck_git_bin()
git_bin_url = load_git_url()
for leaf_path in git_bin_arr:
leaf_files = git_bin_arr[leaf_path]
# empty directory will not be push to oss
if len(leaf_files) == 0:
continue
file_name = path2name(leaf_path)
new_sig = get_local_sig(leaf_files)
if new_sig is None:
continue
if leaf_path in git_bin_url and git_bin_url[leaf_path][0] == new_sig:
continue
# build tar file and push to oss
file_name_with_sig = file_name + '_' + new_sig
tar_out_path = '%s/%s.tar.gz' % (git_oss_cache_dir, file_name_with_sig)
subprocess.check_output(['tar', '-czf', tar_out_path] + leaf_files)
save_path = '%s/%s' % (git_oss_data_dir, file_name_with_sig)
oss_bucket.put_object_from_file(save_path, tar_out_path)
oss_bucket.put_object_acl(save_path, oss2.OBJECT_ACL_PUBLIC_READ)
git_bin_url[leaf_path] = (new_sig, save_path)
logging.info('pushed %s' % leaf_path)
updated = True
for leaf_path in list(git_bin_url.keys()):
if leaf_path not in git_bin_arr:
del git_bin_url[leaf_path]
logging.info('dropped %s' % leaf_path)
updated = True
if updated:
save_git_url(git_bin_url)
logging.info('push succeed.')
else:
logging.warning('nothing to push')
subprocess.check_output(['git', 'add', git_bin_url_path])
elif sys.argv[1] == 'pull':
# pull images from remote
any_update = False
git_bin_arr = load_git_bin()
git_bin_url = load_git_url()
for leaf_path in git_bin_arr:
leaf_files = git_bin_arr[leaf_path]
if len(leaf_files) == 0:
if os.path.isfile(leaf_path):
logging.error('conflicts: %s is a file, but was a dir' % leaf_path)
elif not os.path.isdir(leaf_path):
os.makedirs(leaf_path)
continue
# newly add files
if leaf_path not in git_bin_url:
continue
file_name = path2name(leaf_path)
all_file_exist = True
for tmp in leaf_files:
if not os.path.exists(tmp):
all_file_exist = False
remote_sig = git_bin_url[leaf_path][0]
if all_file_exist:
local_sig = get_local_sig(leaf_files)
if local_sig == remote_sig:
continue
else:
local_sig = ''
update = False
if len(sys.argv) > 2 and (sys.argv[2] == '-f' or
sys.argv[2] == '--force'):
update = True
else:
if has_conflict(leaf_path, leaf_files):
update = get_yes_no(
'update %s using remote file[remote_sig=%s local_sig=%s]?[N/Y]' %
(leaf_path, remote_sig, local_sig))
else:
update = True
if not update:
continue
# pull from remote oss
remote_path = git_bin_url[leaf_path][1]
_, file_name_with_sig = os.path.split(remote_path)
tar_tmp_path = '%s/%s.tar.gz' % (git_oss_cache_dir, file_name_with_sig)
max_retry = 5
while max_retry > 0:
try:
if not os.path.exists(tar_tmp_path):
in_cache = False
if oss_bucket:
oss_bucket.get_object_to_file(remote_path, tar_tmp_path)
else:
url = 'http://%s.%s/%s' % (bucket_name, host, remote_path)
# subprocess.check_output(['wget', url, '-O', tar_tmp_path])
if sys.platform.startswith('linux'):
subprocess.check_output(['wget', url, '-O', tar_tmp_path])
elif sys.platform.startswith('darwin'):
subprocess.check_output(['curl', url, '--output', tar_tmp_path])
elif sys.platform.startswith('win'):
subprocess.check_output(['curl', url, '--output', tar_tmp_path])
else:
in_cache = True
logging.info('%s is in cache' % file_name_with_sig)
subprocess.check_output(['tar', '-zxf', tar_tmp_path])
local_sig = get_local_sig(leaf_files)
if local_sig == remote_sig:
break
if in_cache:
logging.warning('cache invalid, will download from remote')
os.remove(tar_tmp_path)
continue
logging.warning('download failed, local_sig(%s) != remote_sig(%s)' %
(local_sig, remote_sig))
except subprocess.CalledProcessError as ex:
logging.error('exception: %s' % str(ex))
except oss2.exceptions.RequestError as ex:
logging.error('exception: %s' % str(ex))
os.remove(tar_tmp_path)
if accl_endpoint is not None and host != accl_endpoint:
logging.info('will try accelerate endpoint: %s' % accl_endpoint)
host = accl_endpoint
if oss_auth:
oss_bucket = oss2.Bucket(oss_auth, host, bucket_name)
max_retry -= 1
logging.info('%s updated' % leaf_path)
any_update = True
if not any_update:
logging.info('nothing to be updated')
elif sys.argv[1] == 'add':
add_path = sys.argv[2]
if not os.path.exists(add_path):
raise ValueError('add path %s does not exist' % add_path)
bin_file_map = {}
try:
bin_file_map = load_git_bin()
except Exception as ex:
logging.warning('load_git_bin exception: %s' % traceback.format_exc(ex))
pass
leaf_dirs = list_leafs(add_path)
any_new = False
for leaf_path, leaf_files in leaf_dirs:
for leaf_file in leaf_files:
tmp_out = subprocess.check_output(['git', 'ls-files', leaf_file])
if len(tmp_out.strip()) > 0:
subprocess.check_output(['git', 'rm', '--cached', leaf_file])
if leaf_path not in bin_file_map:
bin_file_map[leaf_path] = leaf_files
any_new = True
else: # check whether the files are the same
old_leaf_files = bin_file_map[leaf_path]
if not lst_eq(old_leaf_files, leaf_files):
bin_file_map[leaf_path] = merge_lst(old_leaf_files, leaf_files)
any_new = True
if any_new:
# write back to .git_bin_path
save_git_bin(bin_file_map)
logging.info('added %s' % add_path)
else:
logging.info('already add %s' % add_path)
subprocess.check_output(['git', 'add', '.git_bin_path'])
elif sys.argv[1] == 'remove':
del_path = sys.argv[2]
try:
bin_file_map = load_git_bin()
except Exception as ex:
logging.warning('load_git_bin exception: %s' % traceback.format_exc(ex))
pass
leaf_dirs = list_leafs(del_path)
any_update = False
for leaf_path, leaf_files in leaf_dirs:
if leaf_path in bin_file_map:
for leaf_file in leaf_files:
if leaf_file in bin_file_map[leaf_path]:
tmp_id = bin_file_map[leaf_path].index(leaf_file)
del bin_file_map[leaf_path][tmp_id]
any_update = True
if len(bin_file_map[leaf_path]) == 0:
del bin_file_map[leaf_path]
if any_update:
save_git_bin(bin_file_map)
logging.info('remove %s' % del_path)
elif sys.argv[1] == 'resolve_conflict':
git_objs = {}
with open(git_bin_path, 'r') as fin:
merge_start = 0
for line_str in fin:
if line_str.startswith('<<<<<<<'):
merge_start = 1
elif line_str.startswith('======='):
merge_start = 2
elif line_str.startswith('>>>>>>>'):
merge_start = 0
elif merge_start == 0:
tmp_obj = json.loads(line_str)
leaf_name = tmp_obj['leaf_name']
leaf_file = tmp_obj['leaf_file']
git_objs[leaf_name] = leaf_file
elif merge_start == 1:
tmp_obj = json.loads(line_str)
leaf_name = tmp_obj['leaf_name']
leaf_file = tmp_obj['leaf_file']
git_objs[leaf_name] = leaf_file
elif merge_start == 2:
tmp_obj = json.loads(line_str)
leaf_name = tmp_obj['leaf_name']
leaf_file = tmp_obj['leaf_file']
if leaf_name in git_objs:
union = git_objs[leaf_name]
for tmp in leaf_file:
if tmp not in union:
union.append(tmp)
logging.info('add %s to %s' % (tmp, leaf_name))
git_objs[leaf_name] = union
else:
git_objs[leaf_name] = leaf_file
else:
logging.warning('invalid state: merge_start = %d, line_str = %s' %
(merge_start, line_str))
save_git_bin(git_objs)
git_bin_url_map = {}
with open(git_bin_url_path, 'r') as fin:
merge_start = 0
for line_str in fin:
if line_str.startswith('<<<<<<<'):
merge_start = 1
elif line_str.startswith('======='):
merge_start = 2
elif line_str.startswith('>>>>>>>'):
merge_start = 0
elif merge_start in [0, 1, 2]:
line_json = json.loads(line_str)
if line_json['leaf_path'] in git_objs:
git_bin_url_map[line_json['leaf_path']] = (line_json['sig'],
line_json['remote_path'])
else:
logging.warning('invalid state: merge_start = %d, line_str = %s' %
(merge_start, line_str))
save_git_url(git_bin_url_map)
logging.info('all conflicts fixed.')
else:
logging.warning('invalid cmd: %s' % sys.argv[1])
logging.warning(
'choices are: %s' %
','.join(['push', 'pull', 'add', 'remove', 'resolve_conflict']))