Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions norpm/macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 = ""
Expand Down
18 changes: 10 additions & 8 deletions norpm/specfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ""

Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_spec_expansion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Loading