Skip to content

Commit 8f02741

Browse files
authored
Merge pull request #3297 from ComputeCanada/find_glob_pattern
added function find_glob_pattern to filetools.py
2 parents 7c1c288 + 4832d2d commit 8f02741

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

easybuild/tools/filetools.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,18 @@ def find_easyconfigs(path, ignore_dirs=None):
758758
return files
759759

760760

761+
def find_glob_pattern(glob_pattern, fail_on_no_match=True):
762+
"""Find unique file/dir matching glob_pattern (raises error if more than one match is found)"""
763+
if build_option('extended_dry_run'):
764+
return glob_pattern
765+
res = glob.glob(glob_pattern)
766+
if len(res) == 0 and not fail_on_no_match:
767+
return None
768+
if len(res) != 1:
769+
raise EasyBuildError("Was expecting exactly one match for '%s', found %d: %s", glob_pattern, len(res), res)
770+
return res[0]
771+
772+
761773
def search_file(paths, query, short=False, ignore_dirs=None, silent=False, filename_only=False, terse=False,
762774
case_sensitive=False):
763775
"""

test/framework/filetools.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
@author: Kenneth Hoste (Ghent University)
3030
@author: Stijn De Weirdt (Ghent University)
3131
@author: Ward Poelmans (Ghent University)
32+
@author: Maxime Boissonneault (Compute Canada, Universite Laval)
3233
"""
3334
import datetime
3435
import glob
@@ -148,6 +149,29 @@ def test_find_base_dir(self):
148149
os.chdir(tmpdir)
149150
self.assertTrue(os.path.samefile(foodir, ft.find_base_dir()))
150151

152+
def test_find_glob_pattern(self):
153+
"""test find_glob_pattern function"""
154+
tmpdir = tempfile.mkdtemp()
155+
os.mkdir(os.path.join(tmpdir, 'python2.7'))
156+
os.mkdir(os.path.join(tmpdir, 'python2.7', 'include'))
157+
os.mkdir(os.path.join(tmpdir, 'python3.5m'))
158+
os.mkdir(os.path.join(tmpdir, 'python3.5m', 'include'))
159+
160+
self.assertEqual(ft.find_glob_pattern(os.path.join(tmpdir, 'python2.7*')),
161+
os.path.join(tmpdir, 'python2.7'))
162+
self.assertEqual(ft.find_glob_pattern(os.path.join(tmpdir, 'python2.7*', 'include')),
163+
os.path.join(tmpdir, 'python2.7', 'include'))
164+
self.assertEqual(ft.find_glob_pattern(os.path.join(tmpdir, 'python3.5*')),
165+
os.path.join(tmpdir, 'python3.5m'))
166+
self.assertEqual(ft.find_glob_pattern(os.path.join(tmpdir, 'python3.5*', 'include')),
167+
os.path.join(tmpdir, 'python3.5m', 'include'))
168+
self.assertEqual(ft.find_glob_pattern(os.path.join(tmpdir, 'python3.6*'), False), None)
169+
self.assertErrorRegex(EasyBuildError, "Was expecting exactly", ft.find_glob_pattern,
170+
os.path.join(tmpdir, 'python3.6*'))
171+
self.assertErrorRegex(EasyBuildError, "Was expecting exactly", ft.find_glob_pattern,
172+
os.path.join(tmpdir, 'python*'))
173+
174+
151175
def test_encode_class_name(self):
152176
"""Test encoding of class names."""
153177
for (class_name, encoded_class_name) in self.class_names:

0 commit comments

Comments
 (0)