0
  !   ‘ˆÍ�þZxM¬Ùd�€ŽW¤#@þæ„Í8ÿåä¿â ?÷     ‘"""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+
"""

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
import os

try:
    from typing import Iterable, Optional, IO, List, Dict, Union
    from os import PathLike
except ImportError:
    pass

import collections.abc


def _parse_table_file(fd):
    # type: (IO[str]) -> Iterable[List[str]]
    for line in fd:
        line = line.rstrip()
        if not line or line.startswith("#"):
            continue
        yield line.split()


_QuadTuple = collections.namedtuple("_QuadTuple", ['api_name', 'libc_name', 'os_name', 'cpu_name'])


class QuadTupleDpkgArchitecture(_QuadTuple):
    """Implementation detail of ArchTable"""

    def __contains__(self, item):
        # type: (object) -> bool
        if isinstance(item, QuadTupleDpkgArchitecture):
            # This covers both equal and wildcard matches and semantically matches how dpkg does it
            return self.api_name in ('any', item.api_name) \
                   and self.libc_name in ('any', item.libc_name) \
                   and self.os_name in ('any', item.os_name) \
                   and self.cpu_name in ('any', item.cpu_name)
        return super().__contains__(item)

    @property
    def is_wildcard(self):
        # type: () -> bool
        return any(x == 'any' for x in self)


class DpkgArchTable:

    def __init__(self, arch2tuple):
        # type: (Dict[str, QuadTupleDpkgArchitecture]) -> None
        self._arch2table = arch2tuple
        self._wildcard_cache = {
            'any': QuadTupleDpkgArchitecture('any', 'any', 'any', 'any')
        }  # type: Dict[str, QuadTupleDpkgArchitecture]

    @classmethod
    def load_arch_table(cls, path='/usr/share/dpkg'):
        # type: (Union[str, PathLike[str]]) -> DpkgArchTable
        # NOTE! This method is stubbed in including doctests to support non-Debian systems
        #   See conftest.py for the concrete implementation and the limited data set available.
        """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.

        :param 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")
        """
        tupletable_path = os.path.join(path, 'tupletable')
        cputable_path = os.path.join(path, 'cputable')
        triplet_compat = False
        if not os.path.exists(tupletable_path):
            triplettable_path = os.path.join(path, 'triplettable')
            if os.path.join(triplettable_path):
                triplet_compat = True
                tupletable_path = triplettable_path

        with open(tupletable_path, encoding='utf-8') as tuple_fd,\
                o