Fix clearml-task add Task.init() call in the incorrect location when single local file is used

This commit is contained in:
allegroai 2022-02-02 15:35:57 +02:00
parent 9f77b3275f
commit 34fc80b039
2 changed files with 12 additions and 4 deletions

View File

@ -173,7 +173,7 @@ class CreateAndPopulate(object):
name=self.task_name, name=self.task_name,
project=Task.get_project_id(self.project_name), project=Task.get_project_id(self.project_name),
type=str(self.task_type or Task.TaskTypes.training), type=str(self.task_type or Task.TaskTypes.training),
) ) # type: dict
if self.output_uri: if self.output_uri:
task_state['output'] = dict(destination=self.output_uri) task_state['output'] = dict(destination=self.output_uri)
else: else:
@ -279,6 +279,7 @@ class CreateAndPopulate(object):
else: else:
script_entry = os.path.abspath(script_entry) script_entry = os.path.abspath(script_entry)
idx_a = 0 idx_a = 0
lines = None
# find the right entry for the patch if we have a local file (basically after __future__ # find the right entry for the patch if we have a local file (basically after __future__
if local_entry_file: if local_entry_file:
with open(local_entry_file, 'rt') as f: with open(local_entry_file, 'rt') as f:
@ -308,13 +309,19 @@ class CreateAndPopulate(object):
"+Task.init()\n" \ "+Task.init()\n" \
"+\n".format( "+\n".format(
script_entry=script_entry, idx_a=idx_a, idx_b=idx_a + 1) script_entry=script_entry, idx_a=idx_a, idx_b=idx_a + 1)
elif local_entry_file and lines:
# if we are here it means we do not have a git diff, but a single script file
init_lines = ["from clearml import Task\n", "Task.init()\n\n"]
task_state['script']['diff'] = ''.join(lines[:idx_a] + init_lines + lines[idx_a:])
# no need to add anything, we patched it.
task_init_patch = ""
else: else:
# Add Task.init call # Add Task.init call
task_init_patch += \ task_init_patch += \
"from clearml import Task\n" \ "from clearml import Task\n" \
"Task.init()\n\n" "Task.init()\n\n"
# make sure we add the dif at the end of the current diff # make sure we add the diff at the end of the current diff
task_state['script']['diff'] = task_state['script'].get('diff', '') task_state['script']['diff'] = task_state['script'].get('diff', '')
if task_state['script']['diff'] and not task_state['script']['diff'].endswith('\n'): if task_state['script']['diff'] and not task_state['script']['diff'].endswith('\n'):
task_state['script']['diff'] += '\n' task_state['script']['diff'] += '\n'
@ -544,7 +551,8 @@ if __name__ == '__main__':
:param a_function: A global function to convert into a standalone Task :param a_function: A global function to convert into a standalone Task
:param function_kwargs: Optional, provide subset of function arguments and default values to expose. :param function_kwargs: Optional, provide subset of function arguments and default values to expose.
If not provided automatically take all function arguments & defaults If not provided automatically take all function arguments & defaults
:param function_input_artifacts: Optional, pass input arguments to the function from other Tasks's output artifact. :param function_input_artifacts: Optional, pass input arguments to the function from
other Tasks's output artifact.
Example argument named `numpy_matrix` from Task ID `aabbcc` artifact name `answer`: Example argument named `numpy_matrix` from Task ID `aabbcc` artifact name `answer`:
{'numpy_matrix': 'aabbcc.answer'} {'numpy_matrix': 'aabbcc.answer'}
:param function_return: Provide a list of names for all the results. :param function_return: Provide a list of names for all the results.

View File

@ -116,7 +116,7 @@ def cli():
raise_on_missing_entries=True, raise_on_missing_entries=True,
verbose=True, verbose=True,
) )
# verify args # verify args before creating the Task
create_populate.update_task_args(args.args) create_populate.update_task_args(args.args)
print('Creating new task') print('Creating new task')