def _generate_python_code(self):
if self.use_dill:
pickling_library = 'dill'
else:
pickling_library = 'pickle'
fn = self.python_callable
# dont try to read pickle if we didnt pass anything
if self._pass_op_args():
load_args_line = 'with open(sys.argv[1], "rb") as f: arg_dict = {}.load(f)'.format(pickling_library)
else:
load_args_line = 'arg_dict = {"args": [], "kwargs": {}}'
# no indents in original code so we can accept any type of indents in the original function
# we deserialize args, call function, serialize result if necessary
return dedent("""\
import {pickling_library}
import sys
{load_args_code}
args = arg_dict["args"]
kwargs = arg_dict["kwargs"]
with open(sys.argv[3], 'r') as f: virtualenv_string_args = list(map(lambda x: x.strip(), list(f)))
{python_callable_lines}
res = {python_callable_name}(*args, **kwargs)
with open(sys.argv[2], 'wb') as f: res is not None and {pickling_library}.dump(res, f)
""").format(
load_args_code=load_args_line,
python_callable_lines=dedent(inspect.getsource(fn)),
python_callable_name=fn.__name__,
pickling_library=pickling_library)
self.log.info("Done.")
评论列表
文章目录