debarchwildcardtest/daklib_arch.py

118 lines
3.8 KiB
Python
Raw Normal View History

2014-11-26 11:24:57 +00:00
"""architecture matching
@copyright: 2014, Ansgar Burchardt <ansgar@debian.org>
@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
2017-01-15 08:08:49 +00:00
import errno
2014-11-26 11:24:57 +00:00
def _load_table(path):
table = []
with open(path, 'r') as fh:
for line in fh:
if not line or line.startswith('#'):
continue
table.append(line.split())
return table
_cached_cputable = None
def _cputable():
global _cached_cputable
if _cached_cputable is None:
_cached_cputable = _load_table('/usr/share/dpkg/cputable')
return _cached_cputable
2017-01-15 08:08:49 +00:00
_cached_arch2tuple = None
_cached_tuple2arch = None
def _tupletable():
global _cached_arch2tuple, _cached_tuple2arch
if _cached_arch2tuple is None or _cached_tuple2arch is None:
try:
tripletable = False
table = _load_table('/usr/share/dpkg/tupletable')
except IOError as e:
if e.errno != errno.ENOENT:
raise
tripletable = True
table = _load_table('/usr/share/dpkg/triplettable')
arch2tuple = {}
tuple2arch = {}
def add_tuple(tuple, arch):
if tripletable:
tuple = "base-{}".format(tuple)
arch2tuple[arch] = tuple
tuple2arch[tuple] = arch
2014-11-26 11:24:57 +00:00
for row in table:
if '<cpu>' in row[0] or '<cpu>' in row[1]:
for cpu in _cputable():
replaced_row = [ column.replace('<cpu>', cpu[0]) for column in row ]
2017-01-15 08:08:49 +00:00
add_tuple(replaced_row[0], replaced_row[1])
2014-11-26 11:24:57 +00:00
else:
2017-01-15 08:08:49 +00:00
add_tuple(row[0], row[1])
_cached_arch2tuple = arch2tuple
_cached_tuple2arch = tuple2arch
return _cached_tuple2arch, _cached_arch2tuple
2014-11-26 11:24:57 +00:00
class InvalidArchitecture(Exception):
pass
2017-01-15 08:08:49 +00:00
def Debian_arch_to_Debian_tuple(arch):
2014-11-26 11:24:57 +00:00
parts = arch.split('-')
# Handle architecture wildcards
if 'any' in parts:
2017-01-15 08:08:49 +00:00
if len(parts) == 4:
2014-11-26 11:24:57 +00:00
return parts
2017-01-15 08:08:49 +00:00
elif len(parts) == 3:
return 'any', parts[0], parts[1], parts[2]
2014-11-26 11:24:57 +00:00
elif len(parts) == 2:
2017-01-15 08:08:49 +00:00
return 'any', 'any', parts[0], parts[1]
2014-11-26 11:24:57 +00:00
else:
2017-01-15 08:08:49 +00:00
return 'any', 'any', 'any', 'any'
2014-11-26 11:24:57 +00:00
if len(parts) == 2 and parts[0] == 'linux':
arch = parts[1]
2017-01-15 08:08:49 +00:00
tuple = _tupletable()[1].get(arch, None)
if tuple is None:
2014-11-26 11:24:57 +00:00
return None
2017-01-15 08:08:49 +00:00
return tuple.split('-', 3)
2014-11-26 11:24:57 +00:00
def match_architecture(arch, wildcard):
2017-01-15 08:08:49 +00:00
# 'all' has no valid tuple
2014-11-26 11:24:57 +00:00
if arch == 'all' or wildcard == 'all':
return arch == wildcard
if wildcard is 'any' or arch == wildcard:
return True
2017-01-15 08:08:49 +00:00
tuple_arch = Debian_arch_to_Debian_tuple(arch)
tuple_wildcard = Debian_arch_to_Debian_tuple(wildcard)
2014-11-26 11:24:57 +00:00
2017-01-15 08:08:49 +00:00
if tuple_arch is None or len(tuple_arch) != 4:
2014-11-26 11:24:57 +00:00
raise InvalidArchitecture('{0} is not a valid architecture name'.format(arch))
2017-01-15 08:08:49 +00:00
if tuple_wildcard is None or len(tuple_wildcard) != 4:
2014-11-26 11:24:57 +00:00
raise InvalidArchitecture('{0} is not a valid architecture name or wildcard'.format(wildcard))
2017-01-15 08:08:49 +00:00
for i in range(0,4):
if tuple_arch[i] != tuple_wildcard[i] and tuple_wildcard[i] != 'any':
2014-11-26 11:24:57 +00:00
return False
return True