Skip to main content

specfile.specfile

Specfile

class Specfile()

Class that represents a spec file.

Attributes:

  • autosave - Whether to automatically save any changes made.

__init__

def __init__(path: Union[Path, str],
sourcedir: Optional[Union[Path, str]] = None,
autosave: bool = False,
macros: Optional[List[Tuple[str, Optional[str]]]] = None,
force_parse: bool = False) -> None

Initializes a specfile object.

Arguments:

  • path - Path to the spec file.
  • sourcedir - Path to sources and patches.
  • autosave - Whether to automatically save any changes made.
  • macros - List of extra macro definitions.
  • force_parse - Whether to attempt to parse the spec file even if one or more sources required to be present at parsing time are not available. Such sources include sources referenced from shell expansions in tag values and sources included using the %include directive.

path

@property
def path() -> Path

Path to the spec file.

sourcedir

@property
def sourcedir() -> Path

Path to sources and patches.

macros

@property
def macros() -> List[Tuple[str, Optional[str]]]

List of extra macro definitions.

force_parse

@property
def force_parse() -> bool

Whether to attempt to parse the spec file even if one or more sources required to be present at parsing time are not available.

tainted

@property
def tainted() -> bool

Indication that parsing of the spec file was forced and one or more sources required to be present at parsing time were not available and were replaced with dummy files.

rpm_spec

@property
def rpm_spec() -> rpm.spec

Underlying rpm.spec instance.

reload

def reload() -> None

Reloads the spec file content.

save

def save() -> None

Saves the spec file content.

expand

def expand(expression: str,
extra_macros: Optional[List[Tuple[str, Optional[str]]]] = None,
skip_parsing: bool = False) -> str

Expands an expression in the context of the spec file.

Arguments:

  • expression - Expression to expand.
  • extra_macros - Extra macros to be defined before expansion is performed.
  • skip_parsing - Do not parse the spec file before expansion is performed. Defaults to False. Mutually exclusive with extra_macros. Set this to True only if you are certain that the global macro context is up-to-date.

Returns:

Expanded expression.

get_active_macros

def get_active_macros() -> List[Macro]

Gets active macros in the context of the spec file.

This includes built-in RPM macros, macros loaded from macro files and macros defined in the spec file itself.

Returns:

List of Macro objects.

lines

@ContextManager
def lines() -> Generator[List[str], None, None]

Context manager for accessing spec file lines.

Yields:

Spec file lines as list of strings.

macro_definitions

@ContextManager
def macro_definitions() -> Generator[MacroDefinitions, None, None]

Context manager for accessing macro definitions.

Yields:

Macro definitions in the spec file as MacroDefinitions object.

sections

@ContextManager
def sections() -> Generator[Sections, None, None]

Context manager for accessing spec file sections.

Yields:

Spec file sections as Sections object.

parsed_sections

@property
def parsed_sections() -> Sections

Parsed spec file sections.

tags

@ContextManager
def tags(
section: Union[str,
Section] = "package") -> Generator[Tags, None, None]

Context manager for accessing tags in a specified section.

Arguments:

  • section - Name of the requested section or an existing Section instance. Defaults to preamble.

Yields:

Tags in the section as Tags object.

changelog

@ContextManager
def changelog(
section: Optional[Section] = None
) -> Generator[Optional[Changelog], None, None]

Context manager for accessing changelog.

Arguments:

  • section - Optional Section instance to be processed. If not set, the first %changelog section (if any) will be processed.

Yields:

Spec file changelog as Changelog object or None if there is no %changelog section.

prep

@ContextManager
def prep() -> Generator[Optional[Prep], None, None]

Context manager for accessing %prep section.

Yields:

Spec file %prep section as Prep object.

sources

@ContextManager
def sources(
allow_duplicates: bool = False,
default_to_implicit_numbering: bool = False,
default_source_number_digits: int = 1
) -> Generator[Sources, None, None]

Context manager for accessing sources.

Arguments:

  • allow_duplicates - Whether to allow duplicate entries when adding new sources.
  • default_to_implicit_numbering - Use implicit numbering (no source numbers) by default.
  • default_source_number_digits - Default number of digits in a source number.

Yields:

Spec file sources as Sources object.

patches

@ContextManager
def patches(
allow_duplicates: bool = False,
default_to_implicit_numbering: bool = False,
default_source_number_digits: int = 1
) -> Generator[Patches, None, None]

Context manager for accessing patches.

Arguments:

  • allow_duplicates - Whether to allow duplicate entries when adding new patches.
  • default_to_implicit_numbering - Use implicit numbering (no source numbers) by default.
  • default_source_number_digits - Default number of digits in a source number.

