Skip to content

Commit 6486933

Browse files
committed
ignore stderr of 'pip list' in det_installed_python_packages function provided by Python easyblock
1 parent e52499f commit 6486933

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

easybuild/easyblocks/p/python.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,12 @@ def det_installed_python_packages(names_only=True, python_cmd=None):
152152
'--disable-pip-version-check',
153153
'--format', 'json',
154154
])
155-
res = run_shell_cmd(cmd, fail_on_error=False, hidden=True)
155+
# only check stdout, not stderr which might contain user facing warnings
156+
# (on deprecation of Python 2.7, for example)
157+
res = run_shell_cmd(cmd, split_stderr=True, fail_on_error=False, hidden=True)
156158
if res.exit_code:
157159
raise EasyBuildError(f'Failed to determine installed python packages: {res.output}')
158160

159-
# only check stdout, not stderr which might contain user facing warnings
160161
log.info(f'Got list of installed Python packages: {res.output}')
161162
pkgs = json.loads(res.output.strip())
162163
return [pkg['name'] for pkg in pkgs] if names_only else pkgs

test/easyblocks/easyblock_specific.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,14 @@ def test_det_installed_python_packages(self):
276276
Test det_installed_python_packages function providyed by PythonPackage easyblock
277277
"""
278278
pkg1 = None
279-
res = pythonpackage.det_installed_python_packages(python_cmd=sys.executable)
279+
res = python.det_installed_python_packages(python_cmd=sys.executable)
280280
# we can't make too much assumptions on which installed Python packages are found
281281
self.assertTrue(isinstance(res, list))
282282
if res:
283283
pkg1_name = res[0]
284284
self.assertTrue(isinstance(pkg1_name, str))
285285

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

297+
def mocked_run_shell_cmd_pip(cmd, **kwargs):
298+
stderr = None
299+
if "pip list" in cmd:
300+
output = '[{"name": "example", "version": "1.2.3"}]'
301+
stderr = "DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020"
302+
else:
303+
# unexpected command
304+
return None
305+
306+
return RunShellCmdResult(cmd=cmd, exit_code=0, output=output, stderr=stderr, work_dir=None,
307+
out_file=None, err_file=None, cmd_sh=None, thread_id=None, task_id=None)
308+
python.run_shell_cmd = mocked_run_shell_cmd_pip
309+
res = python.det_installed_python_packages(python_cmd=sys.executable)
310+
self.assertEqual(res, ['example'])
311+
297312
def test_det_py_install_scheme(self):
298313
"""Test det_py_install_scheme function provided by PythonPackage easyblock."""
299314
res = pythonpackage.det_py_install_scheme(sys.executable)

0 commit comments

Comments
 (0)