File: debarch.py

package info (click to toggle)
botch 0.21-8
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,298,428 kB
  • sloc: xml: 11,924,948; ml: 4,497; python: 3,620; sh: 1,269; makefile: 319
file content (111 lines) | stat: -rw-r--r-- 3,446 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""architecture matching

@copyright: 2014, Ansgar Burchardt <ansgar@debian.org>
@copyright: 2014-2017, Johannes Schauer <j.schauer@email.de>
@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

# FIXME: this should be integrated into python-debian
#        see: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=771058

_cached_arch2tuple = None
_cached_tuple2arch = None
_cached_cputable = None


def _load_table(path):
    table = []
    with open(path, "r") as f:
        for line in f:
            if not line or line.startswith('#'):
                continue
            table.append(line.split())
    return table


def _read_cputable():
    global _cached_cputable
    if _cached_cputable is None:
        _cached_cputable = _load_table('/usr/share/dpkg/cputable')
    return _cached_cputable


def _read_tupletable():
    global _cached_arch2tuple, _cached_tuple2arch
    if _cached_arch2tuple is None or _cached_tuple2arch is None:
        table = _load_table('/usr/share/dpkg/tupletable')
        arch2tuple = dict()
        tuple2arch = dict()
        for row in table:
            debtuple = row[0]
            debarch = row[1]
            if '<cpu>' in debtuple:
                for r in _read_cputable():
                    cpu = r[0]
                    dt = debtuple.replace('<cpu>', cpu)
                    da = debarch.replace('<cpu>', cpu)
                    arch2tuple[da] = dt
                    tuple2arch[dt] = da
            else:
                arch2tuple[debarch] = debtuple
                tuple2arch[debtuple] = debarch
        _cached_arch2tuple = arch2tuple
        _cached_tuple2arch = tuple2arch
    return _cached_tuple2arch, _cached_arch2tuple


def debwildcard_to_debtuple(arch):
    arch_tuple = arch.split('-', 3)

    if 'any' in arch_tuple:
        if len(arch_tuple) == 4:
            return arch_tuple
        elif len(arch_tuple) == 3:
            return ('any', arch_tuple[0], arch_tuple[1], arch_tuple[2])
        elif len(arch_tuple) == 2:
            return ('any', 'any', arch_tuple[0], arch_tuple[1])
        else:
            return ('any', 'any', 'any', 'any')
    else:
        return debarch_to_debtuple(arch)


def debarch_to_debtuple(arch):
    if (arch.startswith("linux-")):
        arch = arch[6:]

    tuple = _read_tupletable()[1].get(arch)

    if tuple is None:
        return
    return tuple.split('-', 3)


def match_architecture(real, alias):
    if alias == real or alias == "any":
        return True

    real = debarch_to_debtuple(real)
    alias = debwildcard_to_debtuple(alias)

    if real is None or len(real) != 4 or alias is None or len(alias) != 4:
        return False

    for i in range(0, 4):
        if (alias[i] != real[i] and alias[i] != "any"):
            return False
    return True