Skip to content

Commit

Permalink
Merge pull request #3 from boegel/457_fix_EBDEVEL_modulefiles
Browse files Browse the repository at this point in the history
use quote_str function in set_environment - fixes issue with develop modules
  • Loading branch information
fgeorgatos committed Feb 11, 2013
2 parents 94fe100 + 263674e commit b8a9f8d
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 38 deletions.
28 changes: 5 additions & 23 deletions easybuild/framework/easyconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@
from distutils.version import LooseVersion

from easybuild.tools.build_log import EasyBuildError, get_log
from easybuild.tools.systemtools import get_shared_lib_ext
from easybuild.tools.filetools import run_cmd
from easybuild.tools.ordereddict import OrderedDict
from easybuild.tools.systemtools import get_shared_lib_ext
from easybuild.tools.toolchain.utilities import search_toolchain
from easybuild.tools.utilities import quote_str

# we use a tuple here so we can sort them based on the numbers
MANDATORY = (0, 'mandatory')
Expand Down Expand Up @@ -954,25 +955,6 @@ def tweak(src_fn, target_fn, tweaks, log):

tweaks.pop(key)

def quoted(x):
"""Obtain a new value to be used in string replacement context.
For non-string values, it just returns the exact same value.
For string values, it tries to escape the string in quotes, e.g.,
foo becomes 'foo', foo'bar becomes "foo'bar",
foo'bar"baz becomes \"\"\"foo'bar"baz\"\"\", etc.
"""
if type(x) == str:
if "'" in x and '"' in x:
return '"""%s"""' % x
elif "'" in x:
return '"%s"' % x
else:
return "'%s'" % x
else:
return x

# add parameters or replace existing ones
for (key,val) in tweaks.items():

Expand All @@ -992,10 +974,10 @@ def quoted(x):
diff = not res.group(1) == val

if diff:
ectxt = regexp.sub("%s = %s # tweaked by EasyBuild (was: %s)" % (key, quoted(val), res.group(1)), ectxt)
log.info("Tweaked '%s' to '%s'" % (key, quoted(val)))
ectxt = regexp.sub("%s = %s # tweaked by EasyBuild (was: %s)" % (key, quote_str(val), res.group(1)), ectxt)
log.info("Tweaked '%s' to '%s'" % (key, quote_str(val)))
else:
additions.append("%s = %s" % (key, quoted(val)))
additions.append("%s = %s" % (key, quote_str(val)))


if additions:
Expand Down
7 changes: 1 addition & 6 deletions easybuild/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
from easybuild.tools.modules import curr_module_paths, mk_module_path
from easybuild.tools.toolchain.utilities import search_toolchain
from easybuild.tools.ordereddict import OrderedDict
from easybuild.tools.utilities import any
from easybuild.tools.version import VERBOSE_VERSION as FRAMEWORK_VERSION
EASYBLOCKS_VERSION = 'UNKNOWN'
try:
Expand Down Expand Up @@ -1540,12 +1541,6 @@ def list_toolchains():

print "\t%s: %s" % (tcname, ', '.join(sorted(tc_elems)))

# FIXME: remove when Python version on which we rely provides any by itself
def any(ls):
"""Reimplementation of 'any' function, which is not available in Python 2.4 yet."""

return sum([bool(x) for x in ls]) != 0

if __name__ == "__main__":
try:
options, orig_paths, log, logfile, hn, parser = parse_options()
Expand Down
2 changes: 1 addition & 1 deletion easybuild/test/easyconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ def runTest(self):
self.assertEqual(ec['toolchain'], {'name': tcname, 'version': tcver})
# can't check for key 'foo', because EasyConfig ignores parameter names it doesn't know about
txt = open(res[1], "r").read()
self.assertTrue(re.search("foo = '%s'" % specs['foo'], txt))
self.assertTrue(re.search('foo = "%s"' % specs['foo'], txt))
os.remove(res[1])

# should pick correct version, i.e. not newer than what's specified, if a choice needs to be made
Expand Down
13 changes: 8 additions & 5 deletions easybuild/test/modulegenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ def runTest(self):
desc = self.modgen.get_description()
self.assertEqual(desc, expected)

