debian.changelog module

Facilities for reading and writing Debian changelogs

The aim of this module is to provide programmatic access to Debian changelogs to query and manipulate them. The format for the changelog is defined in deb-changelog(5)

Stability: The API is not marked as stable but hasn’t changed incompatibly since 2007. Potential users of these classes are asked to work with the python-debian maintainers to improve, extend and stabilise this API.

Overview

Create a changelog object using the constuctor. Pass it the contents of the file if there are some entries, or None to create an empty changelog:

>>> import debian.changelog
>>> ch = debian.changelog.Changelog()
>>> maintainer, email = 'John Doe', 'joe@example.com'
>>> timestamp = 1617222715
>>> # You might want to use get_maintainer() a la:
>>> # maintainer, email = debian.changelog.get_maintainer()
>>> ch.new_block(
...     package='example',
...     version='0.1',
...     distributions='unstable',
...     urgency='low',
...     author="%s <%s>" % (maintainer, email),
...     # You can also omit timestamp, if you are fine with "now"
...     # We use a hard-coded timestamp for deterministic output
...     date=debian.changelog.format_date(timestamp=1617222715, localtime=False)
... )
>>> ch.add_change('')
>>> ch.add_change(' * Some change')
>>> ch.add_change('')
>>> print(ch, end='')
example (0.1) unstable; urgency=low

 * Some change

 -- John Doe <joe@example.com>  Wed, 31 Mar 2021 20:31:55 -0000

If you have the full contents of a changelog, but are only interested in the most recent versions you can pass the max_blocks keyword parameter to the constuctor to limit the number of blocks of the changelog that will be parsed. If you are only interested in the most recent version of the package then pass max_blocks=1:

>>> import gzip
>>> from debian.changelog import Changelog
>>> with gzip.open('/usr/share/doc/dpkg/changelog.Debian.gz') as fh:  
...     ch = Changelog(fh, max_blocks=1)
>>> print('''
...     Package: %s
...     Version: %s
...     Urgency: %s''' % (ch.package, ch.version, ch.urgency))  
    Package: dpkg
    Version: 1.18.24
    Urgency: medium

See /usr/share/doc/python-debian/examples/changelog/ or the git repository for examples of usage.

The Changelog class is the key class within this module.

Changelog Classes

class debian.changelog.ChangeBlock(package: str | None = None, version: Version | str | None = None, distributions: str | None = None, urgency: str | None = None, urgency_comment: str | None = None, changes: List[str] | None = None, author: str | None = None, date: str | None = None, other_pairs: Dict[str, str] | None = None, encoding: str = 'utf-8')

Bases: object

Holds all the information about one block from the changelog.

See deb-changelog(5) for more details about the format of the changelog block and the necessary data.

Parameters:
  • package – str, name of the package

  • version – str or Version, version of the package

  • distributions – str, distributions to which the package is released

  • urgency – str, urgency of the upload

  • urgency_comment – str, comment about the urgency setting

  • changes – list of str, individual changelog entries for this block

  • author – str, name and email address of the changelog author

  • date – str, date of the changelog in RFC822 (date -R) format

  • other_pairs – dict, key=value pairs from the header of the changelog, other than the urgency value that is specified separately

  • encoding – specify the encoding to be used; note that Debian Policy mandates the use of UTF-8.

_format(allow_missing_author: bool | None = False) str
_get_bugs_closed_generic(type_re: Pattern[str]) List[int]
_get_version() Version | None
_set_version(version: Version | str | None) None
add_change(change: str) None

Append a change entry to the block

add_trailing_line(line: str) None

Add a sign-off (trailer) line to the block

property bugs_closed: List[int]

List of (Debian) bugs closed by the block

changes() List[str]

Get the changelog entries for this block as a list of str

property lp_bugs_closed: List[int]

List of Launchpad bugs closed by the block

other_keys_normalised() Dict[str, str]

Obtain a dict from the block header (other than urgency)

property version: Version | None

The package version that this block pertains to

class debian.changelog.Changelog(file: bytes | str | IO[str] | Iterable[str] | Iterable[bytes] | None = None, max_blocks: int | None = None, allow_empty_author: bool = False, strict: bool = False, encoding: str = 'utf-8')

Bases: object

Represents a debian/changelog file.

To get the properly formatted changelog back out of the object merely call str() on it. The returned string should be a properly formatted changelog.

Parameters:
  • file – str, list of str, or file-like. The contents of the changelog, either as a str, unicode object, or an iterator of lines such as a filehandle, (each line is either a str or unicode)

  • max_blocks – int, optional (Default: None, no limit) The maximum number of blocks to parse from the input.

  • allow_empty_author – bool, optional (Default: False), Whether to allow an empty author in the trailer line of a change block.

  • strict – bool, optional (Default: False, use a warning) Whether to raise an exception if there are errors.

  • encoding – str, If the input is a str or iterator of str, the encoding to use when interpreting the input.

