|
| 1 | +""" |
| 2 | +Extract macro-expanded tag value (e.g., BuildArch) from given specfile. |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +import tempfile |
| 7 | +from copr_common.request import SafeRequest |
| 8 | +import logging |
| 9 | + |
| 10 | +from norpm.macrofile import system_macro_registry |
| 11 | +from norpm.specfile import specfile_expand, ParserHooks |
| 12 | +from norpm.overrides import override_macro_registry |
| 13 | +from norpm.exceptions import NorpmError |
| 14 | + |
| 15 | +DEFAULT_OVERRIDE_URL = "https://raw.githubusercontent.com/praiskup/norpm-macro-overrides/refs/heads/main/distro-arch-specific.json" |
| 16 | + |
| 17 | +class _TagHooks(ParserHooks): |
| 18 | + """ Gather access to spec tags """ |
| 19 | + def __init__(self, expanded_tags): |
| 20 | + self.expanded_tags = set(expanded_tags) |
| 21 | + self.tags = {} |
| 22 | + |
| 23 | + def tag_found(self, name, value, _tag_raw): |
| 24 | + """ |
| 25 | + Parser hook that gathers the tags' values, if defined. |
| 26 | + """ |
| 27 | + if name not in self.expanded_tags: |
| 28 | + return |
| 29 | + if name not in self.tags: |
| 30 | + self.tags[name] = [] |
| 31 | + # tags may be specified multiple times within a single spec file |
| 32 | + self.tags[name] += [value] |
| 33 | + |
| 34 | + |
| 35 | +def collapse_tag_values_cb(array_of_tag_values): |
| 36 | + """ |
| 37 | + Process tags that represent sets of strings (e.g., ExcludeArch). |
| 38 | +
|
| 39 | + If a tag is specified multiple times within the specfile, this function |
| 40 | + performs a union of all encountered values. The resulting set contains |
| 41 | + every unique string defined across all instances of the tag. |
| 42 | +
|
| 43 | + Returns: |
| 44 | + set: A set of all unique strings extracted from the tag definitions. |
| 45 | + """ |
| 46 | + concat = " ".join(array_of_tag_values) |
| 47 | + return set(concat.split()) |
| 48 | + |
| 49 | + |
| 50 | +def extract_tags_from_specfile(specfile, extract_tags, override_database=None, |
| 51 | + target=None, tag_cb=None): |
| 52 | + """ |
| 53 | + Parse the given SPECFILE against system macros and optionally TARGET macros. |
| 54 | +
|
| 55 | + If TARGET is specified, OVERRIDE_DATABASE (a file path) must also be |
| 56 | + provided. If TARGET is omitted, the local system macros are used by |
| 57 | + default. |
| 58 | +
|
| 59 | + Args: |
| 60 | + specfile (str): Path to the specfile to be parsed. |
| 61 | + target (str, optional): Target distribution (e.g., "rhel-7"). |
| 62 | + override_database (str, optional): Database file path required if target |
| 63 | + is set. |
| 64 | + tag_cb (callable, optional): A callback function used to transform the |
| 65 | + list of values for each tag. If provided, each item in the return |
| 66 | + dictionary is passed through this function. A common choice is |
| 67 | + `collapse_tag_values_cb`, which collapses the list into a set of |
| 68 | + unique strings. |
| 69 | +
|
| 70 | + Returns: |
| 71 | + dict: A mapping of lowercase tagnames to their processed values. |
| 72 | + Default format: {'excludearch': ['ppc64 ppc64le', 's390x i386']} |
| 73 | + With tag_cb: The value type is determined by the callback's return. |
| 74 | + With tag_cb=collapse_tag_values_cb: |
| 75 | + {'excludearch': {'ppc64', 'ppc64le', 's390x', 'i386'}} |
| 76 | + Note: |
| 77 | + Since RPM tags are case-insensitive, all tagnames are normalized |
| 78 | + using .lower(). |
| 79 | + """ |
| 80 | + registry = system_macro_registry() |
| 81 | + if override_database: |
| 82 | + registry = override_macro_registry(registry, override_database, target) |
| 83 | + |
| 84 | + # %dist definition contains %lua mess, it's safer to clear it (since we |
| 85 | + # don't necessarily need it) |
| 86 | + registry["dist"] = "" |
| 87 | + |
| 88 | + # norpm maintains a few tricks to ease the spec file parsing |
| 89 | + registry.known_norpm_hacks() |
| 90 | + |
| 91 | + tags = _TagHooks(extract_tags) |
| 92 | + try: |
| 93 | + with open(specfile, "r", encoding="utf8") as fd: |
| 94 | + specfile_expand(fd.read(), registry, tags) |
| 95 | + except NorpmError as err: |
| 96 | + print("WARNING: Building for all architectures since " |
| 97 | + f"the spec file parser: failed: {err}") |
| 98 | + |
| 99 | + if not tag_cb: |
| 100 | + return tags.tags |
| 101 | + |
| 102 | + processed = {} |
| 103 | + for tag, values in tags.tags.items(): |
| 104 | + processed[tag] = tag_cb(values) |
| 105 | + |
| 106 | + return processed |
| 107 | + |
| 108 | + |
| 109 | +def get_architecture_specific_tags(specfile, extract_tags, targets, |
| 110 | + override_database=DEFAULT_OVERRIDE_URL, |
| 111 | + log=logging): |
| 112 | + """ |
| 113 | + A high-level tool for Copr, working with temporary files, etc. |
| 114 | + """ |
| 115 | + architecture_tags = {} |
| 116 | + request = SafeRequest(log=log) |
| 117 | + response = request.get(override_database) |
| 118 | + with tempfile.NamedTemporaryFile(mode='wb', delete=False) as temp: |
| 119 | + temp_path = temp.name |
| 120 | + temp.write(response.content) |
| 121 | + |
| 122 | + try: |
| 123 | + for distro in targets: |
| 124 | + ask = distro |
| 125 | + if distro.startswith("custom-"): |
| 126 | + # TODO: what do we do about custom-* chroots? |
| 127 | + ask = "fedora-rawhide" |
| 128 | + log.info("Extracting arch-specific tags for %s", distro) |
| 129 | + architecture_tags[distro] = extract_tags_from_specfile( |
| 130 | + specfile, |
| 131 | + extract_tags, |
| 132 | + override_database=temp_path, |
| 133 | + target=ask, |
| 134 | + tag_cb=collapse_tag_values_cb |
| 135 | + ) |
| 136 | + finally: |
| 137 | + try: |
| 138 | + os.unlink(temp_path) |
| 139 | + except OSError: |
| 140 | + pass |
| 141 | + return architecture_tags |
0 commit comments