# test loadModule
# test load_module
expected = """
if { ![is-loaded name/version] } {
module load name/version
}
"""
self.assertEqual(expected, self.modgen.load_module("name", "version"))

# test unloadModule
# test unload_module
expected = """
if { ![is-loaded name/version] } {
if { [is-loaded name] } {
Expand All @@ -95,7 +95,7 @@ def runTest(self):
"""
self.assertEqual(expected, self.modgen.unload_module("name", "version"))

# test prependPaths
# test prepend_paths
expected = """prepend-path key $root/path1
prepend-path key $root/path2
"""
Expand All @@ -110,8 +110,11 @@ def runTest(self):
self.modgen.prepend_paths, "key2", ["bar", "/tmp/foo"])


# test setEnvironment
self.assertEqual("setenv\tkey\t\tvalue\n", self.modgen.set_environment("key", "value"))
# test set_environment
self.assertEqual('setenv\tkey\t\t"value"\n', self.modgen.set_environment("key", "value"))
self.assertEqual("setenv\tkey\t\t'va\"lue'\n", self.modgen.set_environment("key", 'va"lue'))
self.assertEqual('setenv\tkey\t\t"va\'lue"\n', self.modgen.set_environment("key", "va'lue"))
self.assertEqual('setenv\tkey\t\t"""va"l\'ue"""\n', self.modgen.set_environment("key", """va"l'ue"""))

def tearDown(self):
"""cleanup"""
Expand Down
5 changes: 3 additions & 2 deletions easybuild/tools/module_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from easybuild.tools.config import install_path
from easybuild.tools.filetools import rmtree2
from easybuild.tools.modules import Modules
from easybuild.tools.utilities import quote_str


log = get_log('moduleGenerator')
Expand Down Expand Up @@ -177,8 +178,8 @@ def set_environment(self, key, value):
"""
Generate setenv statement for the given key/value pair.
"""
# Double quotes are needed, to ensure smooth working of EBDEVEL* modulefiles
return 'setenv\t%s\t\t"%s"\n' % (key, value)
# quotes are needed, to ensure smooth working of EBDEVEL* modulefiles
return 'setenv\t%s\t\t%s\n' % (key, quote_str(value))

def __del__(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion easybuild/tools/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def run_module(self, *args, **kwargs):
try:
exec stdout
except Exception, err:
raise EasyBuildError("Changing environment as dictated by module failed: %s" % err)
raise EasyBuildError("Changing environment as dictated by module failed: %s (%s)" % (err, stdout))

# Process stderr
result = []
Expand Down
57 changes: 57 additions & 0 deletions easybuild/tools/utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
##
# Copyright 2012-2013 Ghent University
#
# This file is part of EasyBuild,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
# with support of Ghent University (http://ugent.be/hpc),
# the Flemish Supercomputer Centre (VSC) (https://vscentrum.be/nl/en),
# the Hercules foundation (http://www.herculesstichting.be/in_English)
# and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en).
#
# http://github.com/hpcugent/easybuild
#
# EasyBuild is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation v2.
#
# EasyBuild is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with EasyBuild. If not, see <http://www.gnu.org/licenses/>.
##
"""
Module with various utility functions
@authors: Kenneth Hoste (UGent)
"""

# FIXME: remove when Python version on which we rely provides any by itself
def any(ls):
"""Reimplementation of 'any' function, which is not available in Python 2.4 yet."""

return sum([bool(x) for x in ls]) != 0

def quote_str(x):
"""
Obtain a new value to be used in string replacement context.
For non-string values, it just returns the exact same value.
For string values, it tries to escape the string in quotes, e.g.,
foo becomes 'foo', foo'bar becomes "foo'bar",
foo'bar"baz becomes \"\"\"foo'bar"baz\"\"\", etc.
"""

if isinstance(x, basestring):
if "'" in x and '"' in x:
return '"""%s"""' % x
elif '"' in x:
return "'%s'" % x
else:
return '"%s"' % x
else:
return x

0 comments on commit b8a9f8d

Please sign in to comment.