-
Notifications
You must be signed in to change notification settings - Fork 108
/
repos.py
675 lines (490 loc) · 22.8 KB
/
repos.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
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# Collective Mind repositories
import os
from cmind.repo import Repo
from cmind import utils
class Repos:
"""
CM repositories class
"""
def __init__(self, path, cfg, path_to_internal_repo = ''):
"""
Initialize CM repositories class
Args:
path (str): path to directory with repos.json where all paths of registered CM repositories are stored
cfg (dict): CM configuration
(path_to_internal_repo) (str): path to the internal CM repository (useful during development/debugging)
Returns:
(python class) with the following vars:
* path (str): path to CM repositories and index file
* full_path_to_repos (str): path to CM repositories
* path_to_internal_repo (str): path to the internal CM repository
* full_path_to_repo_paths (str): full path to repos.json
* lst (list): list of initialized CM repository classes
"""
self.path = path
self.cfg = cfg
self.full_path_to_repos = ''
# Paths to CM repos
self.paths = []
# List of initialized repositories
self.lst = []
# Potential path to internal repo
self.path_to_internal_repo = path_to_internal_repo
self.full_path_to_repo_paths = ''
self.extra_info = {}
############################################################
def load(self, init = False):
"""
Load or initialize repos.json file with repositories
Args:
init (bool): if False do not init individual CM repositories
Returns:
(CM return dict):
* return (int): return code == 0 if no error and >0 if error
* (error) (str): error string if return>0
"""
# If reload after updating repos
if init:
self.paths = []
self.lst = []
# Check if home directory exists. Create it otherwise.
if not os.path.isdir(self.path):
os.makedirs(self.path)
# Check repos holder
full_path_to_repos = os.path.join(self.path,
self.cfg['dir_repos'])
self.full_path_to_repos = full_path_to_repos
# If placeholder for repos doesn't exist, create it
if not os.path.isdir(full_path_to_repos):
os.makedirs(full_path_to_repos)
# Search if there is a file with repos
full_path_to_repo_paths = os.path.join(self.path, self.cfg['file_repos'])
self.full_path_to_repo_paths = full_path_to_repo_paths
if os.path.isfile(full_path_to_repo_paths):
r = utils.load_json(full_path_to_repo_paths)
if r['return']>0: return r
self.paths = r['meta']
else:
r = utils.save_json(full_path_to_repo_paths, meta = self.paths)
if r['return']>0: return r
# Check internal repo (will be after local)
if self.path_to_internal_repo != '' and os.path.isdir(self.path_to_internal_repo):
self.paths.insert(0, self.path_to_internal_repo)
# Prepare path for local repo (will be the first in the search as a local scratchpad)
path_local_repo = os.path.join(full_path_to_repos, self.cfg['local_repo_name'])
if not os.path.isdir(path_local_repo):
os.makedirs(path_local_repo)
path_local_repo_meta = os.path.join(path_local_repo, self.cfg['file_meta_repo']+'.yaml')
if not os.path.isfile(path_local_repo_meta):
r = utils.save_yaml(path_local_repo_meta,
meta = self.cfg['local_repo_meta'])
if r['return']>0: return r
if path_local_repo not in self.paths:
self.paths.insert(0, path_local_repo)
# Check that repository exists and load meta description
checked_self_paths = []
for path_to_repo in self.paths:
# First try concatenated path and then full path (if imported)
found = False
for full_path_to_repo in [os.path.join(full_path_to_repos, path_to_repo),
path_to_repo]:
if os.path.isdir(full_path_to_repo):
# Load description
repo = Repo(full_path_to_repo, self.cfg)
r = repo.load()
if r['return']>0 and r['return']!=16: return r
# Load only if desc exists
if r['return']!=16:
# Set only after all initializations
self.lst.append(repo)
repo_uid = repo.meta['uid']
if repo_uid!='':
self.extra_info[repo_uid]=repo
repo_alias = repo.meta['alias']
if repo_alias!='':
self.extra_info[repo_alias]=repo
found = True
break
# Repo path exists but repo itself doesn't exist - fail
if found:
checked_self_paths.append(path_to_repo)
else:
print ('WARNING: repository path {} not found (check file {})'.format(path_to_repo, full_path_to_repo_paths))
# Save with correct paths
if len(checked_self_paths)!=len(self.paths):
import copy
self.paths = copy.deepcopy(checked_self_paths)
if self.path_to_internal_repo in checked_self_paths:
checked_self_paths.remove(self.path_to_internal_repo)
print ('WARNING: fixed repo list file {}'.format(full_path_to_repo_paths))
r = utils.save_json(full_path_to_repo_paths, meta = checked_self_paths)
if r['return']>0: return r
return {'return':0}
############################################################
def process(self, repo_path, mode='add'):
"""
Add or delete CM repository
Args:
repo_path (str): path to CM repository
(mode) (str): "add" (default)
or "delete"
Returns:
(CM return dict):
* return (int): return code == 0 if no error and >0 if error
* (error) (str): error string if return>0
"""
# Load clean file with repo paths
r = utils.load_json(self.full_path_to_repo_paths)
if r['return']>0: return r
paths = r['meta']
modified = False
if mode == 'add':
if repo_path not in paths:
if len(paths)>0:
# Load meta of the current repo
path_to_repo_desc = os.path.join(repo_path, self.cfg['file_meta_repo'])
r=utils.load_yaml_and_json(file_name_without_ext=path_to_repo_desc)
if r['return']>0: return r
meta = r['meta']
alias = meta.get('alias', '')
uid = meta.get('uid', '')
# Check that no repos exist with the same alias and/or uid
# (to avoid adding forks and original repos)
for path in paths:
path_to_existing_repo_desc = os.path.join(path, self.cfg['file_meta_repo'])
r=utils.load_yaml_and_json(file_name_without_ext=path_to_existing_repo_desc)
if r['return']>0: return r
existing_meta = r['meta']
existing_alias = existing_meta.get('alias', '')
existing_uid = existing_meta.get('uid', '')
exist = False
if alias != '' and existing_alias !='' and alias == existing_alias:
exist = True
if not exist and uid !='' and existing_uid !='' and uid == existing_uid:
exist = True
if exist:
return {'return':1, 'error':'CM repository with the same alias "{}" and/or uid "{}" already exists in {}'.format(alias, uid, path)}
paths.append(repo_path)
modified = True
elif mode == 'delete':
new_paths = []
for p in paths:
if p==repo_path:
modified = True
else:
new_paths.append(p)
paths=new_paths
if modified:
r = utils.save_json(self.full_path_to_repo_paths, meta = paths)
if r['return']>0: return r
# Reload repos
self.load(init=True)
return {'return':0}
############################################################
def pull(self, alias, url = '', branch = '', checkout = '', console = False, desc = '', prefix = '', depth = None,
path_to_repo = None, checkout_only = False, skip_zip_parent_dir = False):
"""
Clone or pull CM repository
Args:
alias (str): CM repository alias
(url) (str): Git repository URL
(branch) (str): Git repository branch
(checkout) (str): Git repository checkout
(checkout_only) (bool): only checkout existing repo
(depth) (int): Git repository depth
(console) (bool): if True, print some info to console
(desc) (str): optional repository description
(prefix) (str): sub-directory to be used inside this CM repository to store artifacts
(path_to_repo) (str): force path to repo (useful to pull imported repos with non-standard path)
(checkout_only) (bool): only checkout Git repository but don't pull
(skip_zip_parent_dir) (bool): skip parent dir in CM ZIP repo (useful when
downloading CM repo archives from GitHub)
Returns:
(CM return dict):
* return (int): return code == 0 if no error and >0 if error
* (error) (str): error string if return>0
* (meta) (dict): meta of the CM repository
"""
# Prepare path
if path_to_repo == None:
path_to_repo = os.path.join(self.full_path_to_repos, alias)
if console:
print ('Local path: '+path_to_repo)
print ('')
# Check if repository already exists but corrupted
path_to_repo_desc = os.path.join(path_to_repo, self.cfg['file_meta_repo'])
r=utils.is_file_json_or_yaml(file_name = path_to_repo_desc)
if r['return']>0: return r
repo_desc_exists=r['is_file']
if os.path.isdir(path_to_repo) and not repo_desc_exists:
print ('')
print ('WARNING: directory {} already exists but without cmr.yaml - maybe clone or download was corrupted!'.format(path_to_repo))
x = input('Delete this repo (Y/n)? ')
if x.strip().lower() not in ['n','no']:
import shutil
print ('')
print ('Deleting {} ...'.format(path_to_repo))
shutil.rmtree(path_to_repo, onerror=utils.rm_read_only)
print ('')
cur_dir = os.getcwd()
clone=False
download=True if url.find('.zip')>0 else False
if not checkout_only:
if download:
# If CM repo already exists
if os.path.isdir(path_to_repo):
return {'return':1, 'error':'repository is already installed'}
os.makedirs(path_to_repo)
os.chdir(path_to_repo)
cmd = 'wget --no-check-certificate "'+url+'" -O '+alias
else:
if os.path.isdir(path_to_repo):
# Attempt to update
os.chdir(path_to_repo)
cmd = 'git pull'
else:
# Attempt to clone
clone = True
os.chdir(self.full_path_to_repos)
cmd = 'git clone '+url+' '+alias
# Check if depth is set
if depth!=None and depth!='':
cmd+=' --depth '+str(depth)
if console:
print (cmd)
print ('')
r = os.system(cmd)
if clone and not os.path.isdir(path_to_repo):
return {'return':1, 'error':'repository was not cloned'}
os.chdir(path_to_repo)
if download and not checkout_only:
import zipfile
pack_file = os.path.join(path_to_repo, alias)
# Attempt to read cmr.json
repo_pack_file = open(pack_file, 'rb')
repo_pack_zip = zipfile.ZipFile(repo_pack_file)
repo_pack_desc = self.cfg['file_meta_repo']
files=repo_pack_zip.namelist()
repo_path = path_to_repo
if console:
print ('Unpacking {} to {} ...'.format(pack_file, repo_path))
parent_dir = ''
# Unpacking zip
for f in files:
if not f.startswith('..') and not f.startswith('/') and not f.startswith('\\'):
if skip_zip_parent_dir and parent_dir == '':
parent_dir = f
ff = f[len(parent_dir):] if parent_dir != '' else f
file_path = os.path.join(repo_path, ff)
if f.endswith('/'):
# create directory
if not os.path.exists(file_path):
os.makedirs(file_path)
else:
dir_name = os.path.dirname(file_path)
if not os.path.exists(dir_name):
os.makedirs(dir_name)
# extract file
file_out = open(file_path, 'wb')
file_out.write(repo_pack_zip.read(f))
file_out.close()
repo_pack_zip.close()
repo_pack_file.close()
# remove original file
os.remove(pack_file)
# Check if branch
if branch != '' or checkout != '':
cmd = 'git checkout'
# When checkout only, we do not need -b for branch
extra_flag = ' ' if checkout_only else ' -b '
if branch != '':
cmd += extra_flag + branch
if checkout!='':
cmd += ' ' + checkout
if console:
print ('')
print (cmd)
print ('')
r = os.system(cmd)
if r>0:
return {'return':1, 'error':'git checkout for repository failed'}
# Check if repo description exists
r=utils.is_file_json_or_yaml(file_name = path_to_repo_desc)
if r['return']>0: return r
must_update_repo_desc = False
if r['is_file']:
# Load meta from the repository
r=utils.load_yaml_and_json(file_name_without_ext=path_to_repo_desc)
if r['return']>0: return r
meta = r['meta']
else:
# Prepare meta
r=utils.gen_uid()
if r['return']>0: return r
repo_uid = r['uid']
meta = {
'uid': repo_uid,
'alias': alias,
'git':True,
}
if desc!='': meta['desc']=desc
if prefix!='': meta['prefix']=prefix
must_update_repo_desc = True
# GF blocked the following code because if we create a fork of mlcommons@ck for example to ctuning@mlcommons-ck
# and then pull ctuning@mlcommons-ck, it attempts to rewrite .cmr.yaml with the new alias which we do not want to do!
# We want to keep whatever is in .cmr.yaml to avoid ambiguities ...
# else:
# # Load meta from the repository
# r=utils.load_yaml_and_json(file_name_without_ext=path_to_repo_desc)
# if r['return']>0: return r
#
# meta = r['meta']
#
# # If alias forced by user is not the same as in meta, update it
# # https://github.com/mlcommons/ck/issues/196
#
# if alias != meta.get('alias',''):
# meta['alias']=alias
#
# must_update_repo_desc = True
if must_update_repo_desc:
r=utils.save_yaml(path_to_repo_desc + '.yaml', meta=meta)
if r['return']>0: return r
# Check path to repo with prefix
path_to_repo_with_prefix = path_to_repo
if prefix!='':
path_to_repo_with_prefix = os.path.join(path_to_repo, prefix)
if not os.path.isdir(path_to_repo_with_prefix):
os.makedirs(path_to_repo_with_prefix)
# Get final alias
alias = meta.get('alias', '')
# Update repo list
# TBD: make it more safe (reload and save)
r = self.process(path_to_repo, 'add')
if r['return']>0: return r
# Go back to original directory
os.chdir(cur_dir)
if console:
print ('')
print ('CM alias for this repository: {}'.format(alias))
return {'return':0, 'meta':meta}
############################################################
def init(self, alias, uid, path = '', console = False, desc = '', prefix = '', only_register = False):
"""
Init CM repository in a given path
Args:
alias (str): CM repository alias
uid (str): CM repository UID
(path) (str): local path to a given repository (otherwise use $HOME/CM/repos)
(desc) (str): optional repository description
(prefix) (str): sub-directory to be used inside this CM repository to store artifacts
(only_register) (bool): if True, only register path in the CM index of repositories but do not recreate cmr.yaml
Returns:
(CM return dict):
* return (int): return code == 0 if no error and >0 if error
* (error) (str): error string if return>0
* meta (dict): meta of the CM repository
* path_to_repo (str): path to repository
* path_to_repo_desc (str): path to repository description
* path_to_repo_with_prefix (str): path to repository with prefix (== path_to_repo if prefix == "")
"""
# Prepare path
if uid == '':
r=utils.gen_uid()
if r['return']>0: return r
uid = r['uid']
repo_name=alias if alias!='' else uid
path_to_repo = os.path.join(self.full_path_to_repos, repo_name) if path=='' else path
# Convert potentially relative path into an absolute path
# For example, it's needed when we initialize repo in the current directory
# or use . and ..
path_to_repo = os.path.abspath(path_to_repo)
if console:
print ('Local path to the CM repository: '+path_to_repo)
print ('')
# If only register (description already exists), skip meta preparation
path_to_repo_desc = os.path.join(path_to_repo, self.cfg['file_meta_repo'])
if not only_register:
if not os.path.isdir(path_to_repo):
os.makedirs(path_to_repo)
# Check if file already exists
r=utils.is_file_json_or_yaml(file_name=path_to_repo_desc)
if r['return'] >0: return r
if r['is_file']:
return {'return':1, 'error':'Repository description already exists in {}'.format(path_to_repo)}
meta = {
'uid': uid,
'alias': alias,
'git':False,
}
if desc!='':
meta['desc']=desc
if prefix!='':
meta['prefix']=prefix
r=utils.save_yaml(path_to_repo_desc + '.yaml', meta=meta)
if r['return']>0: return r
# Check requirements.txt
path_to_requirements = os.path.join(path_to_repo, 'requirements.txt')
if not os.path.isfile(path_to_requirements):
r = utils.save_txt(file_name = path_to_requirements, string = self.cfg['new_repo_requirements'])
if r['return']>0: return r
else:
r=utils.load_yaml_and_json(file_name_without_ext=path_to_repo_desc)
if r['return'] >0: return r
meta = r['meta']
# Check if exist and create if not
path_to_repo_with_prefix = path_to_repo
if prefix!='':
path_to_repo_with_prefix = os.path.join(path_to_repo, prefix)
if not os.path.isdir(path_to_repo_with_prefix):
os.makedirs(path_to_repo_with_prefix)
# Update repo list
# TBD: make it more safe (reload and save)
r = self.process(path_to_repo, 'add')
if r['return']>0: return r
return {'return':0, 'meta':meta,
'path_to_repo': path_to_repo,
'path_to_repo_desc': path_to_repo_desc,
'path_to_repo_with_prefix': path_to_repo_with_prefix}
############################################################
def delete(self, lst, remove_all = False, console = False, force = False):
"""
Delete CM repository or repositories with or without content
Args:
lst (list of CM repository classes): list of CM repositories
remove_all (bool): if True, remove the content and unregister CM repository
console (bool): if True, output some info to console
force (bool): if True, do not ask questions
Returns:
(CM return dict):
* return (int): return code == 0 if no error and >0 if error
* (error) (str): error string if return>0
"""
# Navigate through repos from the search function
for repo in lst:
# Prepare path
path_to_repo = repo.path
if console:
print ('Local path to a CM repository: '+path_to_repo)
if path_to_repo not in self.paths:
return {'return':16, 'error':'repository not found in CM index - weird'}
if console:
if not force:
ask = input(' Are you sure you want to delete this repository (y/N): ')
ask = ask.strip().lower()
if ask!='y':
print (' Skipped!')
continue
# TBD: make it more safe (reload and save)
r = self.process(path_to_repo, 'delete')
if r['return']>0: return r
# Check if remove all
if remove_all:
import shutil
if console:
print (' Deleting repository content ...')
shutil.rmtree(path_to_repo, onerror=utils.rm_read_only)
else:
if console:
print (' CM repository was unregistered from CM but its content was not deleted ...')
return {'return':0}