diff --git a/norpm/macro.py b/norpm/macro.py index e009ea1..7da6041 100644 --- a/norpm/macro.py +++ b/norpm/macro.py @@ -162,6 +162,15 @@ def is_macro_name(name): return all(is_macro_character(c) for c in name) +def drop_curly_brackets(call): + """ + Drop curly brackets from macro call. + """ + if call.startswith("%{"): + return call[2:-1] + return call[1:] + + def parse_macro_call(call): """Given a macro call, return 4-ary (success, name, conditionals, params) @@ -175,10 +184,7 @@ def parse_macro_call(call): # pylint: disable=too-many-branches success = True - if call.startswith("%{"): - call = call[2:-1] - else: - call = call[1:] + call = drop_curly_brackets(call) conditionals = set() name = "" diff --git a/norpm/specfile.py b/norpm/specfile.py index e646166..e909793 100644 --- a/norpm/specfile.py +++ b/norpm/specfile.py @@ -19,7 +19,7 @@ import re from norpm.tokenize import tokenize, Special, BRACKET_TYPES, OPENING_BRACKETS -from norpm.macro import is_macro_character, parse_macro_call +from norpm.macro import is_macro_character, parse_macro_call, drop_curly_brackets from norpm.macrofile import macrofile_parse, macrofile_split_generator from norpm.getopt import getopt from norpm.logging import get_logger @@ -583,7 +583,8 @@ def _expand_snippet(context, snippet, definitions, depth=0): if _isdef_start(snippet): if not context.expanding: return "" - _, params = snippet[1:].split(maxsplit=1) + call = drop_curly_brackets(snippet) + _, params = call.split(maxsplit=1) macrofile_parse("%" + params, definitions, inspec=True) return "" @@ -886,11 +887,11 @@ def _specfile_expand_generator(context, content, macros): def _isdef_start(string, keywords=None): if keywords is None: keywords = ["global", "define"] - for pfx in keywords: - pfx = "%" + pfx - if string.startswith(pfx): - if string[len(pfx):][0] in ["\t", " "]: - return True + for mpfx in keywords: + for pfx in [f"%{mpfx}", f"%{{{mpfx}"]: + if string.startswith(pfx): + if string[len(pfx):][0] in ["\t", " "]: + return True return False @@ -929,7 +930,8 @@ def _specfile_expand_string_generator(context, string, macros, depth=0, if not context.expanding: continue - _, definition = buffer.split(maxsplit=1) + definition = drop_curly_brackets(buffer) + _, definition = definition.split(maxsplit=1) expanded_def = specfile_expand_string(definition, macros, depth+1) for name, body, params in macrofile_split_generator('%' + expanded_def, inspec=True): macros[name] = (body, params) diff --git a/tests/test_spec_expansion.py b/tests/test_spec_expansion.py index 8baae61..71ed9c0 100644 --- a/tests/test_spec_expansion.py +++ b/tests/test_spec_expansion.py @@ -473,3 +473,21 @@ def test_quote_and_gsub_and_doublestar(): 1.2.3.4 epoch """ + + +def test_definition_with_curly_brackets(): + """ + Test '%{define foo 1}' and '%{global foo 1}' + """ + spec= """\ +%{global hello 1}%hello +%{define world 2}%world +%{define with_space %{quote: a b c} }%with_space +%[ %{global hello 3} || %{define world 4} ]%hello%world +""" + assert specfile_expand(spec, MacroRegistry()) == """\ +1 +2 + a b c +034 +"""