-
Notifications
You must be signed in to change notification settings - Fork 120
/
core.py
1799 lines (1325 loc) · 62.6 KB
/
core.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
# Collective Mind core functions
#
# Written by Grigori Fursin
from cmind.config import Config
from cmind.repos import Repos
from cmind.index import Index
from cmind.automation import Automation
#from cmind.automation import AutomationDummy
from cmind import utils
import sys
import os
# Outdated in Python 3.12+
#import imp
import importlib
import pkgutil
import inspect
import logging
cm = None
############################################################
class CM(object):
"""
Main CM class
"""
############################################################
def __init__(self, repos_path = '', debug = False):
"""
Initialize CM configuration class
Args:
(repos_path) (str) - path to CM repositories and other information.
(debug) (bool) - if True, raise errors in internal functions
instead of returning a dictionary with "return">0.
Returns:
(python class) with the following vars:
* cfg (dict): internal CM configuration
* debug (bool): debug state
* repos_path (str): path to CM repositories and other information
* path_to_cmind (str): path to the used CM toolkit
* path_to_cmind_core_module (str): path to CM core module
* path_to_cmind_repo (str): path to the "internal" CM repo
* repos (list): list of initialized CM repository objects
* common_automation (Automation class): initialized common automation with actions when a given automation is not found or doesn't exist
* output (str): value of --out during the first access to CM (to print errors, etc)
"""
# Initialize and update CM configuration
self.cfg = Config().cfg
# Check if debug (raise error instead of soft error)
self.debug = False
if debug or os.environ.get(self.cfg['env_debug'],'').strip().lower() in ['yes','on','true']:
self.debug = True
# Explicit path to direcory with CM repositories and other internals
self.repos_path = repos_path
if self.repos_path == '':
s = os.environ.get(self.cfg['env_repos'],'').strip()
if s != '':
self.repos_path = s
if self.repos_path == '':
from os.path import expanduser
self.repos_path = os.path.join(expanduser("~"), self.cfg['default_home_dir'])
path_to_cmind = os.environ.get(self.cfg['env_home'],'').strip()
if path_to_cmind != '':
self.path_to_cmind = path_to_cmind
self.path_to_cmind_core_module = os.path.join(self.path_to_cmind, 'core.py')
else:
self.path_to_cmind_core_module = inspect.getfile(inspect.currentframe())
self.path_to_cmind = os.path.dirname(self.path_to_cmind_core_module)
self.path_to_cmind_repo = os.path.join(self.path_to_cmind, self.cfg['cmind_repo'])
# Repositories
self.repos = None
# Common automation
self.common_automation = None
# Output of the first access
self.output = None
# Check Python version
self.python_version = list(sys.version_info)
# Save output to json or yaml(only from CLI)
self.save_to_json = ''
self.save_to_yaml = ''
# Logging
self.logger = None
self.xlogger = None
# Index
self.index = None
self.use_index = True
if os.environ.get(self.cfg['env_index'],'').strip().lower() in ['no','off','false']:
self.use_index = False
# Check if CM v3+ was called (to avoid mixing up older versions and make them co-exist)
self.x_was_called = False
# Misc state
self.state = {}
############################################################
def error(self, r):
"""
If r['return']>0: print CM error and raise error if in debugging mode
Args:
r (dict): output from CM function with "return" and "error"
Returns:
(dict): r
"""
import os
if r['return']>0:
if self.debug:
raise Exception(r['error'])
text = self.cfg['error_prefix'] + ' ' + r['error'] + '!\n'
sys.stderr.write(f'\n{text}')
return r
############################################################
def errorx(self, r):
"""
If r['return']>0: print CM error and raise error if in debugging mode
Args:
r (dict): output from CM function with "return" and "error"
Returns:
(dict): r
"""
import os
if r['return']>0:
if self.debug:
raise Exception(r['error'])
if 'warning' in r:
message = r.get('warning', '')
if message != '':
message = message[0].upper() + message[1:]
message = '\nCMX warning: ' + message + '!\n'
else:
module_path = r.get('module_path', '')
lineno = r.get('lineno', '')
message = ''
if not self.xlogger == None or (module_path != '' and lineno != ''):
call_stack = self.state.get('call_stack', [])
if not self.xlogger == None:
self.log(f"x error call stack: {call_stack}", "debug")
self.log(f"x error: {r}", "debug")
# sys.stderr.write('^'*60 + '\n')
sys.stderr.write('\n')
if not self.xlogger == None:
sys.stderr.write('CMX call stack:\n')
for cs in call_stack:
sys.stderr.write(f' * {cs}\n')
message += '\n'
else:
message += '\n'
message += self.cfg['error_prefix2']
if module_path != '' and lineno !='':
message += f' while running automation {module_path} ({lineno}):\n\n'
text = r['error']
text = text[0].upper() + text[1:]
else:
message += ': '
text = r['error']
if not text.endswith('!'): text += '!'
message += text + '\n'
sys.stderr.write(message)
return r
############################################################
def prepare_error(self, returncode, error):
"""
Prepare error dictionary with the module and line number of an error
Args:
returncode (int): CMX returncode
error (str): error message
Returns:
(dict): r
return (int)
error (str)
module_path (str): path to module
lineno (int): line number
"""
from inspect import getframeinfo, stack
caller = getframeinfo(stack()[1][0])
return {'return': returncode,
'error': error,
'module_path': caller.filename,
'lineno': caller.lineno}
############################################################
def embed_error(self, r):
"""
Embed module and line number to an error
Args:
r (dict): CM return dict
Returns:
(dict): r
return (int)
error (str)
module_path (str): path to module
lineno (int): line number
"""
from inspect import getframeinfo, stack
caller = getframeinfo(stack()[1][0])
r['module_path'] = caller.filename
r['lineno'] = caller.lineno
return r
############################################################
def halt(self, r):
"""
If r['return']>0: print CM error and raise error if in debugging mode or halt with "return" code
Args:
r (dict): output from CM function with "return" and "error"
Returns:
(dict): r
"""
# Force console
self.error(r)
sys.exit(r['return'])
############################################################
def log(self, s, t = 'info'):
"""
Args:
s (str): log string
t (str): log type - "info" (default)
"debug"
"warning"
"error"
Returns:
None
"""
logger = self.xlogger
if logger != None:
if t == 'debug':
self.logger.debug(s)
elif t == 'warning':
self.logger.warning(s)
elif t == 'error':
self.logger.error(s)
# info
else:
self.logger.info(s)
return
############################################################
def access(self, i, out = None):
"""
Access CM automation actions in a unified way similar to micro-services.
(Legacy. Further development in the new "x" function).
i (dict | str | argv): unified CM input
Args:
(action) (str): automation action
(automation (CM object): CM automation in format (alias | UID | alias,UID)
or (repo alias | repo UID | repo alias,UID):(alias | UID | alias,UID)
(artifact) (CM object): CM artifact
(artifacts) (list of CM objects): extra CM artifacts
(common) (bool): if True, use common automation action from Automation class
(help) (bool): if True, print CM automation action API
(ignore_inheritance) (bool): if True, ignore inheritance when searching for artifacts and automations
(out) (str): if 'con', tell automations and CM to output extra information 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
* Output from a CM automation action
"""
# Check the type of input
if i is None:
i = {}
# Attempt to detect debug flag early (though suggest to use environment)
# If error in parse_cli, it will raise error
if self.cfg['flag_debug'] in i:
self.debug = True
# Check if log
if self.logger is None:
self.logger = logging.getLogger("cm")
# Parse as command line if string or list
if type(i) == str or type(i) == list:
import cmind.cli
r = cmind.cli.parse(i)
if r['return'] >0 : return r
i = r['cm_input']
# Check if force out programmatically (such as from CLI)
if 'out' not in i and out is not None:
i['out'] = out
output = i.get('out','')
# Check if save to json
if 'save_to_json' in i and i['save_to_json']!='':
self.save_to_json = i['save_to_json']
# Set self.output to the output of the very first access
# to print error in the end if needed
if self.output is None:
self.output = output
# Check if console
console = (output == 'con')
# Check if has help flag
cm_help = i.get(self.cfg['flag_help'], False) or i.get(self.cfg['flag_help2'], False)
# Initialized common automation with collective database actions
if self.common_automation == None:
self.common_automation = Automation(self, __file__)
# Check automation action
action = i.get('action','')
# Check automation
automation = i.get('automation','')
# Check if asked for "version" and no automation
if action == 'version' and automation == '':
automation = 'core'
elif action == '' and automation == '' and i.get('version',False):
action = 'version'
automation = 'core'
elif action == 'init' and automation == '':
automation = 'core'
# Print basic help if action == ''
extra_help = True if action == 'help' and automation == '' else False
if action == '' or extra_help:
if console:
print (self.cfg['info_cli'])
if cm_help or extra_help:
print_db_actions(self.common_automation, self.cfg['action_substitutions'], '')
return {'return':0, 'warning':'no action specified'}
# Load info about all CM repositories (to enable search for automations and artifacts)
if self.repos == None:
repos = Repos(path = self.repos_path, cfg = self.cfg,
path_to_internal_repo = self.path_to_cmind_repo)
r = repos.load()
if r['return'] >0 : return r
# Set only after all initializations
self.repos = repos
# Load index
if self.index is None:
self.index = Index(self.repos_path, self.cfg)
if self.use_index:
r = self.index.load()
if r['return']>0: return r
if not r['exists']:
# First time
if console:
print ('Warning: CM index is used for the first time. CM will reindex all artifacts now - it may take some time ...')
r = self.access({'action':'reindex',
'automation':'repo,55c3e27e8a140e48'})
if r['return']>0: return r
# Check if forced common automation
use_common_automation = True if i.get('common',False) else False
automation_lst = []
use_any_action = False
artifact = i.get('artifact','').strip()
artifacts = i.get('artifacts',[]) # Only if more than 1 artifact
# Check if automation is "." - then attempt to detect repo, automation and artifact from the current directory
if automation == '.' or artifact == '.':
r = self.access({'action':'detect',
'automation':'repo,55c3e27e8a140e48'})
if r['return']>0: return r
# Check and substitute automation
if automation == '.':
automation = ''
if r.get('artifact_found', False):
if not r.get('found_in_current_path',False):
# If not in the root directory (otherwise search through all automations)
automation = r['cm_automation']
# Check and make an artifact (only if artifacts are not specified)
if artifact == '.' or artifact == '':
artifact = ''
if r.get('cm_artifact','')!='':
artifact = r['cm_artifact']
if r.get('registered', False):
cm_repo = r['cm_repo']
if ':' not in artifact:
artifact = cm_repo + ':' + artifact
for ia in range(0,len(artifacts)):
a = artifacts[ia]
if ':' not in a:
a = cm_repo + ':' + a
artifacts[ia] = a
# If automation!='', attempt to find it and load
# Otherwise use the common automation
if automation != '':
# Parse automation potentially with a repository
# and convert it into CM object [(artifact,UID) (,(repo,UID))]
r = utils.parse_cm_object(automation)
if r['return'] >0 : return r
parsed_automation = r['cm_object']
i['parsed_automation'] = parsed_automation
if use_common_automation:
# Check that UID is set otherwise don't know how to add
xuid=parsed_automation[0][1]
if xuid == '':
return {'return':1, 'error':'you must add `,CM UID` for automation {} when using --common'.format(parsed_automation[0][0])}
elif not utils.is_cm_uid(xuid):
return {'return':1, 'error':'you must use CM UID after automation {} when using --common'.format(parsed_automation[0][0])}
automation_meta = {}
if automation != '' and not use_common_automation:
# If wildcards in automation, use the common one (usually for search across different automations)
# However, still need above "parse_automation" for proper search
if '*' in automation or '?' in automation:
use_common_automation = True
else:
# First object in a list is an automation
# Second optional object in a list is a repo
auto_name = parsed_automation[0] if len(parsed_automation)>0 else ('','')
auto_repo = parsed_automation[1] if len(parsed_automation)>1 else None
# Search for automations in repos (local, internal, other) TBD: maybe should be local, other, internal?
ii={'parsed_automation':[('automation','bbeb15d8f0a944a4')],
'parsed_artifact':parsed_automation}
# Ignore inheritance when called recursively
if i.get('ignore_inheritance',False):
ii['ignore_inheritance']=True
r = self.common_automation.search(ii)
if r['return']>0: return r
automation_lst = r['list']
if len(automation_lst)==1:
automation = automation_lst[0]
automation_path = automation.path
automation_name = self.cfg['common_automation_module_name']
automation_meta = automation.meta
use_any_action = automation_meta.get('use_any_action',False)
# Update parsed_automation with UID and alias
parsed_automation[0] = (automation_meta.get('alias',''),
automation_meta.get('uid',''))
# Find Python module for this automation: should also work with 3.12+
automation_full_path = os.path.join(automation_path, automation_name + '.py')
if not os.path.isfile(automation_full_path):
return {'return': 1, 'error': 'can\'t find Python module file {}'.format(automation_full_path)}
# Oudated
# try:
# found_automation = imp.find_module(automation_name, [automation_path])
# except ImportError as e: # pragma: no cover
# return {'return': 1, 'error': 'can\'t find Python module code (path={}, name={}, err={})'.format(automation_path, automation_name, format(e))}
found_automation_spec = importlib.util.spec_from_file_location(automation_name, automation_full_path)
if found_automation_spec == None:
return {'return': 1, 'error': 'can\'t find Python module file {}'.format(automation_full_path)}
# Outdated
# automation_handler = found_automation[0]
# Generate uid for the run-time extension of the loaded Python module
# otherwise Python modules with the same extension (key.py for example)
# will be reloaded ...
# r = utils.gen_uid()
# if r['return'] > 0: return r
# automation_run_time_uid = 'rt-'+r['uid']
try:
# Outdated
# loaded_automation = imp.load_module(automation_run_time_uid, automation_handler, automation_full_path, found_automation[2])
loaded_automation = importlib.util.module_from_spec(found_automation_spec)
found_automation_spec.loader.exec_module(loaded_automation)
except Exception as e: # pragma: no cover
return {'return': 1, 'error': 'can\'t load Python module code (path={}, name={}, err={})'.format(automation_path, automation_name, format(e))}
# Outdated
# if automation_handler is not None:
# automation_handler.close()
loaded_automation_class = loaded_automation.CAutomation
# Try to load meta description
automation_path_meta = os.path.join(automation_path, self.cfg['file_cmeta'])
r = utils.is_file_json_or_yaml(file_name = automation_path_meta)
if r['return']>0: return r
if not r['is_file']:
return {'return':4, 'error':'automation meta not found in {}'.format(automation_path)}
# Load artifact class
r=utils.load_yaml_and_json(automation_path_meta)
if r['return']>0: return r
automation_meta = r['meta']
elif len(automation_lst)>1:
return {'return':2, 'error':'ambiguity because several automations were found for {}'.format(auto_name)}
# Report an error if a repo is specified for a given automation action but it's not found there
if len(automation_lst)==0 and auto_repo is not None:
return {'return':3, 'error':'automation was not found in a specified repo {}'.format(auto_repo)}
# Convert action into function (substitute internal words)
original_action = action
action = action.replace('-','_')
if action in self.cfg['action_substitutions']:
action = self.cfg['action_substitutions'][action]
elif action in automation_meta.get('action_substitutions',{}):
action = automation_meta['action_substitutions'][action]
# Check if common automation and --help
if (use_common_automation or automation=='') and cm_help:
return print_action_help(self.common_automation,
self.common_automation,
'common',
action,
original_action)
# If no automation was found we do not force common automation, check if should fail or continue
if not use_common_automation and len(automation_lst)==0:
if self.cfg['fail_if_automation_not_found']:
# Quit with error
if automation=='':
return {'return':4, 'error':'automation was not specified'}
else:
return {'return':4, 'error':'automation "{}" not found'.format(automation)}
# If no automation was found or we force common automation
if use_common_automation or len(automation_lst)==0:
auto=('automation','bbeb15d8f0a944a4')
from . import automation as loaded_automation
loaded_automation_class = loaded_automation.Automation
automation_full_path = loaded_automation.self_path
automation_meta = {
'alias':'automation',
'uid':'bbeb15d8f0a944a4'
}
# Finalize automation class initialization
initialized_automation = loaded_automation_class(self, automation_full_path)
initialized_automation.meta = automation_meta
initialized_automation.full_path = automation_full_path
# Check action in a class when importing
if use_any_action:
action = 'any'
print_automation = automation_meta.get('alias','') + ',' + automation_meta.get('uid','')
initialized_automation.artfact = print_automation
# Check if help about automation actions
if action == 'help':
import types
print (self.cfg['info_cli'])
print ('')
print ('Automation python module: {}'.format(automation_full_path))
r = print_db_actions(self.common_automation, self.cfg['action_substitutions'], automation_meta.get('alias',''))
if r['return']>0: return r
db_actions = r['db_actions']
actions = []
for d in sorted(dir(initialized_automation)):
if d not in db_actions and type(getattr(initialized_automation, d))==types.MethodType and not d.startswith('_'):
actions.append(d)
if len(actions)>0:
print ('')
print ('Automation actions:')
print ('')
for d in actions:
print (' * cm ' + d + ' ' + automation_meta.get('alias',''))
return {'return':0, 'warning':'no automation action'}
# Check if action exists
if not hasattr(initialized_automation, action):
return {'return':4, 'error':'action "{}" not found in automation "{}"'.format(action, print_automation)}
# Check if help for a given automation action
delayed_help = False
delayed_help_api = ''
delayed_help_api_prefix = ''
delayed_help_api_prefix_0 = ''
if cm_help:
# Find path to automation
rr = print_action_help(initialized_automation,
self.common_automation,
print_automation,
action,
original_action)
if rr['return']>0: return rr
if not rr.get('delayed_help', False):
return rr
delayed_help = True
delayed_help_api = rr['help']
delayed_help_api_prefix = rr['help_prefix']
delayed_help_api_prefix_0 = rr['help_prefix_0']
# Process artifacts for this automation action
if len(artifacts)>0:
parsed_artifacts = []
for extra_artifact in artifacts:
# Parse artifact
r = parse_cm_object_and_check_current_dir(self, extra_artifact)
if r['return'] >0 : return r
parsed_artifacts.append(r['cm_object'])
i['parsed_artifacts'] = parsed_artifacts
# Check artifact and artifacts
if artifact != '':
# Parse artifact
r = parse_cm_object_and_check_current_dir(self, artifact)
if r['return'] >0 : return r
i['parsed_artifact'] = r['cm_object']
# Check min CM version requirement
min_cm_version = automation_meta.get('min_cm_version','').strip()
if min_cm_version != '':
from cmind import __version__ as current_cm_version
comparison = utils.compare_versions(current_cm_version, min_cm_version)
if comparison < 0:
return {'return':1, 'error':'CM automation requires CM version >= {} while current CM version is {} - please update using "pip install cmind -U"'.format(min_cm_version, current_cm_version)}
# Call automation action
action_addr=getattr(initialized_automation, action)
r = action_addr(i)
# Check if need to save index
if self.use_index and self.index.updated:
rx = self.index.save()
# Ignore output for now to continue working even if issues ...
self.index.updated=False
# If delayed help
if delayed_help and not r.get('skip_delayed_help', False):
print ('')
print (delayed_help_api_prefix_0)
print ('')
print (delayed_help_api_prefix)
print ('')
print (delayed_help_api)
return r
############################################################
def x(self, i, out = None):
"""
New unified access to CM automation actions
Args:
i (dict | str | argv): unified CM input
* (action) (str): automation action
* (automation (CM object): CM automation in format (alias | UID | alias,UID)
or (repo alias | repo UID | repo alias,UID):(alias | UID | alias,UID)
* (artifact) (CM object): CM artifact
* (artifacts) (list of CM objects): extra CM artifacts
Control flags starting with - :
* (out) (str): if 'con', tell automations and CM to output extra information to console
* (common) (bool): if True, use common automation action from Automation class
* (help) (bool): if True, print CM automation action API
* (ignore_inheritance) (bool): if True, ignore inheritance when searching for artifacts and automations
Returns:
(CM return dict):
* return (int): return code == 0 if no error and >0 if error
* (error) (str): error string if return>0
* Output from a given CM automation action
"""
import copy
# Check if very first access call
x_was_called = self.x_was_called
self.x_was_called = True
cur_dir = os.getcwd()
# Check the type of input
if i is None:
i = {}
# Attempt to detect debug flag early (though suggest to use environment)
# If error in parse_cli, it will raise error
if self.cfg['flag_debug'] in i:
self.debug = True
# Parse as command line if string or list
if type(i) == str or type(i) == list:
import cmind.cli
r = cmind.cli.parsex(i)
if r['return'] >0 : return r
i = r['cmx_input']
# Assemble input flags for extra checks in automations
if 'control' not in i:
i['control'] = {}
i['control']['_input'] = {}
for k in i:
if k not in ['control', 'action', 'automation', 'artifact', 'artifacts']:
i['control']['_input'][k] = i[k]
control = i['control']
# Expose only control flags
control_flags = {}
for flag in control:
if not flag.startswith('_'):
control_flags[flag] = control[flag]
# Check if unknown flags
# f should be deprecated in the future - used for backwards
# compatibility with older commands like cm/cmx rm cache -f
unknown_control_flags = [flag for flag in control_flags if flag not in [
'h', 'help', 'version', 'out', 'j', 'json',
'save_to_json_file', 'save_to_yaml_file', 'common',
'ignore_inheritance', 'log', 'logfile', 'raise', 'repro',
'i', 'f', 'time', 'profile']]
delayed_error = ''
if len(unknown_control_flags)>0:
unknown_control_flags_str = ','.join(unknown_control_flags)
delayed_error = f'Unknown control flag(s): {unknown_control_flags_str}'
# Force print help
control['h'] = True
if control.pop('f', ''):
i['f'] = True
output_json = (control.get('j', False) or control.get('json', False))
self_time = control.get('time', False)
if not x_was_called and self_time:
import time
self_time1 = time.time()
self_profile = control.get('profile', False)
self_info = control.get('i', False)
# Check repro
use_log = str(control_flags.pop('log', '')).strip().lower()
log_file = control_flags.pop('logfile', '')
if control.get('repro', '') != '':
if not os.path.isdir('cmx-repro'):
os.mkdir('cmx-repro')
if log_file == '':
log_file = os.path.join(cur_dir, 'cmx-repro', 'cmx.log')
if use_log == '':
use_log = 'debug'
ii = copy.deepcopy(i)
ii['control'] = {}
for k in control:
if not k.startswith('_') and k not in ['repro']:
ii['control'][k] = i[k]
utils.save_json(os.path.join('cmx-repro', 'cmx-input.json'),
meta = ii)
# Check logging
if self.xlogger is None:
log_level = None
if use_log == "false":
use_log = ''
elif use_log == "true":
use_log = 'info'
if log_file == '':
log_file = None
else:
if use_log == '':
use_log = 'debug'
if use_log != '':
if use_log == 'debug':
log_level = logging.DEBUG
elif use_log == 'warning':
log_level = logging.WARNING
elif use_log == 'error':
log_level = logging.ERROR
else:
# info by default
log_level = logging.INFO
# Configure
self.xlogger = logging.getLogger("cmx")
logging.basicConfig(filename = log_file, filemode = 'w', level = log_level)
# Check if force out programmatically (such as from CLI)
if 'out' not in control and out is not None:
control['out'] = out
use_raise = control.get('raise', False)
# Log access
recursion = self.state.get('recursion', 0)
self.state['recursion'] = recursion + 1
if not self.xlogger == None:
log_action = i.get('action', '')
log_automation = i.get('automation', '')
log_artifact = i.get('artifact', '')
spaces = ' ' * recursion
self.log(f"x log: {spaces} {log_action} {log_automation} {log_artifact}", "info")
self.log(f"x input: {spaces} ({i})", "debug")
# Call access helper
if not x_was_called and self_info:
utils.get_memory_use(True)
print ('')
utils.get_disk_use('/', True)
print ('')
if not x_was_called and self_profile:
# https://docs.python.org/3/library/profile.html#module-cProfile
import cProfile, pstats, io
from pstats import SortKey
profile = cProfile.Profile()
profile.enable()
r = self._x(i, control)
if delayed_error != '' and r['return'] == 0:
r['return'] = 1
r['error'] = delayed_error
if not self.xlogger == None:
self.log(f"x output: {r}", "debug")
self.state['recursion'] = recursion
if not x_was_called:
if self_profile:
profile.disable()
s = io.StringIO()
sortby = SortKey.CUMULATIVE
ps = pstats.Stats(profile, stream=s).sort_stats(sortby)
ps.print_stats(32)
print ('')
print ('CMX profile:')
print ('')
print (s.getvalue())
# Very first call (not recursive)
# Check if output to json and save file
if self_time: