Fix run as module (issue #359)

This commit is contained in:
allegroai 2021-05-26 18:29:53 +03:00
parent f8a76278bf
commit b1ce3e0653

View File

@ -846,18 +846,25 @@ class ScriptInfo(object):
if cls.is_running_from_module(): if cls.is_running_from_module():
argvs = '' argvs = ''
git_root = os.path.abspath(script_dict['repo_root']) if script_dict['repo_root'] else None git_root = os.path.abspath(str(script_dict['repo_root'])) if script_dict['repo_root'] else None
for a in sys.argv[1:]: for a in sys.argv[1:]:
if git_root and os.path.exists(a): if git_root and os.path.exists(a):
# check if common to project: # check if common to project:
a_abs = os.path.abspath(a) a_abs = os.path.abspath(a)
if os.path.commonpath([a_abs, git_root]) == git_root: if os.path.commonpath([a_abs, git_root]) == git_root:
# adjust path relative to working dir inside git repo # adjust path relative to working dir inside git repo
a = ' ' + os.path.relpath(a_abs, os.path.join(git_root, script_dict['working_dir'])) a = ' ' + os.path.relpath(
a_abs, os.path.join(git_root, str(script_dict['working_dir'])))
argvs += ' {}'.format(a) argvs += ' {}'.format(a)
# noinspection PyBroadException
try:
module_name = vars(sys.modules['__main__'])['__spec__'].name
except Exception:
module_name = vars(sys.modules['__main__'])['__package__']
# update the script entry point to match the real argv and module call # update the script entry point to match the real argv and module call
script_dict['entry_point'] = '-m {}{}'.format( script_dict['entry_point'] = '-m {}{}'.format(module_name, (' ' + argvs) if argvs else '')
vars(sys.modules['__main__'])['__package__'], (' ' + argvs) if argvs else '')
except Exception: except Exception:
pass pass
return script_dict return script_dict