debian_inspector.version module

class debian_inspector.version.Version(epoch=0, upstream=None, revision='0')

Bases: object

Rich comparison of Debian package versions as first-class Python objects.

The Version class is a subclass of the built in str type that implements rich comparison according to the version sorting order defined in the Debian Policy Manual. Use it to sort Debian package versions from oldest to newest in ascending version order like this:

>>> from debian_inspector.version import Version
>>> unsorted = ['0.1', '0.5', '1.0', '2.0', '3.0', '1:0.4', '2:0.3']
>>> print([str(v) for v in sorted(Version.from_string(s) for s in unsorted)])
['0.1', '0.5', '1.0', '2.0', '3.0', '1:0.4', '2:0.3']

This example uses ‘epoch’ numbers (the numbers before the colons) to demonstrate that this version sorting order is different from regular sorting and ‘natural order sorting’.

compare(other_version)
epoch
classmethod from_string(version)
revision
to_dict()
tuple()
upstream
debian_inspector.version.coerce_version(value)

Return a Version object from value.

Parameters:

value – The value to coerce (a string or Version object).

Returns:

A Version object.

debian_inspector.version.compare_strings(version1, version2)

Compare two version strings (upstream or revision) using Debain semantics and return one of the following integer numbers:

  • -1 means version1 sorts before version2

  • 0 means version1 and version2 are equal

  • 1 means version1 sorts after version2

debian_inspector.version.compare_strings_key(x)

Return a key string function suitable for use in sorted().

debian_inspector.version.compare_version_objects(version1, version2)

Compare two Version objects and return one of the following integer numbers:

  • -1 means version1 sorts before version2

  • 0 means version1 and version2 are equal

  • 1 means version1 sorts after version2

debian_inspector.version.compare_versions(version1, version2)

Compare two Version objects or strings and return one of the following integer numbers:

  • -1 means version1 sorts before version2

  • 0 means version1 and version2 are equal

  • 1 means version1 sorts after version2

debian_inspector.version.compare_versions_key(x)

Return a key version function suitable for use in sorted().

debian_inspector.version.eval_constraint(version1, operator, version2)

Evaluate a versions constraint where two Debian package versions are compared with an operator such as < or >. Return True if the constraint is satisfied and False otherwise.

debian_inspector.version.get_digit_prefix(characters)

Return the digit prefix from a list of characters.

debian_inspector.version.get_non_digit_prefix(characters)

Return the non-digit prefix from a list of characters.

debian_inspector.version.logger = <Logger debian_inspector.version (WARNING)>

Parse, compare and sort Debian package versions.

This module is an implementation of the version comparison and sorting algorithm described at https://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version

This has been substantially modified and enhanced from the original python-dpkg Dpkg class by Nathan J. Meh and team from The Climate Corporation and code from python-deb-pkg-tools by Peter Odding to extract only the subset that does the version parsing, comparison and version constraints evaluatiob.

So much so that little of this code may still looks like the original.

Some examples:

#### Compare two arbitrary version strings

>>> from debian_inspector import debver
>>> debver.compare_versions('0:1.0-test1', '0:1.0-test2')
-1
>>> debver.compare_versions('1.0', '0.6')
1
>>> debver.compare_versions('2:1.0', '1:1.0')
-1

#### Use Version as a key function to sort a list of version strings

>>> from debian_inspector.debver import Version
>>> sorted(['0:1.0-test1', '1:0.0-test0', '0:1.0-test2'] , key=Version.from_string)
['0:1.0-test1', '0:1.0-test2', '1:0.0-test0']