Fix support for auto package detection for standalone scripts

This commit is contained in:
allegroai 2019-11-15 21:59:50 +02:00
parent d19fde7041
commit 15683b5b43

View File

@ -76,7 +76,6 @@ class ScriptRequirements(object):
modules = ImportedModules() modules = ImportedModules()
try_imports = set() try_imports = set()
local_mods = list() local_mods = list()
cur_dir = project_path # os.getcwd()
ignore_paths = collections.defaultdict(set) ignore_paths = collections.defaultdict(set)
if not ignores: if not ignores:
ignore_paths[project_path].add('.git') ignore_paths[project_path].add('.git')
@ -85,6 +84,14 @@ class ScriptRequirements(object):
parent_dir = os.path.dirname(path) parent_dir = os.path.dirname(path)
ignore_paths[parent_dir].add(os.path.basename(path)) ignore_paths[parent_dir].add(os.path.basename(path))
if os.path.isfile(project_path):
fake_path = Path(project_path).name
with open(project_path, 'rb') as f:
fmodules, try_ipts = file_import_modules(fake_path, f.read())
modules |= fmodules
try_imports |= try_ipts
else:
cur_dir = project_path # os.getcwd()
for dirpath, dirnames, files in os.walk(project_path, followlinks=True): for dirpath, dirnames, files in os.walk(project_path, followlinks=True):
if dirpath in ignore_paths: if dirpath in ignore_paths:
dirnames[:] = [d for d in dirnames dirnames[:] = [d for d in dirnames
@ -457,7 +464,8 @@ class ScriptInfo(object):
# create requirements if backend supports requirements # create requirements if backend supports requirements
# if jupyter is present, requirements will be created in the background, when saving a snapshot # if jupyter is present, requirements will be created in the background, when saving a snapshot
if not jupyter_filepath and Session.check_min_api_version('2.2'): if not jupyter_filepath and Session.check_min_api_version('2.2'):
script_requirements = ScriptRequirements(Path(repo_root).as_posix()) script_requirements = ScriptRequirements(
Path(repo_root).as_posix() if repo_info.url else script_path.as_posix())
if create_requirements: if create_requirements:
requirements = script_requirements.get_requirements() requirements = script_requirements.get_requirements()
else: else: