Skip to content

Instantly share code, notes, and snippets.

@generalmimon
Created October 10, 2021 21:00
Show Gist options
  • Save generalmimon/11a329038b8b6afb245e7eafd6abe95d to your computer and use it in GitHub Desktop.
Save generalmimon/11a329038b8b6afb245e7eafd6abe95d to your computer and use it in GitHub Desktop.
Python script to read RPM tags.md file and print KSY enum definition of the tags
import re
from collections import OrderedDict
tag_regex = r"\* (\w+) \((\d+)\)"
heading_regex = r"^## (.+)"
sections = {}
# https://github.com/rpm-software-management/rpm/raw/911448f2/doc/manual/tags.md
with open('tags.md', 'r', encoding='utf-8') as f:
contents = f.read()
matches = list(re.finditer(heading_regex, contents, re.MULTILINE))
for i, m in enumerate(matches):
(heading,) = m.groups()
body_start = m.end()
body_end = matches[i + 1].start() if i + 1 < len(matches) else None
sections[heading] = contents[body_start:body_end]
tags = {}
for heading, body in sections.items():
if heading == 'Aliases':
continue
is_obsolete = bool(re.match(r"deprecated|obsolete", heading, re.IGNORECASE))
matches = re.finditer(tag_regex, body)
for m in matches:
(tag_name, tag_value) = m.groups()
tags[int(tag_value)] = (tag_name, is_obsolete)
tags_ordered = OrderedDict(sorted(tags.items()))
enum_str = ''.join([(f' # {k}: {v.lower()}\n' if is_obsolete else f""" {k}:
id: {v.lower()}
-orig-id: RPMTAG_{v.upper()}
""") for k, (v, is_obsolete) in tags_ordered.items()])
print(enum_str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment