Skip to main content

A library for parsing and manipulating RPM spec files.

Project description

specfile

Python library for parsing and manipulating RPM spec files. Main focus is on modifying existing spec files, any change should result in a minimal diff.

This project is still a work in progress.

Motivation

Originally, rebase-helper provided an API for spec file modifications that was also used by packit. The goal of this project is to make the interface more general and convenient to use by not only packit but also by other Python projects that need to interact with RPM spec files.

Important terms used in this library

Section

Section is a spec file section, it has a well-defined name that starts with % character and that can optionally be followed by arguments.

In this library, the starting % of section name is ommited for convenience.

There is a special section internally called %package, often also referred to as preamble, and it represents the content of the spec file that preceeds the first named section (usually %description). This section contains the main package metadata (tags). Metadata of subpackages are defined in subsequent %package sections, that are not anonymous and are always followed by arguments specifying the name of the subpackage (e.g. %package doc or %package -n completely-different-subpackage-name).

Tag

Tag represents a single item of metadata of a package. It has a well-defined name and a value. Tags are defined in %package sections.

For the purposes of this library, a tag can have associated comments. These are consecutive comment lines directly above the tag definition in a spec file.

Examples and use cases

The following examples should cover use cases required by packit.

Instantiating

from specfile import Specfile

# using an absolute path
specfile = Specfile('/tmp/test.spec')

# using a relative path and a different sourcedir
specfile = Specfile('test.spec', sourcedir='/tmp/sources')

Reloading

# if the spec file happens to be modified externally, it can be reloaded
specfile.reload()

Saving changes

# no autosave
specfile = Specfile('test.spec')
...
# saving explicitly when needed
specfile.save()

# enabling autosave, changes are saved immediately after any modification
specfile = Specfile('test.spec', autosave=True)

# as a context manager, saving is performed at context exit
with Specfile('test.spec') as specfile:
    ...

Low-level manipulation

with specfile.sections() as sections:
    # replacing the content of a section
    sections.prep = ['%autosetup -p1']
    # removing a section
    del sections.changelog
    # swapping two sections
    sections[1], sections[2] = sections[2], sections[1]
    # accessing a section with arguments
    print(getattr(sections, 'package devel'))
    # inserting a line into a section
    sections.build.insert(0, 'export VERBOSE=1')

# copying a section from one specfile to another
with specfile1.sections() as sections1, with specfile2.sections() as sections2:
    sections2.changelog[:] = sections1.changelog

Mid-level manipulation - tags and changelog

# accessing tags in preamble
with specfile.tags() as tags:
    # name of the first tag
    print(tags[0].name)
    # raw value of the first tag
    print(tags[0].value)
    # expanded value of the first tag
    print(tags[0].expanded_value)
    # comments associated with the first tag
    print(tags[0].comments)
    # value of a tag by name
    print(tags.url)
    tags.url = 'https://example.com'

# accessing tags in subpackages
with specfile.tags('package devel') as tags:
    print(tags.requires)

# working with changelog
with specfile.changelog() as changelog:
    # most recent changelog entry
    print(changelog[-1])
    # making changes
    changelog[1].content.append('- another line')
    # removing the oldest entry
    del changelog[0]

High-level manipulation

Version and release

# getting version and release
print(specfile.version)
print(specfile.release)

# setting version and release
specfile.version = '2.1'
specfile.release = '3'

# setting both at the same time (release defaults to 1)
specfile.set_version_and_release('2.1', release='3')

# setting version while trying to preserve macros
specfile.set_version_and_release('2.1', preserve_macros=True)

Changelog

# adding a new entry, author is determined using rpmdev-packager (if available)
specfile.add_changelog_entry('New upstream release 2.1')

# adding a new entry, specifying author and timestamp explicitly
specfile.add_changelog_entry(
    'New upstream release 2.1',
    author='Nikola Forró',
    email='nforro@redhat.com',
    timestamp=datetime.date(2021, 11, 20),
)

Sources and patches

print(specfile.sources)
print(specfile.patches)
print(specfile.sources[0].filename)
specfile.sources.append('tests.tar.gz')
specfile.patches[0] = 'downstream.patch'

# fetching non-local sources (including patches)
specfile.download_remote_sources()

# same thing, but trying to fetch from lookaside cache first
specfile.download_remote_sources(lookaside=True)

Other attributes

print(specfile.url)
specfile.summary = '...'

Caveats

RPM macros

specfile uses RPM for parsing spec files and macro expansion. Unfortunately, macros are always stored in a global context, which poses a problem for multiple instances of Specfile.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

specfile-0.1.1.tar.gz (23.4 kB view details)

Uploaded Source

Built Distribution

specfile-0.1.1-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file specfile-0.1.1.tar.gz.

File metadata

  • Download URL: specfile-0.1.1.tar.gz
  • Upload date:
  • Size: 23.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for specfile-0.1.1.tar.gz
Algorithm Hash digest
SHA256 fa901e4615ffbb0c71e4ac62aa9f7a15dad5c603567117c1bd97d7512b128838
MD5 5c7e4fea89d41200de1954fe6a43d5fd
BLAKE2b-256 3e74bc5ec75402d60f4855de9d141725180da16c1e0e12f1baec1bf391e680e5

See more details on using hashes here.

Provenance

File details

Details for the file specfile-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: specfile-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for specfile-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6947a693953fa7079ec55df4b9ca2f616dc1bed73a614640dbe87c756c12a0b9
MD5 897387b6835320dc4f6e683d9eb9adc3
BLAKE2b-256 41a0c299cac72f34a487194324637dcc06ac23238cff0ed770840bc76779f1a6

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page