Skip to content

Commit 826ec33

Browse files
authored
Fix mlc test script (#303)
1 parent 63f68f3 commit 826ec33

File tree

8 files changed

+44
-273
lines changed

8 files changed

+44
-273
lines changed

.github/scripts/process_individual_tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@
3535
ret = mlc.access(ii)
3636
if ret['return'] > 0:
3737
raise Exception(r['error'])
38+
ii = {'action': 'rm', 'target': 'cache', 'f': True}
39+
ret = mlc.access(ii)

.github/workflows/check-broken-links.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
name: "Check .md README files for broken links"
2-
32
on:
43
pull_request:
54
branches:
@@ -13,7 +12,6 @@ jobs:
1312
steps:
1413
- uses: actions/checkout@v4
1514

16-
1715
# Checks the status of hyperlinks in .md files in verbose mode
1816
- name: Check links
1917
uses: gaurav-nelson/github-action-markdown-link-check@v1

.github/workflows/test-mlc-script-features.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ jobs:
4444
mlc search cache --tags=get,dataset,preprocessed,imagenet,-_NCHW
4545
mlc search cache --tags=get,dataset,preprocessed,imagenet,-_NHWC
4646
# Tests run-scripts on windows,ubuntu,macos
47-
mlcr get,wkhtmltopdf
47+
48+
- name: Test native script name variants
49+
run: |
50+
mlcr get,wkhtmltopdf --quiet
4851
4952
- name: Test versions
5053
continue-on-error: true

automation/script/module.py

Lines changed: 29 additions & 256 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,7 @@ def _run(self, i):
502502
ii = {}
503503
ii['tags'] = tags_string
504504
ii['out'] = None
505-
for key in ["automation", "parsed_automation",
506-
"artifact", "parsed_artifact"]:
505+
for key in ["automation", "artifact", "item", "details"]:
507506
if i.get(key):
508507
ii[key] = i[key]
509508

@@ -2792,13 +2791,19 @@ def search(self, i):
27922791
# Find MLC script(s) based on thier tags to get their meta (can be more than 1)
27932792
# Then check if variations exists inside meta
27942793

2795-
i['tags'] = ','.join(script_tags)
2794+
ii = {}
2795+
ii['tags'] = ','.join(script_tags)
27962796

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]
27992805

2800-
i['target_name'] = "script"
2801-
r = super(ScriptAutomation, self).search(i)
2806+
r = super(ScriptAutomation, self).search(ii)
28022807
if r['return'] > 0:
28032808
return r
28042809

@@ -2901,7 +2906,6 @@ def test(self, i):
29012906
# Find script item(s)
29022907
i['out'] = None
29032908
r = self.search(i)
2904-
29052909
if r['return'] > 0:
29062910
return r
29072911

@@ -2910,7 +2914,6 @@ def test(self, i):
29102914
for script_item in lst:
29112915
path = script_item.path
29122916
meta = script_item.meta
2913-
original_meta = script_item.original_meta
29142917

29152918
alias = meta.get('alias', '')
29162919
uid = meta.get('uid', '')
@@ -2970,14 +2973,14 @@ def test(self, i):
29702973
if given_variations:
29712974
v_split = []
29722975
run_variations = []
2973-
for i, v in enumerate(given_variations):
2976+
for v in given_variations:
29742977
v_split = v.split(",")
2975-
for t in v_split:
2978+
for index, t in enumerate(v_split):
29762979
if not t.startswith("_"):
29772980
# variations must begin with _. We
29782981
# support both with and without _
29792982
# in the meta
2980-
given_variations[i] = f"_{t}"
2983+
v_split[index] = f"_{t}"
29812984
if v_split:
29822985
run_variations.append(
29832986
",".join(v_split))
@@ -3018,7 +3021,8 @@ def test(self, i):
30183021
r = self.action_object.access(ii)
30193022
if r['return'] > 0:
30203023
return r
3021-
3024+
if is_true(i.get('docker_prune', '')):
3025+
docker_prune()
30223026
return {'return': 0, 'list': lst}
30233027

30243028
############################################################
@@ -3097,250 +3101,8 @@ def native_run(self, i):
30973101

30983102
return {'return': 0, 'return_code': rc}
30993103

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-
33433104
##########################################################################
3105+
33443106
def _get_name_for_dynamic_variation_tag(script, variation_tag):
33453107
'''
33463108
Returns the variation name in meta for the dynamic_variation_tag
@@ -5101,6 +4863,17 @@ def _update_env(env, key=None, value=None):
51014863
return {'return': 0}
51024864

51034865

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+
51044877
##########################################################################
51054878
def update_env_with_values(env, fail_on_not_found=False, extra_env=None):
51064879
"""

0 commit comments

Comments
 (0)