version_utils.rpm module

rpm module for version_utils

Contains RPM parsing and comparison operations for version_utils. Public methods include:

  • compare_packages: compare two RPM package strings, e.g. gcc-4.4.7-16.el6.x86_64 and gcc-4.4.7-17.el6.x86_64
  • compare_versions: compare two RPM version strings (the bit between the dashes in an RPM package string)
  • package: parse an RPM package string to get name, epoch, version, release, and architecture information. Returns as a common.Package object.
version_utils.rpm._check_leading(*char_lists)[source]

Remove any non-alphanumeric or non-~ leading characters

Checks the beginning of any provided lists for non-alphanumeric or non-~ (tilde) leading characters and removes them if found. Operates on (and possibly alters) the passed list.

Parameters:char_list (list) – a list or lists of characters
Returns:None
Return type:None
version_utils.rpm._compare_blocks(block_a, block_b)[source]

Compare two blocks of characters

Compares two blocks of characters of the form returned by either the _pop_digits or _pop_letters function. Blocks should be character lists containing only digits or only letters. Both blocks should contain the same character type (digits or letters).

The method of comparison mirrors the method used by RPM. If the blocks are digit blocks, any leading zeros are trimmed, and whichever block is longer is assumed to be larger. If the resultant blocks are the same length, or if the blocks are non-numeric, they are checked for string equality and considered equal if the string equality comparison returns True. If not, whichever evaluates as greater than the other (again in string comparison) is assumed to be larger.

Parameters:
  • block_a (list) – an all numeric or all alphabetic character list
  • block_b (list) – an all numeric or all alphabetic character list. Alphabetic or numeric character should match block_a
Returns:

1 (if a is newer), 0 (if versions are equal) or -1 (if b is newer)

Return type:

int

version_utils.rpm._get_block_result(chars_a, chars_b)[source]

Get the first block from two character lists and compare

If character list a begins with a digit, the _pop_digit function is called on both lists to get blocks of all consecutive digits at the start of each list. If the length of the block returned when popping digits for b is zero (b started with a letter), a is newer. If b is of nonzero length, the blocks are compared using _compare_blocks.

If character list a begins with a letter, the _pop_letter function is called on both lists to get blocks of all consecutive letters at the start of each list. If the length of the block returned when popping letters for b is zero (b started with a digit), b is newer. If b is of nonzero length, blocks a and b are compared using _compare_blocks.

Parameters:
  • chars_a (list) – a list of characters derived from a version string
  • chars_b (list) – a list of characters derived from a version string
Returns:

1 (if a is newer), 0 (if versions are equal), or -1 (if b is newer)

Return type:

int

version_utils.rpm._pop_arch(char_list)[source]

Pop the architecture from a version string and return it

Returns any portion of a string following the final period. In rpm version strings, this corresponds to the package architecture.

Parameters:char_list (list) – an rpm version string in character list form
Returns:the parsed architecture as a string
Return type:str
version_utils.rpm._pop_digits(char_list)[source]

Pop consecutive digits from the front of list and return them

Pops any and all consecutive digits from the start of the provided character list and returns them as a list of string digits. Operates on (and possibly alters) the passed list.

Parameters:char_list (list) – a list of characters
Returns:a list of string digits
Return type:list
version_utils.rpm._pop_letters(char_list)[source]

Pop consecutive letters from the front of a list and return them

Pops any and all consecutive letters from the start of the provided character list and returns them as a list of characters. Operates on (and possibly alters) the passed list

Parameters:char_list (list) – a list of characters
Returns:a list of characters
Return type:list
version_utils.rpm._trim_zeros(*char_lists)[source]

Trim any zeros from provided character lists

Checks the beginning of any provided lists for ‘0’s and removes any such leading zeros. Operates on (and possibly) alters the passed list

Parameters:char_lists (list) – a list or lists of characters
Returns:None
Return type:None
version_utils.rpm.compare_evrs(evr_a, evr_b)[source]

Compare two EVR tuples to determine which is newer

This method compares the epoch, version, and release of the provided package strings, assuming that epoch is 0 if not provided. Comparison is performed on the epoch, then the version, and then the release. If at any point a non-equality is found, the result is returned without any remaining comparisons being performed (e.g. if the epochs of the packages differ, the versions are releases are not compared).

