Skip to content

Commit

Permalink
Merge pull request #1323 from Caylo/dump-versionsuffix-issue1322
Browse files Browse the repository at this point in the history
take care of special characters in template strings for dump function
  • Loading branch information
boegel committed Jul 15, 2015
2 parents 8c32ca8 + 6d7f123 commit 418545f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
7 changes: 4 additions & 3 deletions easybuild/framework/easyconfig/easyconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,6 @@ def to_template_str(value, templ_const, templ_val):
- templ_val is an ordered dictionary of template strings specific for this easyconfig file
"""
if isinstance(value, basestring):

# wrap string into quotes, except if it matches a template constant
if value not in templ_const.values():
value = quote_py_str(value)
Expand All @@ -1003,8 +1002,10 @@ def to_template_str(value, templ_const, templ_val):
else:
# check for template values (note: templ_val dict is 'upside-down')
for tval, tname in templ_val.items():
# only replace full words with templates, not substrings, by using \b in regex
value = re.sub(r"\b" + re.escape(tval) + r"\b", r'%(' + tname + ')s', value)
# only replace full words with templates: word to replace should be at the beginning of a line
# or be preceded by a non-alphanumeric (\W). It should end at the end of a line or be succeeded
# by another non-alphanumeric.
value = re.sub(r'(^|\W)' + re.escape(tval) + r'(\W|$)', r'\1%(' + tname + r')s\2', value)
else:
if isinstance(value, list):
value = '[' + ', '.join([to_template_str(v, templ_const, templ_val) for v in value]) + ']'
Expand Down
31 changes: 28 additions & 3 deletions test/framework/easyconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
from easybuild.framework.easyconfig.constants import EXTERNAL_MODULE_MARKER
from easybuild.framework.easyconfig.easyconfig import EasyConfig
from easybuild.framework.easyconfig.easyconfig import create_paths
from easybuild.framework.easyconfig.easyconfig import get_easyblock_class
from easybuild.framework.easyconfig.easyconfig import get_easyblock_class, quote_py_str, to_template_str
from easybuild.framework.easyconfig.parser import fetch_parameters_from_easyconfig
from easybuild.framework.easyconfig.tweak import obtain_ec_for, tweak_one
from easybuild.tools.build_log import EasyBuildError
Expand Down Expand Up @@ -1238,7 +1238,7 @@ def test_dump_template(self):
"preconfigopts = '--opt1=%s' % name",
"configopts = '--opt2=0.0.1'",
'',
"sanity_check_paths = {'files': ['files/foo/foobar'], 'dirs':[] }",
"sanity_check_paths = {'files': ['files/foo/foobar', 'files/x-test'], 'dirs':[] }",
'',
"foo_extra1 = 'foobar'"
])
Expand All @@ -1260,9 +1260,10 @@ def test_dump_template(self):
r"homepage = 'http://foo.com/'",
r'description = "foo description"', # no templating for description
r"sources = \[SOURCELOWER_TAR_GZ\]",
r"dependencies = \[\('bar', '1.2.3', '%\(versionsuffix\)s'\)\]",
r"preconfigopts = '--opt1=%\(name\)s'",
r"configopts = '--opt2=%\(version\)s'",
r"sanity_check_paths = {'files': \['files/%\(namelower\)s/foobar'\]",
r"sanity_check_paths = {'files': \['files/%\(namelower\)s/foobar', 'files/x-test'\]",
]

for pattern in patterns:
Expand All @@ -1272,6 +1273,30 @@ def test_dump_template(self):
# reparsing the dumped easyconfig file should work
ecbis = EasyConfig(testec)

def test_to_template_str(self):
""" Test for to_template_str method """

# reverse dict of known template constants; template values (which are keys here) must be 'string-in-string
templ_const = {
"'template'":'TEMPLATE_VALUE',
"'%(name)s-%(version)s'": 'NAME_VERSION',
}

templ_val = {
'foo':'name',
'0.0.1':'version',
'-test':'special_char',
}

self.assertEqual(to_template_str("template", templ_const, templ_val), 'TEMPLATE_VALUE')
self.assertEqual(to_template_str("foo/bar/0.0.1/", templ_const, templ_val), "'%(name)s/bar/%(version)s/'")
self.assertEqual(to_template_str("foo-0.0.1", templ_const, templ_val), 'NAME_VERSION')
templ_list = to_template_str(['-test', 'dontreplacenamehere'], templ_const, templ_val)
self.assertEqual(templ_list, "['%(special_char)s', 'dontreplacenamehere']")
templ_dict = to_template_str({'a':'foo', 'b':'notemplate'}, templ_const, templ_val)
self.assertEqual(templ_dict, "{'a': '%(name)s', 'b': 'notemplate'}")
self.assertEqual(to_template_str(('foo', '0.0.1'), templ_const, templ_val), "('%(name)s', '%(version)s')")


def suite():
""" returns all the testcases in this module """
Expand Down

0 comments on commit 418545f

Please sign in to comment.