From 8a73a18fdd2ccbf1f40a8e96fa80993dbd7f2e0d Mon Sep 17 00:00:00 2001 From: Vasileios Karakasis Date: Mon, 13 Jul 2020 23:39:31 +0200 Subject: [PATCH 1/2] Utility function for retrieving the Cray CDT version --- reframe/utility/os_ext.py | 18 ++++++++++++++++++ unittests/test_utility.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/reframe/utility/os_ext.py b/reframe/utility/os_ext.py index b32d58efde..4702fadf4a 100644 --- a/reframe/utility/os_ext.py +++ b/reframe/utility/os_ext.py @@ -456,3 +456,21 @@ def unique_abs_paths(paths, prune_children=True): # FIXME: This should be performed using the minus operator of # `OrderedSet` once #1165 is fixed. return [p for p in unique_paths if p not in children] + + +def cray_cdt_version(): + '''Return the Cray CDT version or :class:`None` for non-Cray systems''' + rcfile = os.getenv('MODULERCFILE', '/opt/cray/pe/cdt/default/modulerc') + try: + with open(rcfile) as fp: + header = fp.readline() + if not header: + return None + + match = re.search(r'^#%Module CDT (\S+)', header) + if not match: + return None + + return match.group(1) + except OSError: + return None diff --git a/unittests/test_utility.py b/unittests/test_utility.py index bb813ca342..057c98b1f8 100644 --- a/unittests/test_utility.py +++ b/unittests/test_utility.py @@ -1136,3 +1136,38 @@ def test_unique_abs_paths(self): with pytest.raises(TypeError): os_ext.unique_abs_paths(None) + + +def test_cray_cdt_version(tmp_path, monkeypatch): + # Mock up a CDT file + rcfile = tmp_path / 'rcfile' + with open(rcfile, 'w') as fp: + fp.write('#%Module CDT 20.06\nblah blah\n') + + monkeypatch.setenv('MODULERCFILE', str(tmp_path / 'rcfile')) + assert os_ext.cray_cdt_version() == '20.06' + + +def test_cray_cdt_version_unknown_fmt(tmp_path, monkeypatch): + # Mock up a CDT file + rcfile = tmp_path / 'rcfile' + with open(rcfile, 'w') as fp: + fp.write('random stuff') + + monkeypatch.setenv('MODULERCFILE', str(tmp_path / 'rcfile')) + assert os_ext.cray_cdt_version() is None + + +def test_cray_cdt_version_empty_file(tmp_path, monkeypatch): + # Mock up a CDT file + rcfile = tmp_path / 'rcfile' + rcfile.touch() + monkeypatch.setenv('MODULERCFILE', str(tmp_path / 'rcfile')) + assert os_ext.cray_cdt_version() is None + + +def test_cray_cdt_version_no_such_file(tmp_path, monkeypatch): + # Mock up a CDT file + rcfile = tmp_path / 'rcfile' + monkeypatch.setenv('MODULERCFILE', str(tmp_path / 'rcfile')) + assert os_ext.cray_cdt_version() is None From a526e3529a19d98fe1c58d65d1e6aa2e153bef5d Mon Sep 17 00:00:00 2001 From: Vasileios Karakasis Date: Tue, 14 Jul 2020 14:41:14 +0200 Subject: [PATCH 2/2] Address PR comments --- unittests/test_utility.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/unittests/test_utility.py b/unittests/test_utility.py index 057c98b1f8..aa9d715f53 100644 --- a/unittests/test_utility.py +++ b/unittests/test_utility.py @@ -1144,7 +1144,7 @@ def test_cray_cdt_version(tmp_path, monkeypatch): with open(rcfile, 'w') as fp: fp.write('#%Module CDT 20.06\nblah blah\n') - monkeypatch.setenv('MODULERCFILE', str(tmp_path / 'rcfile')) + monkeypatch.setenv('MODULERCFILE', str(rcfile)) assert os_ext.cray_cdt_version() == '20.06' @@ -1154,7 +1154,7 @@ def test_cray_cdt_version_unknown_fmt(tmp_path, monkeypatch): with open(rcfile, 'w') as fp: fp.write('random stuff') - monkeypatch.setenv('MODULERCFILE', str(tmp_path / 'rcfile')) + monkeypatch.setenv('MODULERCFILE', str(rcfile)) assert os_ext.cray_cdt_version() is None @@ -1162,12 +1162,12 @@ def test_cray_cdt_version_empty_file(tmp_path, monkeypatch): # Mock up a CDT file rcfile = tmp_path / 'rcfile' rcfile.touch() - monkeypatch.setenv('MODULERCFILE', str(tmp_path / 'rcfile')) + monkeypatch.setenv('MODULERCFILE', str(rcfile)) assert os_ext.cray_cdt_version() is None def test_cray_cdt_version_no_such_file(tmp_path, monkeypatch): # Mock up a CDT file rcfile = tmp_path / 'rcfile' - monkeypatch.setenv('MODULERCFILE', str(tmp_path / 'rcfile')) + monkeypatch.setenv('MODULERCFILE', str(rcfile)) assert os_ext.cray_cdt_version() is None