Parameters:
  • evr_a (tuple) – an EVR tuple
  • evr_b (tuple) – an EVR tuple
version_utils.rpm.compare_packages(rpm_str_a, rpm_str_b, arch_provided=True)[source]

Compare two RPM strings to determine which is newer

Parses version information out of RPM package strings of the form returned by the rpm -q command and compares their versions to determine which is newer. Provided strings do not require an architecture at the end, although if providing strings without architecture, the arch_provided parameter should be set to False.

Note that the packages do not have to be the same package (i.e. they do not require the same name or architecture).

Parameters:
  • rpm_str_a (str) – an rpm package string
  • rpm_str_b (str) – an rpm package string
  • arch_provided (bool) – whether package strings contain architecture information
Returns:

1 (a is newer), 0 (versions are equivalent), or -1 (b is newer)

Return type:

int

version_utils.rpm.compare_versions(version_a, version_b)[source]

Compare two RPM version strings

Compares two RPM version strings and returns an integer indicating the result of the comparison. The method of comparison mirrors that used by RPM, so results should be the same for any standard RPM package.

To perform the comparison, the strings are first checked for equality. If they are equal, the versions are equal. Otherwise, each string is converted to a character list, and a comparison loop is started using these lists.

In the comparison loop, first any non-alphanumeric, non-~ characters are trimmed from the front of the list. Then if the first character from both a and b is a ~ (tilde), it is trimmed. The ~ (tilde) character indicates that a given package or version should be considered older (even if it is numerically larger), so if a begins with a tilde, b is newer, and vice-versa. At this point, if the length of either list has been reduced to 0, the loop is exited. If characters remain in the list, the _get_block_result function is used to pop consecutive digits or letters from the front of hte list and compare them. The result of the block comparison is returned if the blocks are not equal. The loop then begins again.

If the loop exits without returning a value, the lengths of the remaining character lists are compared. If they have the same length (usually 0, since all characters have been popped), they are considered to be equal. Otherwise, whichever is longer is considered to be newer. Generally, unequal length will be due to one character list having been completely consumed while some characters remain on the other, for example when comparing 1.05b to 1.05.

Parameters:
  • version_a (unicode) – An RPM version or release string
  • version_b (unicode) – An RPM version or release string
Returns:

1 (if a is newer), 0 (if versions are equal), or -1 (if b is newer)

Return type:

int

Raises:

RpmError – if an a type is passed that cannot be converted to a list

version_utils.rpm.labelCompare(evr_a, evr_b)[source]

Convenience function to provide the same behaviour as labelCompare from rpm-python.

To be used as a drop-in replacement for labelCompare, thus the utilization of the non-standard camelCase variable name.

To use the version_utils version and fall back to rpm:

try:
from version_utils.rpm import labelCompare
except ImportError:
from rpm import labelCompare
Parameters:
  • evr_a (tuple) – an EVR tuple
  • evr_b (tuple) – an EVR tuple
version_utils.rpm.package(package_string, arch_included=True)[source]

Parse an RPM version string

Parses most (all tested) RPM version strings to get their name, epoch, version, release, and architecture information. Epoch (also called serial) is an optional component for RPM versions, and it is also optional when providing a version string to this function. RPM assumes the epoch to be 0 if it is not provided, so that behavior is mirrored here.

Parameters:
  • package_string (str) –
  • arch_included (bool) –
Returns:

A common.Package object containing all parsed information

Return type:

common.Package

version_utils.rpm.parse_package(package_string, arch_included=True)[source]

Parse an RPM version string to get name, version, and arch

Splits most (all tested) RPM version strings into name, epoch, version, release, and architecture. Epoch (also called serial) is an optional component of RPM versioning and is also optional in version strings provided to this function. RPM assumes the epoch to be 0 if it is not provided, so that behavior is mirrored here.

Deprecated since version 0.2.0. Use rpm.package instead.

Parameters:
  • package_string (str) – an RPM version string of the form returned by the rpm -q command
  • arch_included (bool) – default True - version strings may optionally be provided without the trailing architecture. If providing such strings, set this option to False
Returns:

a dictionary with all parsed package information

Return type:

dict