Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions easybuild/easyblocks/p/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,12 @@ def det_installed_python_packages(names_only=True, python_cmd=None):
'--disable-pip-version-check',
'--format', 'json',
])
res = run_shell_cmd(cmd, fail_on_error=False, hidden=True)
# only check stdout, not stderr which might contain user facing warnings
# (on deprecation of Python 2.7, for example)
res = run_shell_cmd(cmd, split_stderr=True, fail_on_error=False, hidden=True)
if res.exit_code:
raise EasyBuildError(f'Failed to determine installed python packages: {res.output}')

# only check stdout, not stderr which might contain user facing warnings
log.info(f'Got list of installed Python packages: {res.output}')
pkgs = json.loads(res.output.strip())
return [pkg['name'] for pkg in pkgs] if names_only else pkgs
Expand Down
19 changes: 17 additions & 2 deletions test/easyblocks/easyblock_specific.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,14 @@ def test_det_installed_python_packages(self):
Test det_installed_python_packages function providyed by PythonPackage easyblock
"""
pkg1 = None
res = pythonpackage.det_installed_python_packages(python_cmd=sys.executable)
res = python.det_installed_python_packages(python_cmd=sys.executable)
# we can't make too much assumptions on which installed Python packages are found
self.assertTrue(isinstance(res, list))
if res:
pkg1_name = res[0]
self.assertTrue(isinstance(pkg1_name, str))

res_detailed = pythonpackage.det_installed_python_packages(python_cmd=sys.executable, names_only=False)
res_detailed = python.det_installed_python_packages(python_cmd=sys.executable, names_only=False)
self.assertTrue(isinstance(res_detailed, list))
if res_detailed:
pkg1 = res_detailed[0]
Expand All @@ -294,6 +294,21 @@ def test_det_installed_python_packages(self):
ver = pkg1['version']
self.assertTrue(regex.match(ver), f"Pattern {regex.pattern} matches for pkg version: {ver}")

def mocked_run_shell_cmd_pip(cmd, **kwargs):
stderr = None
if "pip list" in cmd:
output = '[{"name": "example", "version": "1.2.3"}]'
stderr = "DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020"
else:
# unexpected command
return None

return RunShellCmdResult(cmd=cmd, exit_code=0, output=output, stderr=stderr, work_dir=None,
out_file=None, err_file=None, cmd_sh=None, thread_id=None, task_id=None)
python.run_shell_cmd = mocked_run_shell_cmd_pip
res = python.det_installed_python_packages(python_cmd=sys.executable)
self.assertEqual(res, ['example'])

def test_det_py_install_scheme(self):
"""Test det_py_install_scheme function provided by PythonPackage easyblock."""
res = pythonpackage.det_py_install_scheme(sys.executable)
Expand Down