@@ -502,8 +502,7 @@ def _run(self, i):
502
502
ii = {}
503
503
ii ['tags' ] = tags_string
504
504
ii ['out' ] = None
505
- for key in ["automation" , "parsed_automation" ,
506
- "artifact" , "parsed_artifact" ]:
505
+ for key in ["automation" , "artifact" , "item" , "details" ]:
507
506
if i .get (key ):
508
507
ii [key ] = i [key ]
509
508
@@ -2792,13 +2791,19 @@ def search(self, i):
2792
2791
# Find MLC script(s) based on thier tags to get their meta (can be more than 1)
2793
2792
# Then check if variations exists inside meta
2794
2793
2795
- i ['tags' ] = ',' .join (script_tags )
2794
+ ii = {}
2795
+ ii ['tags' ] = ',' .join (script_tags )
2796
2796
2797
- i ['out' ] = None
2798
- i ['common' ] = True
2797
+ ii ['out' ] = None
2798
+ ii ['common' ] = True
2799
+
2800
+ ii ['target' ] = "script"
2801
+ for key in ["automation" , "item" ,
2802
+ "artifact" , "details" ]:
2803
+ if i .get (key ):
2804
+ ii [key ] = i [key ]
2799
2805
2800
- i ['target_name' ] = "script"
2801
- r = super (ScriptAutomation , self ).search (i )
2806
+ r = super (ScriptAutomation , self ).search (ii )
2802
2807
if r ['return' ] > 0 :
2803
2808
return r
2804
2809
@@ -2901,7 +2906,6 @@ def test(self, i):
2901
2906
# Find script item(s)
2902
2907
i ['out' ] = None
2903
2908
r = self .search (i )
2904
-
2905
2909
if r ['return' ] > 0 :
2906
2910
return r
2907
2911
@@ -2910,7 +2914,6 @@ def test(self, i):
2910
2914
for script_item in lst :
2911
2915
path = script_item .path
2912
2916
meta = script_item .meta
2913
- original_meta = script_item .original_meta
2914
2917
2915
2918
alias = meta .get ('alias' , '' )
2916
2919
uid = meta .get ('uid' , '' )
@@ -2970,14 +2973,14 @@ def test(self, i):
2970
2973
if given_variations :
2971
2974
v_split = []
2972
2975
run_variations = []
2973
- for i , v in enumerate ( given_variations ) :
2976
+ for v in given_variations :
2974
2977
v_split = v .split ("," )
2975
- for t in v_split :
2978
+ for index , t in enumerate ( v_split ) :
2976
2979
if not t .startswith ("_" ):
2977
2980
# variations must begin with _. We
2978
2981
# support both with and without _
2979
2982
# in the meta
2980
- given_variations [ i ] = f"_{ t } "
2983
+ v_split [ index ] = f"_{ t } "
2981
2984
if v_split :
2982
2985
run_variations .append (
2983
2986
"," .join (v_split ))
@@ -3018,7 +3021,8 @@ def test(self, i):
3018
3021
r = self .action_object .access (ii )
3019
3022
if r ['return' ] > 0 :
3020
3023
return r
3021
-
3024
+ if is_true (i .get ('docker_prune' , '' )):
3025
+ docker_prune ()
3022
3026
return {'return' : 0 , 'list' : lst }
3023
3027
3024
3028
############################################################
@@ -3097,250 +3101,8 @@ def native_run(self, i):
3097
3101
3098
3102
return {'return' : 0 , 'return_code' : rc }
3099
3103
3100
- ############################################################
3101
- def add (self , i ):
3102
- """
3103
- Add MLC script
3104
-
3105
- Args:
3106
- (MLC input dict):
3107
-
3108
- (out) (str): if 'con', output to console
3109
-
3110
- parsed_artifact (list): prepared in MLC CLI or MLC access function
3111
- [ (artifact alias, artifact UID) ] or
3112
- [ (artifact alias, artifact UID), (artifact repo alias, artifact repo UID) ]
3113
-
3114
- (tags) (str): tags to find an MLC script (MLC artifact)
3115
-
3116
- (script_name) (str): name of script (it will be copied to the new entry and added to the meta)
3117
-
3118
- (tags) (string or list): tags to be added to meta
3119
-
3120
- (new_tags) (string or list): new tags to be added to meta (the same as tags)
3121
-
3122
- (json) (bool): if True, record JSON meta instead of YAML
3123
-
3124
- (meta) (dict): preloaded meta
3125
-
3126
- (template) (string): template to use (python)
3127
- (python) (bool): template=python
3128
- (pytorch) (bool): template=pytorch
3129
- ...
3130
-
3131
- Returns:
3132
- (MLC return dict):
3133
-
3134
- * return (int): return code == 0 if no error and >0 if error
3135
- * (error) (str): error string if return>0
3136
-
3137
- """
3138
-
3139
- import shutil
3140
-
3141
- console = i .get ('out' ) == 'con'
3142
- logger = self .action_object .logger
3143
-
3144
- # Try to find script artifact by alias and/or tags
3145
- # ii = utils.sub_input(i, self.cmind.cfg['artifact_keys'])
3146
- ii = {}
3147
- ii ['tags' ] = tags_string
3148
- ii ['out' ] = None
3149
-
3150
- for key in ["automation" , "parsed_automation" ,
3151
- "artifact" , "parsed_artifact" ]:
3152
- if i .get (key ):
3153
- ii [key ] = i [key ]
3154
-
3155
- parsed_artifact = i .get ('parsed_artifact' , [])
3156
-
3157
- artifact_obj = parsed_artifact [0 ] if len (parsed_artifact ) > 0 else None
3158
- artifact_repo = parsed_artifact [1 ] if len (
3159
- parsed_artifact ) > 1 else None
3160
-
3161
- script_name = ''
3162
- if 'script_name' in i :
3163
- script_name = i .get ('script_name' , '' ).strip ()
3164
- del (i ['script_name' ])
3165
-
3166
- if script_name != '' and not os .path .isfile (script_name ):
3167
- return {'return' : 1 ,
3168
- 'error' : 'file {} not found' .format (script_name )}
3169
-
3170
- # Move tags from input to meta of the newly created script artifact
3171
- res = utils .convert_tags_to_list (i ['tags' ])
3172
- if res ['return' ] > 0 :
3173
- return res
3174
- tags_list = res ['tags' ]
3175
- if 'tags' in i :
3176
- del (i ['tags' ])
3177
-
3178
- if len (tags_list ) == 0 :
3179
- if console :
3180
- x = input (
3181
- 'Please specify a combination of unique tags separated by comma for this script: ' )
3182
- x = x .strip ()
3183
- if x != '' :
3184
- tags_list = x .split (',' )
3185
-
3186
- if len (tags_list ) == 0 :
3187
- return {
3188
- 'return' : 1 , 'error' : 'you must specify a combination of unique tags separate by comman using "--new_tags"' }
3189
-
3190
- # Add placeholder (use common action)
3191
- ii ['out' ] = 'con'
3192
- # Avoid recursion - use internal MLC add function to add the script
3193
- # artifact
3194
- ii ['common' ] = True
3195
-
3196
- # Check template path
3197
- template_dir = 'template'
3198
-
3199
- template = i .get ('template' , '' )
3200
-
3201
- if template == '' :
3202
- if i .get ('python' , False ):
3203
- template = 'python'
3204
- elif i .get ('pytorch' , False ):
3205
- template = 'pytorch'
3206
-
3207
- if template != '' :
3208
- template_dir += '-' + template
3209
-
3210
- template_path = os .path .join (self .path , template_dir )
3211
-
3212
- if not os .path .isdir (template_path ):
3213
- return {'return' : 1 , 'error' : 'template path {} not found' .format (
3214
- template_path )}
3215
-
3216
- # Check if preloaded meta exists
3217
- meta = {
3218
- 'cache' : False
3219
- # 20240127: Grigori commented that because newly created script meta looks ugly
3220
- # 'new_env_keys':[],
3221
- # 'new_state_keys':[],
3222
- # 'input_mapping':{},
3223
- # 'docker_input_mapping':{},
3224
- # 'deps':[],
3225
- # 'prehook_deps':[],
3226
- # 'posthook_deps':[],
3227
- # 'post_deps':[],
3228
- # 'versions':{},
3229
- # 'variations':{},
3230
- # 'input_description':{}
3231
- }
3232
-
3233
- fmeta = os .path .join (
3234
- template_path ,
3235
- self .action_object .cfg ['file_cmeta' ])
3236
-
3237
- r = utils .load_yaml_and_json (fmeta )
3238
- if r ['return' ] == 0 :
3239
- utils .merge_dicts ({'dict1' : meta ,
3240
- 'dict2' : r ['meta' ],
3241
- 'append_lists' : True ,
3242
- 'append_unique' : True })
3243
-
3244
- # Check meta from CMD
3245
- xmeta = i .get ('meta' , {})
3246
-
3247
- if len (xmeta ) > 0 :
3248
- utils .merge_dicts ({'dict1' : meta , 'dict2' : xmeta ,
3249
- 'append_lists' : True , 'append_unique' : True })
3250
-
3251
- meta ['automation_alias' ] = self .meta ['alias' ]
3252
- meta ['automation_uid' ] = self .meta ['uid' ]
3253
- meta ['tags' ] = tags_list
3254
-
3255
- script_name_base = script_name
3256
- script_name_ext = ''
3257
- if script_name != '' :
3258
- # separate name and extension
3259
- j = script_name .rfind ('.' )
3260
- if j >= 0 :
3261
- script_name_base = script_name [:j ]
3262
- script_name_ext = script_name [j :]
3263
-
3264
- meta ['script_name' ] = script_name_base
3265
-
3266
- ii ['meta' ] = meta
3267
- ii ['action' ] = 'add'
3268
-
3269
- use_yaml = True if not i .get ('json' , False ) else False
3270
-
3271
- if use_yaml :
3272
- ii ['yaml' ] = True
3273
-
3274
- ii ['automation' ] = 'script,5b4e0237da074764'
3275
-
3276
- for k in ['parsed_automation' , 'parsed_artifact' ]:
3277
- if k in ii :
3278
- del ii [k ]
3279
-
3280
- if artifact_repo is not None :
3281
- ii ['artifact' ] = utils .assemble_object2 (
3282
- artifact_repo ) + ':' + utils .assemble_object2 (artifact_obj )
3283
-
3284
- r_obj = self .action_object .access (ii )
3285
- if r_obj ['return' ] > 0 :
3286
- return r_obj
3287
-
3288
- new_script_path = r_obj ['path' ]
3289
-
3290
- if console :
3291
- logger .info ('Created script in {}' .format (new_script_path ))
3292
-
3293
- # Copy files from template (only if exist)
3294
- files = [
3295
- (template_path , 'README-extra.md' , '' ),
3296
- (template_path , 'customize.py' , '' ),
3297
- (template_path , 'main.py' , '' ),
3298
- (template_path , 'requirements.txt' , '' ),
3299
- (template_path , 'install_deps.bat' , '' ),
3300
- (template_path , 'install_deps.sh' , '' ),
3301
- (template_path , 'plot.bat' , '' ),
3302
- (template_path , 'plot.sh' , '' ),
3303
- (template_path , 'analyze.bat' , '' ),
3304
- (template_path , 'analyze.sh' , '' ),
3305
- (template_path , 'validate.bat' , '' ),
3306
- (template_path , 'validate.sh' , '' )
3307
- ]
3308
-
3309
- if script_name == '' :
3310
- files += [(template_path , 'run.bat' , '' ),
3311
- (template_path , 'run.sh' , '' )]
3312
- else :
3313
- if script_name_ext == '.bat' :
3314
- files += [(template_path , 'run.sh' , script_name_base + '.sh' )]
3315
- files += [('' , script_name , script_name )]
3316
-
3317
- else :
3318
- files += [(template_path , 'run.bat' ,
3319
- script_name_base + '.bat' )]
3320
- files += [('' , script_name , script_name_base + '.sh' )]
3321
-
3322
- for x in files :
3323
- path = x [0 ]
3324
- f1 = x [1 ]
3325
- f2 = x [2 ]
3326
-
3327
- if f2 == '' :
3328
- f2 = f1
3329
-
3330
- if path != '' :
3331
- f1 = os .path .join (path , f1 )
3332
-
3333
- if os .path .isfile (f1 ):
3334
- f2 = os .path .join (new_script_path , f2 )
3335
-
3336
- if console :
3337
- logger .info (' * Copying {} to {}' .format (f1 , f2 ))
3338
-
3339
- shutil .copyfile (f1 , f2 )
3340
-
3341
- return r_obj
3342
-
3343
3104
##########################################################################
3105
+
3344
3106
def _get_name_for_dynamic_variation_tag (script , variation_tag ):
3345
3107
'''
3346
3108
Returns the variation name in meta for the dynamic_variation_tag
@@ -5101,6 +4863,17 @@ def _update_env(env, key=None, value=None):
5101
4863
return {'return' : 0 }
5102
4864
5103
4865
4866
+ def docker_prune ():
4867
+ try :
4868
+ # Run the docker prune command with -a (removes all unused images, not
4869
+ # just dangling ones)
4870
+ result = subprocess .run (["docker" , "system" , "prune" , "-a" , "-f" ],
4871
+ capture_output = True , text = True , check = True )
4872
+ print ("Docker prune output:\n " , result .stdout )
4873
+ except subprocess .CalledProcessError as e :
4874
+ print ("Error while running Docker prune:\n " , e .stderr )
4875
+
4876
+
5104
4877
##########################################################################
5105
4878
def update_env_with_values (env , fail_on_not_found = False , extra_env = None ):
5106
4879
"""
0 commit comments