There are a number of errors that may be thrown by the module:

  • ChangelogParseError: Indicates that the changelog could not be parsed, i.e. there is a line that does not conform to the requirements, or a line was found out of its normal position. May be thrown when using the method parse_changelog. The constructor will not throw this exception.

  • ChangelogCreateError: Some information required to create the changelog was not available. This can be thrown when str() is used on the object, and will occur if a required value is None.

  • VersionError: The string used to create a Version object cannot be parsed as it doesn’t conform to the specification of a version number. Can be thrown when creating a Changelog object from an existing changelog, or instantiating a Version object directly to assign to the version attribute of a Changelog object.

If you have a changelog that may have no author information yet as it is still a work in progress, i.e. the author line is just:

--

rather than:

-- Author <author@debian.org>  Thu, 12 Dec 2006 12:23:34 +0000

then you can pass allow_empty_author=True to the Changelog constructor. If you do this then the author and date attributes may be None.

_format(allow_missing_author: bool | None = False) str
static _parse_error(message: str, strict: bool) None
_raw_versions() List[str | None]
add_change(change: str) None

and a new dot point to a changelog entry

Adds a change entry to the most recent version. The change entry should conform to the required format of the changelog (i.e. start with two spaces). No line wrapping or anything will be performed, so it is advisable to do this yourself if it is a long entry. The change will be appended to the current changes, no support is provided for per-maintainer changes.

property author

The author of the most recent change. This should be a properly formatted name/email pair.

property date

The date associated with the current entry. Should be a properly formatted string with the date and timezone. See the format_date() function.

property debian_revision

The debian part of the version number of the last version.

property debian_version

The debian part of the version number of the last version.

property distributions

A string indicating the distributions that the package will be uploaded to in the most recent version.

property epoch

The epoch number of the last revision, or None if no epoch was used.

property full_version

The full version number of the last version

get_package() str | None

Returns the name of the package in the last entry.

get_version() Version | None

Return a Version object for the last version

get_versions() List[Version]
new_block(package: str | None = None, version: Version | str | None = None, distributions: str | None = None, urgency: str | None = None, urgency_comment: str | None = None, changes: List[str] | None = None, author: str | None = None, date: str | None = None, other_pairs: Dict[str, str] | None = None, encoding: str | None = None) None

Add a new changelog block to the changelog

Start a new ChangeBlock entry representing a new version of the package. The arguments (all optional) are passed directly to the ChangeBlock constructor; they specify the values that can be provided to the set_* methods of this class. If they are omitted the associated attributes must be assigned to before the changelog is formatted as a str or written to a file.

property package: str | None

Name of the package in the last version

parse_changelog(file: bytes | str | IO[str] | Iterable[str] | Iterable[bytes] | None, max_blocks: int | None = None, allow_empty_author: bool = False, strict: bool = True, encoding: str | None = None) None

Read and parse a changelog file

If you create an Changelog object without specifying a changelog file, you can parse a changelog file with this method. If the changelog doesn’t parse cleanly, a ChangelogParseError exception is thrown. The constructor will parse the changelog on a best effort basis.

set_author(author: str) None

set the author of the top changelog entry

set_date(date: str) None

set the date of the top changelog entry

Parameters:

date – str a properly formatted date string (date -R format; see Policy)

set_distributions(distributions: str) None
set_package(package: str) None

set the name of the package in the last entry.

set_urgency(urgency: str) None
set_version(version: Version | str) None

Set the version of the last changelog block

version can be a full version string, or a Version object

property upstream_version

The upstream part of the version number of the last version.

property urgency

A string indicating the urgency with which the most recent version will be uploaded.

property version: Version | None

Version object for latest changelog block. (Property that can both get and set the version.)

property versions: List[Version]

Returns a list of debian.debian_support.Version objects that are listed in the changelog.

write_to_open_file(filehandle: IO[str]) None

Write the changelog entry to a filehandle

Write the changelog out to the filehandle passed. The file argument must be an open file object.

exception debian.changelog.ChangelogCreateError

Bases: Exception

Indicates that changelog could not be created, as all the information required was not given

exception debian.changelog.ChangelogParseError(line: str)

Bases: Exception

Indicates that the changelog could not be parsed

is_user_error = True
exception debian.changelog.VersionError(version: str)

Bases: Exception

Indicates that the version does not conform to the required format

is_user_error = True
debian.changelog.format_date(timestamp: float | None = None, localtime: bool = True) str

format a datestamp in the required format for the changelog

Parameters:
  • timestamp – float, optional. The timestamp (seconds since epoch) for which the date string should be created. If not specified, the current time is used.

  • localtime – bool, optional (default True). Use the local timezone in the date string.

Returns:

str, date stamp formatted according to the changelog specification (i.e. RFC822).

debian.changelog.get_maintainer() Tuple[str | None, str | None]

Get the maintainer information in the same manner as dch.

This function gets the information about the current user for the maintainer field using environment variables of gecos information as appropriate.

It uses the same algorithm as dch to get the information, namely DEBEMAIL, DEBFULLNAME, EMAIL, NAME, /etc/mailname and gecos.

Returns:

a tuple of the full name, email pair as strings. Either of the pair may be None if that value couldn’t be determined.