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.

Source

Source is a source file or a downstream patch defined by a Source/Patch tag or by an entry in %sourcelist/%patchlist section.

Source can be local, specified by a filename, or remote, specified by a URL. Local sources should be located in a directory referred to as sourcedir. Remote sources should be downloaded to this directory.

Sources defined by tags can be explicitly numbered, e.g. Source0 or Patch999, otherwise implicit numbering takes place and source numbers are auto-assigned in a sequential manner.

Prep macros

Prep macros are macros that often appear in (and only in, they don't make sense anywhere else) %prep section.

4 such macros are recognized by this library, %setup, %patch, %autosetup and %autopatch. A typical spec file uses either %autosetup or a combination of %setup and %patch or %autopatch.

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(sections.get('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, changelog and prep

# 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]

# working with macros in %prep section, supports %setup, %patch, %autosetup and %autopatch
from specfile.prep import AutosetupMacro

with specfile.prep() as prep:
    # name of the first macro
    print(prep.macros[0].name)
    # checking if %autosetup is being used
    print('%autosetup' in prep)
    print(AutosetupMacro in prep)
    # changing macro options
    prep.autosetup.options.n = '%{srcname}-%{version}'
    # adding a new %patch macro
    prep.add_patch_macro(28, p=1, b='.test')
    # removing an existing %patch macro by name
    del prep.patch0
    # this works for both '%patch0' and '%patch -P0'
    prep.remove_patch_macro(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

with specfile.sources() as sources:
    # expanded location of the first source
    print(sources[0].expanded_location)
    # adding a source
    sources.append('tests.tar.gz')

with specfile.patches() as patches:
    # modifying location of the first patch
    patches[0].location = 'downstream.patch'
    # removing comments associated with the last patch
    patches[-1].comments.clear()
    # adding and removing patches
    patches.append('another.patch')
    del patches[2]
    # inserting a patch with a specific number
    patches.insert_numbered(999, 'final.patch')

Other attributes

print(specfile.name)
print(specfile.license)
print(specfile.summary)
specfile.url = 'https://example.com'

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.5.0.tar.gz (52.7 kB view details)

Uploaded Source

Built Distribution

specfile-0.5.0-py3-none-any.whl (33.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: specfile-0.5.0.tar.gz
  • Upload date:
  • Size: 52.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for specfile-0.5.0.tar.gz
Algorithm Hash digest
SHA256 c185755a33278cd8ba0546d26230c8b4775fcf40436855bb2d7528068aad607f
MD5 769cbc13c0f2885abb2f248cb37b1e44
BLAKE2b-256 5f2cb18af46f270e16d90825c0e3af9c4f131f260f796ac5f6512d39f88c2d77

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: specfile-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 33.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for specfile-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5bd5c597bef07aefd1082580594d32163301449eee2b35275a354d71bc28b213
MD5 8eae8b84433688d3e947d8ab5a5e4f26
BLAKE2b-256 28322a28992f00e055f9b530c3ae16310239d7f62b6e44b55d651271faefd97e

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