debian._arch_table module

architecture matching

This leverages code from dpkg’s Dpkg::Arch as well as python rewrites from other people. Copyright years imported from the sources.

@copyright: 2006-2015 Guillem Jover <guillem@debian.org> @copyright: 2014, Ansgar Burchardt <ansgar@debian.org> @copyright: 2014-2017, Johannes Schauer Marin Rodrigues <josch@debian.org> @copyright: 2022, Niels Thykier <niels@thykier.net> @license: GPL-2+

class debian._arch_table.DpkgArchTable(arch2tuple: Dict[str, QuadTupleDpkgArchitecture])

Bases: object

_dpkg_arch_to_tuple(dpkg_arch: str) QuadTupleDpkgArchitecture
_dpkg_wildcard_to_tuple(arch: str) QuadTupleDpkgArchitecture
classmethod _from_file(tuple_table_fd: IO[str], cpu_table_fd: IO[str], triplet_compat: bool = False) DpkgArchTable
architecture_equals(arch1: str, arch2: str) bool

Determine whether two dpkg architecture are exactly the same [debarch_eq]

Unlike Python’s == operator, this method also accounts for things like linux-amd64 is a valid spelling of the dpkg architecture amd64 (i.e., architecture_equals(“linux-amd64”, “amd64”) is True).

This method is the closest match to dpkg’s Dpkg::Arch::debarch_eq function.

>>> arch_table = DpkgArchTable.load_arch_table()
>>> arch_table.architecture_equals("linux-amd64", "amd64")
True
>>> arch_table.architecture_equals("amd64", "linux-i386")
False
>>> arch_table.architecture_equals("i386", "linux-amd64")
False
>>> arch_table.architecture_equals("amd64", "amd64")
True
>>> arch_table.architecture_equals("i386", "amd64")
False
>>> # Compatibility with dpkg: if the parameters are equal, then it always return True
>>> arch_table.architecture_equals("unknown", "unknown")
True

Compatibility note: The method emulates Dpkg::Arch::debarch_eq function and therefore returns True if both parameters are the same even though they are wildcards or not known to be architectures.

Parameters:
  • arch1 – A string representing a dpkg architecture.

  • arch2 – A string representing a dpkg architecture.

Returns:

True if the dpkg architecture parameters are (logically) the exact same.

architecture_is_concerned(architecture: str, architecture_restrictions: Iterable[str], allow_mixing_positive_and_negative: bool = False) bool

Determine if a dpkg architecture is part of a list of restrictions [debarch_is_concerned]

This method is the closest match to dpkg’s Dpkg::Arch::debarch_is_concerned function.

Compatibility notes:
  • The Dpkg::Arch::debarch_is_concerned function allow matching of negative and positive restrictions by default. Often, this behaviour is not allowed nor recommended and the Debian Policy does not allow this practice in e.g., Build-Depends. Therefore, this implementation defaults to raising ValueError when this occurs. If the original behaviour is needed, set allow_mixing_positive_and_negative to True.

  • The Dpkg::Arch::debarch_is_concerned function is lazy and exits as soon as it finds a match. This means that if negative and positive restrictions are mixed, then order of the matches are important. This adaption matches that behaviour (provided that allow_mixing_positive_and_negative is set to True)

>>> arch_table = DpkgArchTable.load_arch_table()
>>> arch_table.architecture_is_concerned("linux-amd64", ["amd64", "i386"])
True
>>> arch_table.architecture_is_concerned("amd64", ["!amd64", "!i386"])
False
>>> # This is False because the "!amd64" is matched first.
>>> arch_table.architecture_is_concerned("linux-amd64", ["!linux-amd64", "linux-any"],
...                                      allow_mixing_positive_and_negative=True)
False
>>> # This is True because the "linux-any" is matched first.
>>> arch_table.architecture_is_concerned("linux-amd64", ["linux-any", "!linux-amd64"],
...                                      allow_mixing_positive_and_negative=True)
True
Parameters:
  • architecture – A string representing a dpkg architecture/wildcard.

  • architecture_restrictions – A list of positive (amd64) or negative (!amd64) dpkg architectures or/and wildcards.

  • allow_mixing_positive_and_negative – If True, the architecture_restrictions list can mix positive and negative (e.g., [“!any-amd64”, “any”]) restrictions. If False, mixing will trigger a ValueError.

Returns:

True if architecture is accepted by the architecture_restrictions.

is_wildcard(wildcard: str) bool

Determine if a given string is a dpkg wildcard [debarch_is_wildcard]

This method is the closest match to dpkg’s Dpkg::Arch::debarch_is_wildcard function.

