Package: python-rpm-spec
python-rpm-specpython-rpm-spec is a Python library for parsing RPM spec files.tl;drIf you want to quickly parse a spec file on the command line you might want to giverpmspec --parsea try.rpmspec--parsefile.spec|awk'/Source/ {print $2}'If you write Python, have no/usr/bin/rpmaround, or want to do something slightly more complicated, try using this Python library.RPMs are build from a package's sources along with a spec file. The spec file controls how the RPM is built. This library allows you to parse spec files and gives you simple access to various bits of information that is contained in the spec file.FeaturesNo extra dependencies other than Python 3.Available on all platforms, parse spec files on Windows.Read-only (for manipulating spec files seeAlternativesbelow).Supported Python versionsAllcurrent Python branchesare supported.Python VersionSupported Until3.122028-103.112027-103.102026-103.92025-103.82024-10Installpython-rpm-spec ishostedon PyPI - the Python Package Index. All you need to do ispipinstallpython-rpm-specin your virtual environment.ExamplesThis is how you access a spec file's various definitions:frompyrpm.specimportSpec,replace_macrosspec=Spec.from_file('llvm.spec')print(spec.version)# 3.8.0print(spec.sources[0])# http://llvm.org/releases/%{version}/%{name}-%{version}.src.tar.xzprint(replace_macros(spec.sources[0],spec))# http://llvm.org/releases/3.8.0/llvm-3.8.0.src.tar.xzforpackageinspec.packages:print(f'{package.name}:{package.summaryifhasattr(package,"summary")elsespec.summary}')# llvm: The Low Level Virtual Machine# llvm-devel: Libraries and header files for LLVM# llvm-doc: Documentation for LLVM# llvm-libs: LLVM shared libraries# llvm-static: LLVM static librariesExample showing how to retrieve named source or patch files from a spec:frompyrpm.specimportSpecspec=Spec.from_file('llvm.spec')# Access sources and patches via namefork,vinspec.sources_dict.items():print(f'{k}→{v}')# Source0 → http://llvm.org/releases/%{version}/%{name}-%{version}.src.tar.xz# Source100 → llvm-config.h# Or as a list with indices and so onforsourceinspec.sources:print(source)# http://llvm.org/releases/%{version}/%{name}-%{version}.src.tar.xz# llvm-config.hExample showing how to get versionedBuildRequires:andRequires:out of a spec file:frompyrpm.specimportSpecspec=Spec.from_file('attica-qt5.spec')# Access sources and patches via nameforbrinspec.build_requires:print(f'{br.name}{br.operator}{br.version}'ifbr.versionelsef'{br.name}')# cmake >= 3.0# extra-cmake-modules >= %{_tar_path}# fdupes# kf5-filesystem# pkg-config# cmake(Qt5Core) >= 5.6.0# cmake(Qt5Network) >= 5.6.0If you want that the librarycreate warningsduring parsing, for example on unknown macros, setwarnings_enabledtoTrue:importpyrpm.specpyrpm.spec.warnings_enabled=True# …DependenciesNo extra dependencies are required except for Python 3.7 or newer.Current statusThis library is an ambitious Python script that became a library. It is not complete and it does not fit every use case.It is probably very slow and it relies on regular expressions for parsing.It does not parse everything in a spec file, only the pieces myself and others needed so far.So there is probably still plenty of stuff missing (i.e. support for%include). However, it should not be too complicated to add support for the missing pieces.AlternativesHere is a list of alternatives to this library:packit/specfile- Allows parsing and, different to python-rpm-spec, the manipulation of spec files. Part of packit. Actively developed as of Mar 2023.If you are on a Linux system that has the RPM package manager installed, consider using system tools likerpmspec(8)from rpm-build package. Example:rpmspec --parse foo.specwill parse a spec file to stdout, expanding all the macros installed on the system. Still relies on$HOME/rpmbuildto work.rpmdev-spectool(1)from rpmdevtools package. Example:spectool --get-files foo.specwill download all sources and patches from a spec file.The parsers are probably more up to date and less buggy than this library.DevelopmentIf you want to hack on this library you could start with following recipe:gitclonehttps://github.com/bkircher/python-rpm-spec.git# Clone the repocdpython-rpm-spec# Change into the source directorypython3-mvenv.venv# Create a virtual environmentsource.venv/bin/activate# Activate itpipinstall-rrequirements.txt# Install dependencies for developmentpytest# Execute all testsmypy.# Run the type checkerThat's it.Further referencesRPM project documentationwith a couple of links to books or Fedora project documentation.Take a look at the excellentRPM Packaging Guide, especially the sectionWhat is a SPEC File?Happy hacking!