]> git.decadent.org.uk Git - dak.git/blob - daklib/architecture.py
Add by-hash support
[dak.git] / daklib / architecture.py
1 """architecture matching
2
3 @copyright: 2014, Ansgar Burchardt <ansgar@debian.org>
4 @license: GPL-2+
5 """
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21 def _load_table(path):
22     table = []
23     with open(path, 'r') as fh:
24         for line in fh:
25             if not line or line.startswith('#'):
26                 continue
27             table.append(line.split())
28     return table
29
30 _cached_cputable = None
31 def _cputable():
32     global _cached_cputable
33     if _cached_cputable is None:
34         _cached_cputable = _load_table('/usr/share/dpkg/cputable')
35     return _cached_cputable
36
37 _cached_arch2triplet = None
38 _cached_triplet2arch = None
39 def _triplettable():
40     global _cached_arch2triplet, _cached_triplet2arch
41     if _cached_arch2triplet is None or _cached_triplet2arch is None:
42         table = _load_table('/usr/share/dpkg/triplettable')
43         arch2triplet = {}
44         triplet2arch = {}
45         for row in table:
46             if '<cpu>' in row[0] or '<cpu>' in row[1]:
47                 for cpu in _cputable():
48                     replaced_row = [ column.replace('<cpu>', cpu[0]) for column in row ]
49                     arch2triplet[replaced_row[1]] = replaced_row[0]
50                     triplet2arch[replaced_row[0]] = replaced_row[1]
51             else:
52                 arch2triplet[row[1]] = row[0]
53                 triplet2arch[row[0]] = row[1]
54         _cached_arch2triplet = arch2triplet
55         _cached_triplet2arch = triplet2arch
56     return _cached_triplet2arch, _cached_arch2triplet
57
58 class InvalidArchitecture(Exception):
59     pass
60
61 def Debian_arch_to_Debian_triplet(arch):
62     parts = arch.split('-')
63
64     # Handle architecture wildcards
65     if 'any' in parts:
66         if len(parts) == 3:
67             return parts
68         elif len(parts) == 2:
69             return 'any', parts[0], parts[1]
70         else:
71             return 'any', 'any', 'any'
72
73     if len(parts) == 2 and parts[0] == 'linux':
74         arch = parts[1]
75
76     triplet = _triplettable()[1].get(arch, None)
77     if triplet is None:
78         return None
79     return triplet.split('-', 2)
80
81 def match_architecture(arch, wildcard):
82     # 'all' has no valid triplet
83     if arch == 'all' or wildcard == 'all':
84         return arch == wildcard
85     if wildcard is 'any' or arch == wildcard:
86         return True
87
88     triplet_arch = Debian_arch_to_Debian_triplet(arch)
89     triplet_wildcard = Debian_arch_to_Debian_triplet(wildcard)
90
91     if triplet_arch is None or len(triplet_arch) != 3:
92         raise InvalidArchitecture('{0} is not a valid architecture name'.format(arch))
93     if triplet_wildcard is None or len(triplet_wildcard) != 3:
94         raise InvalidArchitecture('{0} is not a valid architecture name or wildcard'.format(wildcard))
95
96     for i in range(0,3):
97         if triplet_arch[i] != triplet_wildcard[i] and triplet_wildcard[i] != 'any':
98             return False
99     return True