Skip to content

Commit c742f59

Browse files
authored
Merge pull request #3145 from ComputeCanada/dont-add-fileless-paths
only add useful entries for $CPATH, $(LD_)LIBRARY_PATH and $PATH
2 parents fa403ed + c679aa7 commit c742f59

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

easybuild/framework/easyblock.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,9 @@ def make_module_req(self):
12891289
note += "for paths are skipped for the statements below due to dry run"
12901290
lines.append(self.module_generator.comment(note))
12911291

1292+
# for these environment variables, the corresponding subdirectory must include at least one file
1293+
keys_requiring_files = ('CPATH', 'LD_LIBRARY_PATH', 'LIBRARY_PATH', 'PATH')
1294+
12921295
for key in sorted(requirements):
12931296
if self.dry_run:
12941297
self.dry_run_msg(" $%s: %s" % (key, ', '.join(requirements[key])))
@@ -1301,6 +1304,17 @@ def make_module_req(self):
13011304
# only use glob if the string is non-empty
13021305
if path and not self.dry_run:
13031306
paths = sorted(glob.glob(path))
1307+
if paths and key in keys_requiring_files:
1308+
self.log.info("Only retaining paths for %s that include at least one file: %s", key, paths)
1309+
# only retain paths that include at least one file
1310+
retained_paths = []
1311+
for path in paths:
1312+
full_path = os.path.join(self.installdir, path)
1313+
if os.path.isdir(full_path):
1314+
if any(os.path.isfile(os.path.join(full_path, x)) for x in os.listdir(full_path)):
1315+
retained_paths.append(path)
1316+
self.log.info("Retained paths for %s: %s", key, retained_paths)
1317+
paths = retained_paths
13041318
else:
13051319
# empty string is a valid value here (i.e. to prepend the installation prefix, cfr $CUDA_HOME)
13061320
paths = [path]

test/framework/easyblock.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ def test_make_module_req(self):
318318
open(os.path.join(eb.installdir, 'foo.jar'), 'w').write('foo.jar')
319319
open(os.path.join(eb.installdir, 'bla.jar'), 'w').write('bla.jar')
320320
os.mkdir(os.path.join(eb.installdir, 'bin'))
321+
os.mkdir(os.path.join(eb.installdir, 'bin', 'testdir'))
322+
os.mkdir(os.path.join(eb.installdir, 'sbin'))
321323
os.mkdir(os.path.join(eb.installdir, 'share'))
322324
os.mkdir(os.path.join(eb.installdir, 'share', 'man'))
323325
# this is not a path that should be picked up
@@ -329,17 +331,35 @@ def test_make_module_req(self):
329331
self.assertTrue(re.search(r"^prepend-path\s+CLASSPATH\s+\$root/bla.jar$", guess, re.M))
330332
self.assertTrue(re.search(r"^prepend-path\s+CLASSPATH\s+\$root/foo.jar$", guess, re.M))
331333
self.assertTrue(re.search(r"^prepend-path\s+MANPATH\s+\$root/share/man$", guess, re.M))
332-
self.assertTrue(re.search(r"^prepend-path\s+PATH\s+\$root/bin$", guess, re.M))
334+
# bin/ is not added to $PATH if it doesn't include files
335+
self.assertFalse(re.search(r"^prepend-path\s+PATH\s+\$root/bin$", guess, re.M))
336+
self.assertFalse(re.search(r"^prepend-path\s+PATH\s+\$root/sbin$", guess, re.M))
337+
# no include/ subdirectory, so no $CPATH update statement
333338
self.assertFalse(re.search(r"^prepend-path\s+CPATH\s+.*$", guess, re.M))
334339
elif get_module_syntax() == 'Lua':
335340
self.assertTrue(re.search(r'^prepend_path\("CLASSPATH", pathJoin\(root, "bla.jar"\)\)$', guess, re.M))
336341
self.assertTrue(re.search(r'^prepend_path\("CLASSPATH", pathJoin\(root, "foo.jar"\)\)$', guess, re.M))
337342
self.assertTrue(re.search(r'^prepend_path\("MANPATH", pathJoin\(root, "share/man"\)\)$', guess, re.M))
338-
self.assertTrue(re.search(r'^prepend_path\("PATH", pathJoin\(root, "bin"\)\)$', guess, re.M))
343+
# bin/ is not added to $PATH if it doesn't include files
344+
self.assertFalse(re.search(r'^prepend_path\("PATH", pathJoin\(root, "bin"\)\)$', guess, re.M))
345+
self.assertFalse(re.search(r'^prepend_path\("PATH", pathJoin\(root, "sbin"\)\)$', guess, re.M))
346+
# no include/ subdirectory, so no $CPATH update statement
339347
self.assertFalse(re.search(r'^prepend_path\("CPATH", .*\)$', guess, re.M))
340348
else:
341349
self.assertTrue(False, "Unknown module syntax: %s" % get_module_syntax())
342350

351+
# check that bin is only added to PATH if there are files in there
352+
open(os.path.join(eb.installdir, 'bin', 'test'), 'w').write('test')
353+
guess = eb.make_module_req()
354+
if get_module_syntax() == 'Tcl':
355+
self.assertTrue(re.search(r"^prepend-path\s+PATH\s+\$root/bin$", guess, re.M))
356+
self.assertFalse(re.search(r"^prepend-path\s+PATH\s+\$root/sbin$", guess, re.M))
357+
elif get_module_syntax() == 'Lua':
358+
self.assertTrue(re.search(r'^prepend_path\("PATH", pathJoin\(root, "bin"\)\)$', guess, re.M))
359+
self.assertFalse(re.search(r'^prepend_path\("PATH", pathJoin\(root, "sbin"\)\)$', guess, re.M))
360+
else:
361+
self.assertTrue(False, "Unknown module syntax: %s" % get_module_syntax())
362+
343363
# check for behavior when a string value is used as dict value by make_module_req_guesses
344364
eb.make_module_req_guess = lambda: {'PATH': 'bin'}
345365
txt = eb.make_module_req()

0 commit comments

Comments
 (0)