When using "detect_with_pip_freeze" make sure that "package @ file://" lines are replaced with "package==x.y.z" because local file will probably not be available

This commit is contained in:
allegroai 2020-10-12 11:03:33 +03:00
parent 89034233ed
commit 07bdc37dcb
2 changed files with 26 additions and 4 deletions

View File

@ -4,8 +4,30 @@ from .util import get_command_output
def pip_freeze(): def pip_freeze():
req_lines = []
local_packages = []
try: try:
return get_command_output([sys.executable, "-m", "pip", "freeze"]).splitlines() req_lines = get_command_output([sys.executable, "-m", "pip", "freeze"]).splitlines()
# fix "package @ file://" from pip freeze to "package"
for i, r in enumerate(req_lines):
parts = r.split('@', 1)
if parts and len(parts) == 2 and parts[1].strip().lower().startswith('file://'):
req_lines[i] = parts[0]
local_packages.append((i, parts[0].strip()))
# if we found local packages, at least get their versions (using pip list)
if local_packages:
# noinspection PyBroadException
try:
list_lines = get_command_output(
[sys.executable, "-m", "pip", "list", "--format", "freeze"]).splitlines()
for index, name in local_packages:
line = [r for r in list_lines if r.strip().startswith(name+'==')]
if not line:
continue
line = line[0]
req_lines[index] = line.strip()
except Exception:
pass
except Exception as ex: except Exception as ex:
print('Failed calling "pip freeze": {}'.format(str(ex))) print('Failed calling "pip freeze": {}'.format(str(ex)))
return [] return req_lines

View File

@ -48,7 +48,7 @@ from ...debugging.log import LoggerRoot
from ...storage.helper import StorageHelper, StorageError from ...storage.helper import StorageHelper, StorageError
from .access import AccessMixin from .access import AccessMixin
from .log import TaskHandler from .log import TaskHandler
from .repo import ScriptInfo from .repo import ScriptInfo, pip_freeze
from .repo.util import get_command_output from .repo.util import get_command_output
from ...config import config, PROC_MASTER_ID_ENV_VAR, SUPPRESS_UPDATE_MESSAGE_ENV_VAR from ...config import config, PROC_MASTER_ID_ENV_VAR, SUPPRESS_UPDATE_MESSAGE_ENV_VAR
@ -312,7 +312,7 @@ class Task(IdObjectBase, AccessMixin, SetupUploadMixin):
if config.get('development.detect_with_pip_freeze', False): if config.get('development.detect_with_pip_freeze', False):
conda_requirements = "" conda_requirements = ""
requirements = '# Python ' + sys.version.replace('\n', ' ').replace('\r', ' ') + '\n\n'\ requirements = '# Python ' + sys.version.replace('\n', ' ').replace('\r', ' ') + '\n\n'\
+ get_command_output([sys.executable, "-m", "pip", "freeze"]) + "\n".join(pip_freeze())
else: else:
requirements, conda_requirements = script_requirements.get_requirements( requirements, conda_requirements = script_requirements.get_requirements(
entry_point_filename=entry_point_filename) entry_point_filename=entry_point_filename)