>>> arch_table = DpkgArchTable.load_arch_table()
>>> arch_table.is_wildcard("linux-any")
True
>>> arch_table.is_wildcard("amd64")
False
>>> arch_table.is_wildcard("unknown")
False
>>> # Compatibility with the dpkg version of the function.
>>> arch_table.is_wildcard("unknown-any")
True
Compatibility note: The original dpkg function does not ensure that the wildcard matches

any supported architecture and this re-implementation matches that behaviour. Therefore, this method can return True for a wildcard that can never match anything in practice.

Parameters:

wildcard – A string that might represent a dpkg architecture or wildcard.

Returns:

True the parameter is a known dpkg wildcard.

classmethod load_arch_table(path: str | PathLike[str] = '/usr/share/dpkg') DpkgArchTable

Load the Dpkg Architecture Table

This class method loads the architecture table from dpkg, so it can be used.

>>> arch_table = DpkgArchTable.load_arch_table()
>>> arch_table.matches_architecture("amd64", "any")
True

The method assumes the dpkg “tuple arch” format version 1.0 or the older triplet format.

Parameters:

path – Choose a different directory for loading the architecture data. The provided directory must contain the architecture data files from dpkg (such as “tupletable” and “cputable”)

matches_architecture(architecture: str, alias: str) bool

Determine if a dpkg architecture matches another architecture or a wildcard [debarch_is]

This method is the closest match to dpkg’s Dpkg::Arch::debarch_is function.

>>> arch_table = DpkgArchTable.load_arch_table()
>>> arch_table.matches_architecture("amd64", "linux-any")
True
>>> arch_table.matches_architecture("i386", "linux-any")
True
>>> arch_table.matches_architecture("amd64", "amd64")
True
>>> arch_table.matches_architecture("i386", "amd64")
False
>>> arch_table.matches_architecture("all", "amd64")
False
>>> arch_table.matches_architecture("all", "all")
True
>>> # i386 is the short form of linux-i386. Therefore, it does not match kfreebsd-i386
>>> arch_table.matches_architecture("i386", "kfreebsd-i386")
False
>>> # Note that "armel" and "armhf" are "arm" CPUs, so it is matched by "any-arm"
>>> # (similar holds for some other architecture <-> CPU name combinations)
>>> all(arch_table.matches_architecture(n, 'any-arm') for n in ['armel', 'armhf'])
True
>>> # Since "armel" is not a valid CPU name, this returns False (the correct would be
>>> # any-arm as noted above)
>>> arch_table.matches_architecture("armel", "any-armel")
False
>>> # Wildcards used as architecture always fail (except for special cases noted in the
>>> # compatibility notes below)
>>> arch_table.matches_architecture("any-i386", "i386")
False
>>> # any-i386 is not a subset of linux-any (they only have i386/linux-i386 as overlap)
>>> arch_table.matches_architecture("any-i386", "linux-any")
False
>>> # Compatibility with dpkg - if alias is `any` then it always returns True
>>> # even if the input otherwise would not make sense.
>>> arch_table.matches_architecture("any-unknown", "any")
True
>>> # Another side effect of the dpkg compatibility
>>> arch_table.matches_architecture("all", "any")
True

Compatibility note: The method emulates Dpkg::Arch::debarch_is function and therefore returns True if both parameters are the same even though they are wildcards or not known to be architectures. Additionally, if alias is any, then this method always returns True as any is the “match-everything-wildcard”.

Parameters:
  • architecture – A string representing a dpkg architecture.

  • alias – A string representing a dpkg architecture or wildcard to match with.

Returns:

True if the architecture parameter is (logically) the same as the alias parameter OR, if alias is a wildcard, the architecture parameter is a subset of the wildcard. The method returns False if architecture is not a known dpkg architecture, or it is a wildcard.

class debian._arch_table.QuadTupleDpkgArchitecture(api_name, libc_name, os_name, cpu_name)

Bases: _QuadTuple

Implementation detail of ArchTable

property is_wildcard: bool
class debian._arch_table._QuadTuple(api_name, libc_name, os_name, cpu_name)

Bases: tuple

_asdict()

Return a new dict which maps field names to their values.

_field_defaults = {}
_fields = ('api_name', 'libc_name', 'os_name', 'cpu_name')
classmethod _make(iterable)

Make a new _QuadTuple object from a sequence or iterable

_replace(**kwds)

Return a new _QuadTuple object replacing specified fields with new values

api_name

Alias for field number 0

cpu_name

Alias for field number 3

libc_name

Alias for field number 1

os_name

Alias for field number 2

debian._arch_table._parse_table_file(fd: IO[str]) Iterable[List[str]]