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()
>>> ch.new_block(
...     package='example',
...     version='0.1',
...     distributions='unstable',
...     urgency='low',
...     author="%s <%s>" % debian.changelog.get_maintainer(),
...     date=debian.changelog.format_date()
... )
>>> ch.add_change('')
>>> print(ch)
example (0.1) unstable; urgency=low

-- Stuart Prescott <stuart@debian.org>  Sun, 08 Apr 2018 13:03:01 +1000

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=None, version=None, distributions=None, urgency=None, urgency_comment=None, changes=None, author=None, date=None, other_pairs=None, encoding='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()
_get_bugs_closed_generic(type_re)
_get_version()
_set_version(version)
add_change(change)

Append a change entry to the block

add_trailing_line(line)

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

bugs_closed

List of (Debian) bugs closed by the block

changes()

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

lp_bugs_closed

List of Launchpad bugs closed by the block

other_keys_normalised()

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

version

The package version that this block pertains to

class debian.changelog.Changelog(file=None, max_blocks=None, allow_empty_author=False, strict=False, encoding='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 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()
static _parse_error(message, strict)
_raw_versions()
add_change(change)

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.

author

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

date

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

debian_revision

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

debian_version

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

distributions

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

epoch

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

full_version

The full version number of the last version

get_package()

Returns the name of the package in the last entry.

get_version()

Return a Version object for the last version

get_versions()

Returns a list of version objects that the package went through.

new_block(**kwargs)

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.

package

Name of the package in the last version

parse_changelog(file, max_blocks=None, allow_empty_author=False, strict=True, encoding=None)

Read and parse a changelog file

If you create an Changelog object without sepcifying 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)

set the author of the top changelog entry

set_date(date)

set the date of the top changelog entry

Parameters:date – str a properly formatted date string (date -R format; see Policy)
set_distributions(distributions)
set_package(package)

set the name of the package in the last entry.

set_urgency(urgency)
set_version(version)

Set the version of the last changelog block

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

upstream_version

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

urgency

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

version

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

versions

A list of debian.debian_support.Version objects that the package went through. These version objects provide all version attributes such as epoch, debian_revision, upstream_version. These attributes cannot be assigned to.

write_to_open_file(filehandle)

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)

Bases: Exception

Indicates that the changelog could not be parsed

is_user_error = True
class debian.changelog.Version(version)

Bases: debian.debian_support.Version

Represents a version of a Debian package.

exception debian.changelog.VersionError(version)

Bases: Exception

Indicates that the version does not conform to the required format

is_user_error = True
debian.changelog.format_date(timestamp=None, localtime=True)

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()

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 informations as approriate.

It uses the same methods 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.