1 """architecture matching
3 @copyright: 2014, Ansgar Burchardt <ansgar@debian.org>
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.
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.
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
21 def _load_table(path):
23 with open(path, 'r') as fh:
25 if not line or line.startswith('#'):
27 table.append(line.split())
30 _cached_cputable = None
32 global _cached_cputable
33 if _cached_cputable is None:
34 _cached_cputable = _load_table('/usr/share/dpkg/cputable')
35 return _cached_cputable
37 _cached_arch2triplet = None
38 _cached_triplet2arch = None
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')
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]
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
58 class InvalidArchitecture(Exception):
61 def Debian_arch_to_Debian_triplet(arch):
62 parts = arch.split('-')
64 # Handle architecture wildcards
69 return 'any', parts[0], parts[1]
71 return 'any', 'any', 'any'
73 triplet = _triplettable()[1].get(arch, None)
76 return triplet.split('-', 2)
78 def match_architecture(arch, wildcard):
79 # 'all' has no valid triplet
80 if arch == 'all' or wildcard == 'all':
81 return arch == wildcard
82 if wildcard is 'any' or arch == wildcard:
85 triplet_arch = Debian_arch_to_Debian_triplet(arch)
86 triplet_wildcard = Debian_arch_to_Debian_triplet(wildcard)
88 if len(triplet_arch) != 3:
89 raise InvalidArchitecture('{0} is not a valid architecture name'.format(arch))
90 if len(triplet_wildcard) != 3:
91 raise InvalidArchitecture('{0} is not a valid architecture name or wildcard'.format(wildcard))
94 if triplet_arch[i] != triplet_wildcard[i] and triplet_wildcard[i] != 'any':