Skip to content

Commit 670ea75

Browse files
committed
Use os.pathsep consistently
1 parent ca3fafe commit 670ea75

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

easybuild/tools/modules.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,10 +1689,10 @@ def curr_module_paths(normalize=False, clean=True):
16891689
"""
16901690
if clean:
16911691
# avoid empty or nonexistent paths, which don't make any sense
1692-
module_paths = (p for p in os.environ.get('MODULEPATH', '').split(':') if p and os.path.exists(p))
1692+
module_paths = (p for p in os.environ.get('MODULEPATH', '').split(os.pathsep) if p and os.path.exists(p))
16931693
else:
16941694
modulepath = os.environ.get('MODULEPATH')
1695-
module_paths = [] if modulepath is None else modulepath.split(':')
1695+
module_paths = [] if modulepath is None else modulepath.split(os.pathsep)
16961696
if normalize:
16971697
module_paths = (normalize_path(p) for p in module_paths)
16981698
return list(module_paths)
@@ -1702,7 +1702,7 @@ def mk_module_path(paths):
17021702
"""
17031703
Create a string representing the list of module paths.
17041704
"""
1705-
return ':'.join(paths)
1705+
return os.pathsep.join(paths)
17061706

17071707

17081708
def avail_modules_tools():

test/framework/modules.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -497,12 +497,12 @@ def test_curr_module_paths(self):
497497
self.assertEqual(curr_module_paths(), [])
498498
self.assertEqual(curr_module_paths(clean=False), [''])
499499

500-
os.environ['MODULEPATH'] = '%s:%s:%s' % (test1, test2, test3)
500+
os.environ['MODULEPATH'] = os.pathsep.join([test1, test2, test3])
501501
self.assertEqual(curr_module_paths(), [test1, test2, test3])
502502
self.assertEqual(curr_module_paths(clean=False), [test1, test2, test3])
503503

504504
# empty entries and non-existing directories are filtered out
505-
os.environ['MODULEPATH'] = '/doesnotexist:%s::%s:' % (test2, test1)
505+
os.environ['MODULEPATH'] = os.pathsep.join(['/doesnotexist', test2, '', test1, ''])
506506
self.assertEqual(curr_module_paths(), [test2, test1])
507507
# Disabling the clean returns them
508508
self.assertEqual(curr_module_paths(clean=False), ['/doesnotexist', test2, '', test1, ''])
@@ -542,7 +542,7 @@ def test_check_module_path(self):
542542
self.assertEqual(os.environ['MODULEPATH'], os.pathsep.join([mod_install_dir, test1, test2]))
543543

544544
# check behaviour if non-existing directories are included in $MODULEPATH
545-
os.environ['MODULEPATH'] = '%s:/does/not/exist:%s' % (test3, test2)
545+
os.environ['MODULEPATH'] = os.pathsep.join([test3, '/does/not/exist', test2])
546546
modtool.check_module_path()
547547
# non-existing dir is filtered from mod_paths, but stays in $MODULEPATH
548548
self.assertEqual(modtool.mod_paths, [mod_install_dir, test1, test3, test2])
@@ -567,11 +567,11 @@ def test_check_module_path_hmns(self):
567567
doesnotexist = os.path.join(self.test_prefix, 'doesnotexist')
568568
self.assertNotExists(doesnotexist)
569569

570-
os.environ['MODULEPATH'] = '%s:%s' % (core_mod_dir, doesnotexist)
570+
os.environ['MODULEPATH'] = os.pathsep.join([core_mod_dir, doesnotexist])
571571
modtool = modules_tool()
572572

573573
self.assertEqual(modtool.mod_paths, [os.path.dirname(core_mod_dir), core_mod_dir])
574-
self.assertEqual(os.environ['MODULEPATH'], '%s:%s:%s' % (top_mod_dir, core_mod_dir, doesnotexist))
574+
self.assertEqual(os.environ['MODULEPATH'], os.pathsep.join([top_mod_dir, core_mod_dir, doesnotexist]))
575575

576576
# hack prepend_module_path to make sure it's not called again if check_module_path is called again;
577577
# prepend_module_path is fairly expensive, so should be avoided,
@@ -585,7 +585,7 @@ def broken_prepend_module_path(*args, **kwargs):
585585
modtool.check_module_path()
586586

587587
self.assertEqual(modtool.mod_paths, [os.path.dirname(core_mod_dir), core_mod_dir])
588-
self.assertEqual(os.environ['MODULEPATH'], '%s:%s:%s' % (top_mod_dir, core_mod_dir, doesnotexist))
588+
self.assertEqual(os.environ['MODULEPATH'], os.pathsep.join([top_mod_dir, core_mod_dir, doesnotexist]))
589589

590590
def test_prepend_module_path(self):
591591
"""Test prepend_module_path method."""
@@ -741,7 +741,11 @@ def test_wrong_modulepath(self):
741741
"""Test whether modules tool can deal with a broken $MODULEPATH."""
742742
test_modules_path = os.path.realpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'modules'))
743743
modules_test_installpath = os.path.join(self.test_installpath, 'modules', 'all')
744-
os.environ['MODULEPATH'] = '/some/non-existing/path:/this/doesnt/exists/anywhere:%s' % test_modules_path
744+
os.environ['MODULEPATH'] = os.pathsep.join([
745+
'/some/non-existing/path',
746+
'/this/doesnt/exists/anywhere',
747+
test_modules_path
748+
])
745749
init_config()
746750
# purposely *not* using self.modtool here;
747751
# need to check whether creating new ModulesTool instance doesn't break when $MODULEPATH contains faulty paths
@@ -1106,10 +1110,11 @@ def test_modules_tool_stateless(self):
11061110

11071111
def test_mk_module_cache_key(self):
11081112
"""Test mk_module_cache_key method."""
1109-
os.environ['MODULEPATH'] = '%s:/tmp/test' % self.test_prefix
1113+
module_path = os.pathsep.join([self.test_prefix, '/tmp/test'])
1114+
os.environ['MODULEPATH'] = module_path
11101115
res = self.modtool.mk_module_cache_key('thisisapartialkey')
11111116
self.assertIsInstance(res, tuple)
1112-
self.assertEqual(res, ('MODULEPATH=%s:/tmp/test' % self.test_prefix, self.modtool.COMMAND, 'thisisapartialkey'))
1117+
self.assertEqual(res, ('MODULEPATH=%s' % module_path, self.modtool.COMMAND, 'thisisapartialkey'))
11131118

11141119
del os.environ['MODULEPATH']
11151120
res = self.modtool.mk_module_cache_key('thisisapartialkey')
@@ -1188,11 +1193,11 @@ def test_module_use_unuse(self):
11881193

11891194
self.assertNotIn(test_dir1, os.environ.get('MODULEPATH', ''))
11901195
self.modtool.use(test_dir1)
1191-
self.assertTrue(os.environ['MODULEPATH'].startswith('%s:' % test_dir1))
1196+
self.assertTrue(os.environ['MODULEPATH'].startswith(test_dir1 + os.pathsep))
11921197
self.modtool.use(test_dir2)
1193-
self.assertTrue(os.environ['MODULEPATH'].startswith('%s:' % test_dir2))
1198+
self.assertTrue(os.environ['MODULEPATH'].startswith(test_dir2 + os.pathsep))
11941199
self.modtool.use(test_dir3)
1195-
self.assertTrue(os.environ['MODULEPATH'].startswith('%s:' % test_dir3))
1200+
self.assertTrue(os.environ['MODULEPATH'].startswith(test_dir3 + os.pathsep))
11961201

11971202
# Adding an empty modulepath is not possible
11981203
modulepath = os.environ.get('MODULEPATH', '')
@@ -1223,7 +1228,7 @@ def test_module_use_unuse(self):
12231228

12241229
# also test use with high priority
12251230
self.modtool.use(test_dir2, priority=10000)
1226-
self.assertTrue(os.environ['MODULEPATH'].startswith('%s:' % test_dir2))
1231+
self.assertTrue(os.environ['MODULEPATH'].startswith(test_dir2 + os.pathsep))
12271232

12281233
self.modtool.load(['test'])
12291234
self.assertEqual(os.getenv('TEST123'), 'two')
@@ -1235,8 +1240,9 @@ def test_module_use_unuse(self):
12351240
old_module_path = os.environ['MODULEPATH']
12361241
self.modtool._set_module_path(['/foo'])
12371242
self.assertEqual(os.environ['MODULEPATH'], '/foo')
1238-
self.modtool._set_module_path(['/foo', '/bar'])
1239-
self.assertEqual(os.environ['MODULEPATH'], '/foo:/bar')
1243+
foo_and_bar_paths = ['/foo', '/bar']
1244+
self.modtool._set_module_path(foo_and_bar_paths)
1245+
self.assertEqual(os.environ['MODULEPATH'], os.pathsep.join(foo_and_bar_paths))
12401246
self.modtool._set_module_path([''])
12411247
self.assertEqual(os.environ['MODULEPATH'], '')
12421248
self.modtool._set_module_path([])
@@ -1246,8 +1252,8 @@ def test_module_use_unuse(self):
12461252
# Same for generators
12471253
self.modtool._set_module_path(i for i in ['/foo'])
12481254
self.assertEqual(os.environ['MODULEPATH'], '/foo')
1249-
self.modtool._set_module_path(i for i in ['/foo', '/bar'])
1250-
self.assertEqual(os.environ['MODULEPATH'], '/foo:/bar')
1255+
self.modtool._set_module_path(i for i in foo_and_bar_paths)
1256+
self.assertEqual(os.environ['MODULEPATH'], os.pathsep.join(foo_and_bar_paths))
12511257
self.modtool._set_module_path(i for i in [''])
12521258
self.assertEqual(os.environ['MODULEPATH'], '')
12531259
self.modtool._set_module_path(i for i in [])
@@ -1257,7 +1263,9 @@ def test_module_use_unuse(self):
12571263
# check whether prepend with priority actually works (priority is specific to Lmod)
12581264
self.modtool.use(test_dir1, priority=100)
12591265
self.modtool.use(test_dir3)
1260-
self.assertTrue(os.environ['MODULEPATH'].startswith('%s:%s:%s:' % (test_dir2, test_dir1, test_dir3)))
1266+
self.assertTrue(os.environ['MODULEPATH'].startswith(
1267+
os.pathsep.join([test_dir2, test_dir1, test_dir3])
1268+
))
12611269
self.modtool.load(['test'])
12621270
self.assertEqual(os.getenv('TEST123'), 'two')
12631271
self.modtool.unload(['test'])
@@ -1577,7 +1585,7 @@ def test_modulecmd_strip_source(self):
15771585
write_file(modulecmd, modulecmd_txt)
15781586
adjust_permissions(modulecmd, stat.S_IXUSR, add=True)
15791587

1580-
os.environ['PATH'] = '%s:%s' % (self.test_prefix, os.getenv('PATH'))
1588+
os.environ['PATH'] = os.pathsep.join([self.test_prefix, os.getenv('PATH')])
15811589

15821590
modtool = EnvironmentModulesC()
15831591
modtool.run_module('load', 'test123')

0 commit comments

Comments
 (0)