Yields:

Spec file patches as Patches object.

has_autorelease

@property
def has_autorelease() -> bool

Whether the spec file uses %autorelease.

contains_autochangelog

@staticmethod
def contains_autochangelog(section: Section) -> bool

Determines if the specified section contains the %autochangelog macro.

Arguments:

  • section - Section to examine.

Returns:

True if the section contains %autochangelog, False otherwise.

has_autochangelog

@property
def has_autochangelog() -> bool

Whether the spec file uses %autochangelog.

add_changelog_entry

def add_changelog_entry(entry: Union[str, List[str]],
author: Optional[str] = None,
email: Optional[str] = None,
timestamp: Optional[Union[datetime.date,
datetime.datetime]] = None,
evr: Optional[str] = None) -> None

Adds a new %changelog entry. Does nothing if there is no %changelog section or if %autochangelog is being used.

If not specified, author and e-mail will be automatically determined, if possible. Timestamp, if not set, will be set to current time (in local timezone).

Arguments:

  • entry - Entry text or list of entry lines.
  • author - Author of the entry.
  • email - E-mail of the author.
  • timestamp - Timestamp of the entry. Supply datetime rather than date for extended format.
  • evr - Override the EVR part of the changelog entry. Macros will be expanded automatically. By default, the function determines the appropriate value based on the spec file current %{epoch}, %{version}, and %{release} values.

release

@property
def release() -> str

Release string without the dist suffix.

expanded_release

@property
def expanded_release() -> str

Release string without the dist suffix with macros expanded.

set_version_and_release

def set_version_and_release(version: str, release: str = "1") -> None

Sets both version and release at the same time.

Arguments:

  • version - Version string.
  • release - Release string, defaults to "1".

add_patch

def add_patch(location: str,
number: Optional[int] = None,
comment: Optional[str] = None,
initial_number: int = 0,
number_digits: int = 4) -> None

Adds a patch.

Arguments:

  • location - Patch location (filename or URL).
  • number - Patch number. It will be auto-assigned if not specified. If specified, it must be higher than any existing patch number.
  • comment - Associated comment.
  • initial_number - Auto-assigned number to start with if there are no patches.
  • number_digits - Number of digits in the patch number.

Raises:

  • SourceNumberException - If the specified patch number is not higher than any existing patch number.

update_value

def update_value(value: str,
requested_value: str,
position: int,
protected_entities: Optional[str] = None) -> str

Updates a value from within the context of the spec file with a new value, but tries to preserve substitutions of locally defined macros and tags, updating the respective macro definitions and tag values instead.

Arguments:

  • value - Value to update.
  • requested_value - Requested new value.
  • position - Position (line number) of the value in the spec file.
  • protected_entities - Regular expression specifying protected tags and macro definitions, ensuring their values won't be updated.

Returns:

Updated value. Can be equal to the original value.

update_tag

def update_tag(name: str,
value: str,
protected_entities: str = ".*name") -> None

Updates value of the given tag, trying to preserve substitutions of locally defined macros and tags, updating the respective macro definitions and tag values instead.

Arguments:

  • name - Tag name.
  • value - Requested new value.
  • protected_entities - Regular expression specifying protected tags and macro definitions, ensuring their values won't be updated.

update_version

def update_version(
version: str,
prerelease_suffix_pattern: Optional[str] = None,
prerelease_suffix_macro: Optional[str] = None,
comment_out_style: CommentOutStyle = CommentOutStyle.DNL) -> None

Updates spec file version.

If prerelease_suffix_pattern is not set, this method is equivalent to calling update_tag("Version", version). If prerelease_suffix_pattern is set and the specified version matches it, the detected pre-release suffix is prepended with '~' (any existing delimiter is removed) before updating Version to ensure proper sorting by RPM. If prerelease_suffix_macro is also set and such macro definition exists, it is commented out or uncommented accordingly before updating Version.

Arguments:

  • version - Version string.
  • prerelease_suffix_pattern - Regular expression specifying recognized pre-release suffixes. The first capturing group must capture the delimiter between base version and pre-release suffix and can be empty in case there is no delimiter.
  • prerelease_suffix_macro - Macro definition that controls whether spec file version is a pre-release and contains the pre-release suffix. To be commented out or uncommented accordingly.
  • comment_out_style - Style of commenting out prerelease_suffix_macro. See CommentOutStyle. Defaults to update_tag("Version", version)0.

Raises:

  • update_tag("Version", version)1 - If prerelease_suffix_pattern is invalid.