debarchwildcardtest/debarch.py

112 lines
3.4 KiB
Python
Raw Normal View History

2014-11-26 11:24:57 +00:00
"""architecture matching
@copyright: 2014, Ansgar Burchardt <ansgar@debian.org>
2017-01-15 14:19:04 +00:00
@copyright: 2014-2017, Johannes Schauer <j.schauer@email.de>
2014-11-26 11:24:57 +00:00
@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-03-29 15:48:56 +00:00
# FIXME: this should be integrated into python-debian
# see: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=771058
2017-01-15 08:08:49 +00:00
_cached_arch2tuple = None
_cached_tuple2arch = None
2014-11-26 11:24:57 +00:00
_cached_cputable = None
2017-03-29 15:48:56 +00:00
2014-11-26 11:24:57 +00:00
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
2017-03-29 15:48:56 +00:00
2014-11-26 11:24:57 +00:00
def _read_cputable():
global _cached_cputable
if _cached_cputable is None:
_cached_cputable = _load_table('/usr/share/dpkg/cputable')
return _cached_cputable
2017-03-29 15:48:56 +00:00
2017-01-15 08:08:49 +00:00
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()
2014-11-26 11:24:57 +00:00
for row in table:
2017-01-15 08:08:49 +00:00
debtuple = row[0]
2014-11-26 11:24:57 +00:00
debarch = row[1]
2017-01-15 08:08:49 +00:00
if '<cpu>' in debtuple:
2014-11-26 11:24:57 +00:00
for r in _read_cputable():
cpu = r[0]
2017-01-15 08:08:49 +00:00
dt = debtuple.replace('<cpu>', cpu)
2014-11-26 11:24:57 +00:00
da = debarch.replace('<cpu>', cpu)
2017-01-15 08:08:49 +00:00
arch2tuple[da] = dt
tuple2arch[dt] = da
2014-11-26 11:24:57 +00:00
else:
2017-01-15 08:08:49 +00:00
arch2tuple[debarch] = debtuple
tuple2arch[debtuple] = debarch
_cached_arch2tuple = arch2tuple
_cached_tuple2arch = tuple2arch
return _cached_tuple2arch, _cached_arch2tuple
2014-11-26 11:24:57 +00:00
2017-03-29 15:48:56 +00:00
2017-01-15 08:08:49 +00:00
def debwildcard_to_debtuple(arch):
arch_tuple = arch.split('-', 3)
2014-11-26 11:24:57 +00:00
if 'any' in arch_tuple:
2017-01-15 08:08:49 +00:00
if len(arch_tuple) == 4:
2014-11-26 11:24:57 +00:00
return arch_tuple
2017-01-15 08:08:49 +00:00
elif len(arch_tuple) == 3:
return ('any', arch_tuple[0], arch_tuple[1], arch_tuple[2])
2014-11-26 11:24:57 +00:00
elif len(arch_tuple) == 2:
2017-01-15 08:08:49 +00:00
return ('any', 'any', arch_tuple[0], arch_tuple[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
else:
2017-01-15 08:08:49 +00:00
return debarch_to_debtuple(arch)
2014-11-26 11:24:57 +00:00
2017-03-29 15:48:56 +00:00
2017-01-15 08:08:49 +00:00
def debarch_to_debtuple(arch):
2014-11-26 11:24:57 +00:00
if (arch.startswith("linux-")):
arch = arch[6:]
2017-01-15 08:08:49 +00:00
tuple = _read_tupletable()[1].get(arch)
2014-11-26 11:24:57 +00:00
2017-01-15 08:08:49 +00:00
if tuple is None:
2014-11-26 11:24:57 +00:00
return
2017-01-15 08:08:49 +00:00
return tuple.split('-', 3)
2014-11-26 11:24:57 +00:00
2017-03-29 15:48:56 +00:00
2014-11-26 11:24:57 +00:00
def match_architecture(real, alias):
if alias == real or alias == "any":
return True
2017-01-15 08:08:49 +00:00
real = debarch_to_debtuple(real)
alias = debwildcard_to_debtuple(alias)
2014-11-26 11:24:57 +00:00
2017-01-15 08:08:49 +00:00
if real is None or len(real) != 4 or alias is None or len(alias) != 4:
2014-11-26 11:24:57 +00:00
return False
2017-03-29 15:48:56 +00:00
for i in range(0, 4):
2014-11-26 11:24:57 +00:00
if (alias[i] != real[i] and alias[i] != "any"):
return False
return True