-
Notifications
You must be signed in to change notification settings - Fork 108
/
module.py
1198 lines (837 loc) · 37.2 KB
/
module.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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
from cmind.automation import Automation
from cmind import utils
class CAutomation(Automation):
"""
CM "repo" automation actions
"""
############################################################
def __init__(self, cmind, automation_file):
super().__init__(cmind, __file__)
############################################################
def pull(self, i):
"""
Clone or pull CM repository.
Args:
(CM input dict):
(out) (str): if 'con', output to console
(artifact) (str): repository name (alias)
(url) (str): URL of a repository
(pat) (str): Personal Access Token (if supported and url=='')
(branch) (str): Git branch
(checkout) (str): Git checkout
(checkout_only) (bool): only checkout existing repo
(depth) (int): Git depth
(desc) (str): brief repository description (1 line)
(prefix) (str): extra directory to keep CM artifacts
(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
"""
console = i.get('out') == 'con'
alias = i.get('artifact','')
url = i.get('url','')
desc = i.get('desc','')
prefix = i.get('prefix','')
pat = i.get('pat','')
checkout_only = i.get('checkout_only', False)
skip_zip_parent_dir = i.get('skip_zip_parent_dir', False)
if url == '':
if alias != '':
url = self.cmind.cfg['repo_url_prefix']
if '@' not in alias:
alias = self.cmind.cfg['repo_url_org'] + '@' + alias
url += alias.replace('@','/')
if pat != '' and url.startswith('https://'):
url = url[:8]+pat+'@'+url[8:]
else:
if alias == '':
# Get alias from URL
alias = url
# Check if zip file
j = alias.find('.zip')
if j>0:
j1 = alias.rfind('/')
alias = alias[j1+1:j+4]
else:
if alias.endswith('.git'): alias=alias[:-4]
j = alias.find('//')
if j>=0:
j1 = alias.find('/', j+2)
if j1>=0:
alias = alias[j1+1:].replace('/','@')
if url == '':
pull_repos = []
for repo in sorted(self.cmind.repos.lst, key = lambda x: x.meta.get('alias','')):
meta = repo.meta
if meta.get('git', False):
# Note that internal repo alias may not be the same as the real pulled alias since it can be a fork
# Pick it up from the path
repo_path = repo.path
pull_repos.append({'alias': os.path.basename(repo_path),
'path_to_repo': repo_path})
else:
pull_repos = [{'alias':alias,
'url':url,
'branch': i.get('branch', ''),
'checkout': i.get('checkout', ''),
'depth': i.get('depth', '')}]
# Go through repositories and pull
repo_meta = {}
repo_metas = {}
for repo in pull_repos:
alias = repo['alias']
url = repo.get('url', '')
branch = repo.get('branch','')
checkout = repo.get('checkout','')
depth = repo.get('depth','')
path_to_repo = repo.get('path_to_repo', None)
if console:
print (self.cmind.cfg['line'])
print ('Alias: {}'.format(alias))
if url!='':
print ('URL: {}'.format(url))
if branch!='':
print ('Branch: {}'.format(branch))
if checkout!='':
print ('Checkout: {}'.format(checkout))
if depth!='' and depth!=None:
print ('Depth: {}'.format(str(depth)))
print ('')
# Prepare path to repo
repos = self.cmind.repos
r = repos.pull(alias = alias,
url = url,
branch = branch,
checkout = checkout,
console = console,
desc=desc,
prefix=prefix,
depth=depth,
path_to_repo=path_to_repo,
checkout_only=checkout_only,
skip_zip_parent_dir=skip_zip_parent_dir)
if r['return']>0: return r
repo_meta = r['meta']
repo_metas[alias] = repo_meta
if len(pull_repos)>0 and self.cmind.use_index:
if console:
print (self.cmind.cfg['line'])
ii = {'out':'con'} if console else {}
rx = self.reindex(ii)
return {'return':0, 'meta':repo_meta, 'metas': repo_metas}
############################################################
def checkout(self, i):
"""
Checkout repository
Args:
(branch) (str): branch name
(checkout) (str): checkout
See "pull" action
Returns:
See "pull" action
"""
i['checkout_only'] = True
return self.pull(i)
############################################################
def show(self, i):
"""
Show verbose info about registered CM repos.
Args:
See "search" action
Returns:
See "search" action
"""
i['verbose'] = True
return self.search(i)
############################################################
def search(self, i):
"""
List registered CM repos.
Args:
(CM input dict):
(with_prefix) (bool): if True return path to repo artifacts (with prefix if exists)
(out) (str): if 'con', output to console
(verbose) (bool): if True show extra info about repositories
parsed_artifact (list): prepared in CM CLI or CM access function - repository name with wildcards
(min) (bool): if True, return only path
Returns:
(CM return dict):
* return (int): return code == 0 if no error and >0 if error
* (error) (str): error string if return>0
* list (list): list of CM repository objects
"""
console = i.get('out') == 'con'
with_prefix = i.get('with_prefix', False)
lst = []
parsed_artifact = i.get('parsed_artifact',[])
min_out = i.get('min',False)
artifact_obj = parsed_artifact[0] if len(parsed_artifact)>0 else ('','')
artifact_repo = parsed_artifact[1] if len(parsed_artifact)>1 else None
artifact_repo_wildcards = False
if artifact_repo is not None:
if '*' in artifact_repo[0] or '?' in artifact_repo[0]:
artifact_repo_wildcards = True
for repo in self.cmind.repos.lst:
meta = repo.meta
uid = meta['uid']
alias = meta.get('alias','')
r = utils.match_objects(uid = uid,
alias = alias,
uid2 = artifact_obj[1],
alias2 = artifact_obj[0])
if r['return']>0: return r
if r['match']:
lst.append(repo)
# Output paths to console if CLI or forced
if console:
for l in lst:
meta = l.meta
alias = meta.get('alias', '')
uid = meta.get('uid', '')
prefix = meta.get('prefix', '')
path = l.path_with_prefix if with_prefix else l.path
if min_out:
print (path)
else:
if i.get('verbose', False):
uid = meta['uid']
desc = meta.get('desc', '')
git = meta.get('git', False)
prefix = meta.get('prefix', '')
local_alias = os.path.basename(path)
if alias == 'local' or alias == 'internal' or local_alias == 'ck':
local_alias = ''
print (self.cmind.cfg['line'])
print ('Path: {}'.format(path))
if prefix != '':
print (' CM sub-directory: {}'.format(prefix))
if alias != local_alias:
if local_alias!='':
print (' Alias: {}'.format(local_alias))
print (' Original alias: {}'.format(alias))
else:
print (' Alias: {}'.format(alias))
else:
print (' Alias: {}'.format(alias))
print (' UID: {}'.format(uid))
if desc != '':
print ('Description: {}'.format(desc))
print ('Git: {}'.format(str(git)))
if git:
url = ''
branch = ''
checkout = ''
r = self.cmind.access({'action':'system', 'automation':'utils', 'path':path, 'cmd':'git config --get remote.origin.url'})
if r['return'] == 0 and r['ret'] == 0:
url = r['stdout']
r = self.cmind.access({'action':'system', 'automation':'utils', 'path':path, 'cmd':'git rev-parse --abbrev-ref HEAD'})
if r['return'] == 0 and r['ret'] == 0:
branch = r['stdout']
r = self.cmind.access({'action':'system', 'automation':'utils', 'path':path, 'cmd':'git rev-parse HEAD'})
if r['return'] == 0 and r['ret'] == 0:
checkout = r['stdout']
if url!='':
print (' URL: {}'.format(url))
if branch!='':
print (' Branch: {}'.format(branch))
if checkout!='':
print (' Checkout: {}'.format(checkout))
else:
print ('{},{} = {}'.format(alias, uid, path))
return {'return':0, 'list':lst}
############################################################
def where(self, i):
"""
Print only path to a given repo
Args:
(CM input dict):
The same as "search"
Returns:
(CM return dict):
* return (int): return code == 0 if no error and >0 if error
* (error) (str): error string if return>0
* list (list): list of updated CM repository objects
"""
i['min']=True
return self.search(i)
############################################################
def update(self, i):
"""
Update all Git-based CM repositories (git pull).
Args:
(CM input dict):
(out) (str): if 'con', output to console
Returns:
(CM return dict):
* return (int): return code == 0 if no error and >0 if error
* (error) (str): error string if return>0
* list (list): list of updated CM repository objects
"""
console = i.get('out') == 'con'
for repo in self.cmind.repos.lst:
meta = repo.meta
alias = meta.get('alias','')
git = meta.get('git', False)
if git:
if console:
print ('Updating "{}" ...'.format(alias))
print ('')
r = self.cmind.repos.pull(alias = alias, console = console)
if r['return']>0: return r
return {'return':0, 'list':self.cmind.repos.lst}
############################################################
def delete(self, i):
"""
Delete CM repository (either just unregister it or delete the content too).
Args:
(CM input dict):
(out) (str): if 'con', output to console
(verbose) (bool) - if True show extra info about repositories
parsed_artifact (list): prepared in CM CLI or CM access function - repository name with wildcards
(all) (bool) - if True, remove with content otherwise just unlink
(force) (bool) - if True, do not ask questions
(f) (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
"""
console = i.get('out') == 'con'
force = (i.get('force',False) or i.get('f',False))
artifact = i.get('artifact','').strip()
if artifact == '':
return {'return':1, 'error':'repositories are not specified'}
# Search CM repository
i['action']='search'
i['out']=None
r=self.cmind.access(i)
if r['return'] >0 : return r
lst = r['list']
if len(lst)==0:
return {'return':1, 'error':'Repository not found'}
remove_all = i.get('all', '')
r = self.cmind.repos.delete(lst, remove_all = remove_all, console = console, force = force)
if self.cmind.use_index:
ii = {'out':'con'} if console else {}
rx = self.reindex(ii)
return r
############################################################
def ximport(self, i):
if i.get('path','')!='':
i['here']=True
return self.init(i)
############################################################
def init(self, i):
"""
Initialize CM repository.
Args:
(CM input dict):
(out) (str): if 'con', output to console
(artifact) (str): repository name (alias)
(uid) (str): force CM UID for this repository
(path) (str): specify a path where to create this repository
(here) (str): use current path
(desc) (str): brief repository description (1 line)
(prefix) (str): extra directory to keep CM artifacts
Returns:
(CM return dict):
* return (int): return code == 0 if no error and >0 if error
* (error) (str): error string if return>0
* CM repository object
"""
console = i.get('out') == 'con'
alias = i.get('artifact', '')
uid = i.get('uid','')
path = i.get('path', '')
desc = i.get('desc','')
prefix = i.get('prefix','')
# If path is not specified, initialize in the current directory!
if (path=='' and alias=='') or (alias!='' and (path=='.' or i.get('here', False))):
path = os.path.abspath(os.getcwd())
# Check if there is a repo in a path
r = self.cmind.access({'action':'detect',
'automation':self.meta['alias']+','+self.meta['uid'],
'path':path})
if r['return']>0: return r
repo_desc_found = r['found']
repo_registered = r.get('registered',False)
if repo_desc_found:
if repo_registered:
return {'return':1, 'error':'CM repository found in this path and already registered in CM'}
if console:
print ('CM repository description found in this path: {}'.format(r['path_to_repo_desc']))
path= r['path_to_repo']
repo_meta = r['meta']
repo_meta_alias = repo_meta.get('alias','')
repo_meta_uid = repo_meta.get('uid','')
repo_meta_prefix = repo_meta.get('prefix','')
# Check if mismatch in alias
if alias!='':
if repo_meta_alias!='' and repo_meta_alias!=alias:
return {'return':1, 'error':'mismatch between new repo alias and the existing one'}
else:
alias = repo_meta_alias
# Check if mismatch in UID
if uid!='':
if repo_meta_uid!='' and repo_meta_uid!=uid:
return {'return':1, 'error':'mismatch between new repo UID and the existing one'}
else:
uid = repo_meta_uid
# Check if mismatch in prefix
if prefix!='':
if repo_meta_prefix!='' and repo_meta_prefix is not None and repo_meta_prefix!=prefix:
return {'return':1, 'error':'mismatch between new repo prefix and the existing one'}
else:
prefix = repo_meta_prefix
# If still no alias, use from the path
if alias == '':
# Try to get the name of the directory
alias = os.path.basename(path).strip().lower()
# Generate UID
if uid =='':
r=utils.gen_uid()
if r['return']>0: return r
uid = r['uid']
if alias == '': alias = uid
if console:
print (self.cmind.cfg['line'])
print ('Alias: {}'.format(alias))
print ('UID: {}'.format(uid))
print ('Desc: {}'.format(desc))
print ('Prefix: {}'.format(prefix))
print ('')
# Check if repository with this alias and UID doesn't exist
for repo_artifact in [alias, uid]:
ii={'automation':'repo', 'action':'search', 'artifact':repo_artifact}
r=self.cmind.access(ii)
if r['return']>0: return r
lst=r['list']
if len(lst)>0:
return {'return':1, 'error':'Repository "{}" is already registered in CM'.format(repo_artifact)}
# Create repository
r = self.cmind.repos.init(alias = alias, uid = uid, path = path, console = console, desc=desc, prefix=prefix, only_register=repo_desc_found)
if self.cmind.use_index:
ii = {'out':'con'} if console else {}
rx = self.reindex(ii)
return r
############################################################
def add(self, i):
"""
The same as "init".
"""
return self.init(i)
############################################################
def pack(self, i):
"""
Pack CM repository to cm.zip file.
Args:
(CM input dict):
(out) (str): if 'con', output to console
(artifact) (str): repository name (alias)
Returns:
(CM return dict):
* return (int): return code == 0 if no error and >0 if error
* (error) (str): error string if return>0
* CM repository object
"""
import zipfile
console = i.get('out') == 'con'
# Search repository
i['out']=None
r = self.search(i)
if r['return']>0: return r
lst = r['list']
if len(lst)==0:
return {'return':16, 'error':'no repsitories found'}
elif len(lst)>1:
return {'return':16, 'error':'more than 1 repository found'}
repo = lst[0]
repo_path = repo.path
pack_file = self.cmind.cfg['default_repo_pack']
if console:
print ('Packing repo from {} to {} ...'.format(repo_path, pack_file))
r = utils.list_all_files({'path': repo_path, 'all': 'yes', 'ignore_names':['.git']})
if r['return'] > 0: return r
files = r['list']
# Prepare zip archive
try:
f = open(pack_file, 'wb')
z = zipfile.ZipFile(f, 'w', zipfile.ZIP_DEFLATED)
for fn in files:
path_to_file = os.path.join(repo_path, fn)
z.write(path_to_file, fn, zipfile.ZIP_DEFLATED)
z.close()
f.close()
except Exception as e:
return {'return': 1, 'error': 'failed to pack repo: {}'.format(format(e))}
return {'return':0}
############################################################
def unpack(self, i):
"""
Unpack cm.zip and create a CM repository.
Args:
(CM input dict):
(out) (str): if 'con', output to console
Returns:
(CM return dict):
* return (int): return code == 0 if no error and >0 if error
* (error) (str): error string if return>0
* CM repository object
"""
import zipfile
console = i.get('out') == 'con'
pack_file = self.cmind.cfg['default_repo_pack']
if not os.path.isfile(pack_file):
return {'return':16, 'error':'CM repo pack file not found {}'.format(pack_file)}
# Attempt to read cmr.json
repo_pack_file = open(pack_file, 'rb')
repo_pack_zip = zipfile.ZipFile(repo_pack_file)
repo_pack_desc = self.cmind.cfg['file_meta_repo']
files=repo_pack_zip.namelist()
repo_meta = {}
file_yaml = repo_pack_desc + '.yaml'
if file_yaml in files:
repo_meta_yaml = repo_pack_zip.read(file_yaml)
import yaml
repo_meta.update(yaml.load(repo_meta_yaml, Loader=yaml.FullLoader))
file_json = repo_pack_desc + '.json'
if file_json in files:
repo_meta_json = repo_pack_zip.read(file_json)
import json
repo_meta.update(json.load(repo_meta_json))
# Get meta info
uid = repo_meta['uid']
alias = repo_meta['alias']
desc = repo_meta.get('desc','')
prefix = repo_meta.get('prefix','')
# Check if repo exists
r = self.search({'parsed_artifact':[(alias,uid)]})
if r['return']>0: return r
lst = r['list']
if len(lst)>0:
return {'return':1, 'error':'Repository already exists'}
# Initialize repository
r_new = self.init({'artifact':alias,
'uid':uid,
'prefix':prefix})
if r_new['return']>0: return r_new
repo_path = r_new['path_to_repo']
if console:
print ('Unpacking {} to {} ...'.format(pack_file, repo_path))
# Unpacking zip
for f in files:
if not f.startswith('..') and not f.startswith('/') and not f.startswith('\\'):
file_path = os.path.join(repo_path, f)
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()
if self.cmind.use_index:
ii = {'out':'con'} if console else {}
rx = self.reindex(ii)
return r_new
##############################################################################
def import_ck_to_cm(self, i):
"""
Convert all legacy CK repositories to CM repositories
Args:
(CM input dict): None
Returns:
(CM return dict):
* return (int): return code == 0 if no error and >0 if error
* (error) (str): error string if return>0
"""
import ck.kernel as ck
import cmind
import os
r=ck.access({'action':'search',
'module_uoa':'repo',
'add_meta':'yes'})
if r['return']>0: return r
lst=r['lst']
for l in lst:
ruoa=l['data_uoa']
ruid=l['data_uid']
rmeta=l['meta']
rpath=rmeta.get('path','')
if rpath!='' and os.path.isdir(rpath):
print ('********************************************************')
print (ruoa)
print (rpath)
r=cmind.access({'automation':'repo',
'action':'init',
'artifact':ruoa,
'path':rpath})
if r['return']>0: return r
r = convert_ck_dir_to_cm(rpath)
if r['return']>0: return r
return {'return':0}
##############################################################################
def convert_ck_to_cm(self, i):
"""
Convert a CM repository with CK artifacts into CM artifacts
Args:
(CM input dict):
(artifact) (str): CM repository name (alias)
Returns:
(CM return dict):
* return (int): return code == 0 if no error and >0 if error
* (error) (str): error string if return>0
"""
import ck.kernel as ck
import cmind
import os
console = i.get('out') == 'con'
# Search CM repository
i['action']='search'
i['out']=None
r=self.cmind.access(i)
if r['return'] >0 : return r
lst = r['list']
if len(lst)==0:
return {'return':1, 'error':'Repository not found'}
for repo in lst:
ralias = repo.meta['alias']
ruid = repo.meta['uid']
rpath = repo.path
if rpath!='' and os.path.isdir(rpath):
print ('********************************************************')
print (ralias)
print (ruid)
print (rpath)
r = convert_ck_dir_to_cm(rpath)
if r['return']>0: return r
return {'return':0}
############################################################
def detect(self, i):
"""
Detect CM repository in the current path.
Args:
(CM input dict):
(path) (str): specify path where to detect CM repo.
Use current path otherwise.
Returns:
(CM return dict):
* return (int): return code == 0 if no error and >0 if error
* (error) (str): error string if return>0
* found (bool): True if found
* path_to_repo (str): path to repo if repo found
* path_to_repo_desc (str): path to repo description if repo found
* found_in_current_path (bool): if True, found in the current directory
* meta (dict): meta of the found repo
* cm_repo (str): CM repo object (alias,UID)
* registered (bool): True, if this repository is registered in CM
* artifact_found (bool): True, if CM artifact found in the current path
* path_to_artifact (str): path to artifact if found
* path_to_artifact_desc (str): path to artifact description if found
* artifact_found_in_current_path (str): artifact found in the current directory
* artifact_meta (dict): artifact meta description
* cm_artifact (str): CM artifact object (alias,UID)
* cm_automation (str): CM automation object (alias,UID)
"""
console = i.get('out') == 'con'
# Prepare path to repo
repos = self.cmind.repos
path = i.get('path')
if path == '': path = None
# Search for cmr.yaml or cmr.json in current directory and above
r=utils.find_file_in_current_directory_or_above([self.cmind.cfg['file_meta_repo']+'.json',
self.cmind.cfg['file_meta_repo']+'.yaml'],
path_to_start = path)
if r['return']>0: return r
rr = {'return':0}
found = r['found']
rr['found'] = found
registered = False
if found:
path_to_repo_desc = r['path_to_file']
path_to_repo = r['path']
rr['path_to_repo_desc'] = path_to_repo_desc
rr['path_to_repo'] = path_to_repo
rr['found_in_current_path'] = r['found_in_current_path']
# Load meta
r = utils.load_json_or_yaml(path_to_repo_desc)
if r['return']>0: return r
repo_meta = r['meta']
rr['meta'] = repo_meta
# Check if belongs to installed repo already
path_to_repo_real = os.path.realpath(path_to_repo)
if path_to_repo_real == os.path.realpath(repos.path_to_internal_repo):
registered = True
if not registered:
for tmp_path in repos.paths:
if path_to_repo_real == os.path.realpath(tmp_path):
registered = True
if registered:
rr['cm_repo'] = utils.assemble_cm_object(repo_meta['alias'],repo_meta['uid'])
rr['registered'] = registered
artifact_found = False
artifact_found_in_current_path = False
if found:
for reverse in [False, True]:
r=utils.find_file_in_current_directory_or_above([self.cmind.cfg['file_cmeta']+'.json',
self.cmind.cfg['file_cmeta']+'.yaml'],
path_to_start = path,
reverse = reverse)
if r['return']>0: return r
artifact_found = r['found']
artifact_found_in_current_path = r.get('found_in_current_path', False)
rr['artifact_found'] = artifact_found
if artifact_found:
path_to_artifact_desc = r['path_to_file']
path_to_artifact = r['path']
rr['path_to_artifact_desc'] = path_to_artifact_desc
rr['path_to_artifact'] = path_to_artifact
rr['artifact_found_in_current_path'] = artifact_found_in_current_path
rr['artifact_reverse_search'] = reverse