Skip to main content

python-rpm-spec

pytest status PyPI version

python-rpm-spec is a Python-only library for parsing RPM spec files.

tl;dr If you want to quickly parse a spec file on the command line you might want to give rpmspec --parse a try.

rpmspec --parse file.spec | awk '/Source/ {print $2}'

If you write Python, have no /usr/bin/rpm around, 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.

Features

  • No extra dependencies other than Python 3.
  • Available on all platforms, parse spec files on Windows.
  • Read-only (for manipulating spec files see Alternatives below).

Supported Python versions

Python 3.11 and later versions are supported.

Python Version Supported Until
3.14 2030-10
3.13 2029-10
3.12 2028-10
3.11 2027-10

Install

python-rpm-spec is hosted on PyPI - the Python Package Index. All you need to do is

pip install python-rpm-spec

in your virtual environment.

Examples

The libraries main API objects are the Spec object, representing an entire spec file and the replace_macros function which is used to expand macro's into absolute string values.

This is how you access a spec file's various definitions:

from pyrpm.spec import Spec, replace_macros

spec = Spec.from_file('llvm.spec')
print(spec.version)  # 3.8.0
print(spec.sources[0])  # http://llvm.org/releases/%{version}/%{name}-%{version}.src.tar.xz
print(replace_macros(spec.sources[0], spec))  # http://llvm.org/releases/3.8.0/llvm-3.8.0.src.tar.xz

for package in spec.packages:
    print(f'{package.name}: {package.summary if hasattr(package, "summary") else spec.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 libraries

Example showing how to retrieve named source or patch files from a spec:

from pyrpm.spec import Spec

spec = Spec.from_file('llvm.spec')

# Access sources and patches via name
for k, v in spec.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 on
for source in spec.sources:
    print(source)

# http://llvm.org/releases/%{version}/%{name}-%{version}.src.tar.xz
# llvm-config.h

Example showing how to get versioned BuildRequires: and Requires: out of a spec file:

from pyrpm.spec import Spec

spec = Spec.from_file('attica-qt5.spec')

# Access sources and patches via name
for br in spec.build_requires:
    print(f'{br.name} {br.operator} {br.version}' if br.version else f'{br.name}')

# cmake >= 3.0
# extra-cmake-modules >= %{_tar_path}
# fdupes
# kf5-filesystem
# pkg-config
# cmake(Qt5Core) >= 5.6.0
# cmake(Qt5Network) >= 5.6.0

If you want that the library create warnings during parsing, for example on unknown macros, set warnings_enabled to True:

import pyrpm.spec

pyrpm.spec.warnings_enabled = True
# …

Dependencies

No extra dependencies are required except for Python 3.8 or newer.

Current status

This 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.

Also note that there is a GitHub workflow that runs the parser on Fedora's spec files.

Alternatives

Here 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 March 2023.

  • If you are on a Linux system that has the RPM package manager installed, consider using system tools like

    • rpmspec(8) from rpm-build package. Example: rpmspec --parse foo.spec will parse a spec file to stdout, expanding all the macros installed on the system. Still relies on $HOME/rpmbuild to work.
    • rpmdev-spectool(1) from rpmdevtools package. Example: spectool --get-files foo.spec will download all sources and patches from a spec file.

    The parsers of those system tools are probably more up to date and less buggy than this library.

Development

If you want to hack on this library you could start with following recipe:

git clone https://github.com/bkircher/python-rpm-spec.git  # Clone the repo
cd python-rpm-spec  # Change into the source directory
python3 -m venv .venv  # Create a virtual environment
source .venv/bin/activate  # Activate it
pip install -r requirements.txt  # Install dependencies for development
pytest  # Execute all tests
mypy . # Run the type checker

That's it. Make sure to check out the issue tracker for things to work on or open a new issue to let others know what you are working on.

Further references

Happy hacking!

Metadata

Release files for python-rpm-spec 0.18.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-rpm-spec 0.18.0
File Size Uploaded
python_rpm_spec-0.18.0.tar.gz 12.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-rpm-spec 0.18.0
File Interpreter ABI Platform
python_rpm_spec-0.18.0-py3-none-any.whl Python 3 none any Details

Total release size: 23.1 kB

Release files / python_rpm_spec-0.18.0.tar.gz

Download URL python_rpm_spec-0.18.0.tar.gz
Size 12.3 kB
Tags Source
SHA-256 checksum
How to use checksums
6d8ade3abf7d06f3134749b1336367d0163077925a213b147ed8081349ec987c
BLAKE2b-256 checksum
How to use checksums
ed71afdfd6be13511f12161c6371029edea7a6aa405ebde16f43e2126f8f33f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 25, 2026.

Transparency log

Release files / python_rpm_spec-0.18.0-py3-none-any.whl

Download URL python_rpm_spec-0.18.0-py3-none-any.whl
Size 10.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
67281076a7de8110b4e9ef4cb78b178c6dc90fb8e7c9998a2c47b6e923d32e8b
BLAKE2b-256 checksum
How to use checksums
93695270dc9dfe7a332a53262d4d29bab217eeee44b207d28517cdac0d697e57
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 25, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.18.0 This release

2 release files

0.17.0

2 release files

0.15.0

2 release files

0.14.1

2 release files

0.14

2 release files

0.13

2 release files

0.12

2 release files

0.11

2 release files

0.10

2 release files

0.9

2 release files

0.8

2 release files

0.7

1 release file

0.6

1 release file

0.5

1 release file

0.4

1 release file

0.3

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page