)uyc%HdZddlZ ddlmZmZmZmZmZmZddlm Z n #e $rYnwxYwddl Z dZ e jdgdZGdd eZGd d ZdS) aarchitecture 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 @copyright: 2014, Ansgar Burchardt @copyright: 2014-2017, Johannes Schauer Marin Rodrigues @copyright: 2022, Niels Thykier @license: GPL-2+ N)IterableOptionalIOListDictUnion)PathLikec#K|D]D}|}|r|dr.|VEdS)N#)rstrip startswithsplit)fdlines 4/usr/lib/python3/dist-packages/debian/_arch_table.py_parse_table_filer%sa{{}} ts++  jjll  _QuadTuple)api_name libc_nameos_namecpu_namec8eZdZdZfdZedZxZS)QuadTupleDpkgArchitecturez"Implementation detail of ArchTablect|tr@|jd|jfvo/|jd|jfvo|jd|jfvo|jd|jfvSt |SNany) isinstancerrrrrsuper __contains__)selfitem __class__s rr z&QuadTupleDpkgArchitecture.__contains__4s d5 6 6 ?=UDM$::?~%)@@?|t|'<<?} (>> ?ww##D)))rc4td|DS)Nc3"K|] }|dkV dS)rN.0xs r z8QuadTupleDpkgArchitecture.is_wildcard..As&,,!1:,,,,,,r)r)r!s r is_wildcardz%QuadTupleDpkgArchitecture.is_wildcard>s!,,t,,,,,,r)__name__ __module__ __qualname____doc__r propertyr+ __classcell__)r#s@rrr1sY,,*****--X-----rrcleZdZdZed dZeddZdZdZdZ d Z dd Z d Z d S) DpkgArchTablecF||_dtddddi|_dSr) _arch2tabler_wildcard_cache)r! arch2tuples r__init__zDpkgArchTable.__init__Fs.% ,UE5%HH r/usr/share/dpkgc<tj|d}tj|d}d}tj|sCtj|d}tj|rd}|}t |d5}t |d5}||||cd d d cd d d S#1swxYwYd d d d S#1swxYwYd S) a?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") tupletablecputableF triplettableTzutf-8)encoding)triplet_compatN)ospathjoinexistsopen _from_file)clsrAtupletable_path cputable_pathr?triplettable_pathtuple_fdcpu_fds rload_arch_tablezDpkgArchTable.load_arch_tableMs&',,t\:: T:66 w~~o.. 4 " T> B B w||-.. 4!%"3 /G 4 4 4 S]W555 S9?>>(F>>RR S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S Ss66DC9 D9C= =DC= DDDFci}dt|D}t|D]}|d}|d}|rd|z}d|vrR|D]N} |d| } |d| } t| dd|| <Oot|dd||<t |S)Ncg|] }|d S)rr&r's r z,DpkgArchTable._from_file..qsBBBQAaDBBBrrzbase-z-)rreplacerrr3) rFtuple_table_fd cpu_table_fdr?r7cpu_listrow dpkg_tuple dpkg_archr debtuple_cpu dpkg_arch_cpus rrEzDpkgArchTable._from_filems BB"3L"A"ABBB$^44 ] ]CQJAI 2$z1 *$$ (H#-#5#5gx#H#HL$-$5$5gx$H$HM0I%++C331J}--)B:CSCSTWYZC[C[(\ 9%%Z(((rc@ |j|S#t$rYnwxYw|dd}d|vrFt|dkr)|ddt|dk)t |}n||}||j|<|S)NrQrRrr)r6KeyErrorrleninsertr_dpkg_arch_to_tuple)r!arch arch_tupleresults r_dpkg_wildcard_to_tuplez%DpkgArchTable._dpkg_wildcard_to_tuples '- -    D ZZQ'' J  j//A%%!!!U+++j//A%%. ;FF--d33F%+T" s  cZ|dr |dd}|j|S)Nzlinux-)r r5)r!rYs rraz!DpkgArchTable._dpkg_arch_to_tuples3    ) ) &!!"" I **rc|d|fvrdS ||}||}n#t$rYdSwxYw||vS)a 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". :param architecture: A string representing a dpkg architecture. :param 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. rTF)rarer^)r! architecturealiasrY dpkg_wildcards rmatches_architecturez"DpkgArchTable.matches_architecturesvv UL) ) )4 00>>I 88??MM   55 M))*5 AAc||krdS ||}||}n#t$rYdSwxYw||kS)aDetermine 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. :param arch1: A string representing a dpkg architecture. :param arch2: A string representing a dpkg architecture. :returns: True if the dpkg architecture parameters are (logically) the exact same. TF)rar^)r!arch1arch2 dpkg_arch1 dpkg_arch2s rarchitecture_equalsz!DpkgArchTable.architecture_equalssoB E>>4 11%88J11%88JJ   55 Z''rmcd}d}d}t|} ||}n#t$rYdSwxYw|D]n} | dkr | ddkrd}nd}|| } d} | } | ddkr d} | dd} || } || vr| }|r|cSo|s|r|rt d||}|S) a 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 :param architecture: A string representing a dpkg architecture/wildcard. :param architecture_restrictions: A list of positive (amd64) or negative (!amd64) dpkg architectures or/and wildcards. :param 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`. NFr!TrPzarchitecture_restrictions contained mixed positive and negativerestrictions (and allow_mixing_positive_and_negative was not True))iterrar^lowerre ValueError) r!riarchitecture_restrictions"allow_mixing_positive_and_negativeverdictpositive_match_seennegative_match_seenarch_restriction_iterrYarch_restrictionverdict_if_matchedarch_restriction_positiverks rarchitecture_is_concernedz'DpkgArchTable.architecture_is_concernedss\## $%> ? ? 00>>II   55 !6! #! #  2%%"c))&*##&*#" 05577 !% (8 %"c))%*",>> 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. :param wildcard: A string that might represent a dpkg architecture or wildcard. :returns: True the parameter is a known dpkg wildcard. F)rer+r^)r!wildcardrYs rr+zDpkgArchTable.is_wildcardwsI0 )44X>>I ( (    55 s  ,,N)r9)F) r,r-r.r8 classmethodrLrErerarlrsrr+r&rrr3r3Ds   SSS[S>)))[).&+++D*D*D*L*(*(*(ZFKddddL)))))rr3)r/r@typingrrrrrrr ImportErrorcollections.abc collectionsr namedtuplerrr3r&rrrs$  2  @@@@@@@@@@@@@@@@   D $[ #L2b2b2b c c ----- ---&Q)Q)Q)Q)Q)Q)Q)Q)Q)Q)s ''