From: Michael Tremer Date: Sat, 3 Dec 2016 17:36:44 +0000 (+0100) Subject: system: Move architecture-dependent things into an own module X-Git-Tag: 0.9.28~1285^2~1407 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1204717c1ce20ffa542b7816ca320f170f230e89;p=pakfire.git system: Move architecture-dependent things into an own module Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index e4391b24b..df0713fd2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -94,6 +94,7 @@ pakfire_PYTHON = \ src/pakfire/__init__.py \ src/pakfire/__version__.py \ src/pakfire/actions.py \ + src/pakfire/arch.py \ src/pakfire/base.py \ src/pakfire/builder.py \ src/pakfire/cgroup.py \ diff --git a/src/pakfire/arch.py b/src/pakfire/arch.py new file mode 100644 index 000000000..62a027a29 --- /dev/null +++ b/src/pakfire/arch.py @@ -0,0 +1,122 @@ +#!/usr/bin/python3 +############################################################################### +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2016 Pakfire development team # +# # +# 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 3 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, see . # +# # +############################################################################### + +import logging + +log = logging.getLogger("pakfire.arch") +log.propagate = 1 + +class Arch(object): + def __init__(self, arch): + self.arch = arch + + def __eq__(self, other): + return self.arch == other.arch + + @property + def platform(self): + """ + Returns the "class" this architecture belongs to. + """ + if self.arch.startswith("arm") or self.arch == "aarch64": + return "arm" + + if self.arch in ("i686", "x86_64"): + return "x86" + + return "unknown" + + def get_machine(self, vendor=None): + if vendor is None: + vendor = "unknown" + + # Make vendor lowercase + vendor = vendor.lower() + assert vendor + + s = "%s-%s-linux-gnu" % (self.arch, vendor) + + if self.arch.startswith("arm"): + s += "eabi" + + return s + + def get_buildtarget(self, vendor=None): + machine = self.get_machine(vendor) + + # Cut off last segment of machine. + return machine.replace("-gnu", "") + + @property + def compatbile_arches(self): + """ + Returns a list of all architectures that are + compatible (i.e. can be emulated) + """ + x = { + # Host arch : Can build these arches. + # x86 + "x86_64" : ["x86_64", "i686",], + "i686" : ["i686",], + + # ARM + "armv5tel" : ["armv5tel",], + "armv5tejl" : ["armv5tel",], + "armv6l" : ["armv5tel",], + "armv7l" : ["armv7hl", "armv5tel",], + "armv7hl" : ["armv7hl", "armv5tel",], + + "aarch64" : ["aarch64",], + } + + try: + return x[self.arch] + except KeyError: + return [] + + def is_compatible_with(self, arch): + """ + Returns True if the given architecture is compatible + with this architecture. + """ + return arch in self.compatible_arches + + @property + def personality(self): + """ + Return the personality of the target system. + + If host and target system are of the same architecture, we return + None to skip the setting of the personality in the build chroot. + """ + arch2personality = { + "x86_64" : "linux64", + "i686" : "linux32", + "i586" : "linux32", + "i486" : "linux32", + } + + try: + personality = arch2personality[self.arch] + except KeyError: + personality = None + + return personality diff --git a/src/pakfire/distro.py b/src/pakfire/distro.py index bceba0cb5..ddfcde33b 100644 --- a/src/pakfire/distro.py +++ b/src/pakfire/distro.py @@ -19,13 +19,12 @@ # # ############################################################################### +import logging import os import re -import logging -log = logging.getLogger("pakfire") - -from . import system +log = logging.getLogger("pakfire.distro") +log.propagate = 1 class Distribution(object): def __init__(self, data=None): @@ -76,9 +75,7 @@ class Distribution(object): def dump(self): log.debug("Distribution configuration:") - attrs = ("name", "release", "sname", "dist", "vendor", "contact", - "arch", "machine", "buildtarget", "source_dl",) - + attrs = ("name", "release", "sname", "dist", "vendor", "contact", "source_dl") for attr in attrs: log.debug(" %s : %s" % (attr, getattr(self, attr))) @@ -142,57 +139,10 @@ class Distribution(object): def contact(self): return self._data.get("contact", "N/A") - def get_arch(self): - arch = self._data.get("arch", None) or system.system.arch - - # We can not set up a build environment for noarch. - if arch == "noarch": - arch = system.system.arch - - return arch - - def set_arch(self, arch): - # XXX check if we are allowed to set this arch - if not arch: - return - - self._data["arch"] = arch - - arch = property(get_arch, set_arch) - - @property - def platform(self): - """ - Returns the "class" this architecture belongs to. - """ - if self.arch.startswith("arm") or self.arch == "aarch64": - return "arm" - - if self.arch in ("i686", "x86_64"): - return "x86" - - return "unknown" - @property def dist(self): return self.sname[:2] + self.release - @property - def machine(self): - vendor = self.vendor.split()[0] - - s = "%s-%s-linux-gnu" % (self.arch, vendor.lower()) - - if self.arch.startswith("arm"): - s += "eabi" - - return s - - @property - def buildtarget(self): - # Cut off last segment of machine. - return self.machine.replace("-gnu", "") - @property def source_dl(self): return self._data.get("source_dl", None) @@ -208,10 +158,6 @@ class Distribution(object): "DISTRO_SNAME" : self.sname, "DISTRO_RELEASE" : self.release, "DISTRO_DISTTAG" : self.dist, - "DISTRO_ARCH" : self.arch, - "DISTRO_MACHINE" : self.machine, - "DISTRO_PLATFORM" : self.platform, - "DISTRO_BUILDTARGET" : self.buildtarget, "DISTRO_VENDOR" : self.vendor, "DISTRO_CONTACT" : self.contact, "DISTRO_SLOGAN" : self.slogan, @@ -227,29 +173,3 @@ class Distribution(object): info[k.lower()] = v return info - - @property - def personality(self): - """ - Return the personality of the target system. - - If host and target system are of the same architecture, we return - None to skip the setting of the personality in the build chroot. - """ - - if self.arch == system.system.native_arch: - return None - - arch2personality = { - "x86_64" : "linux64", - "i686" : "linux32", - "i586" : "linux32", - "i486" : "linux32", - } - - try: - personality = arch2personality[self.arch] - except KeyError: - personality = None - - return personality diff --git a/src/pakfire/system.py b/src/pakfire/system.py index a4aa25521..1c4b6dc95 100644 --- a/src/pakfire/system.py +++ b/src/pakfire/system.py @@ -24,6 +24,7 @@ import os import socket import tempfile +from . import arch from . import shell from . import _pakfire @@ -35,6 +36,9 @@ class System(object): Class that grants access to several information about the system this software is running on. """ + def __init__(self): + self.arch = arch.Arch(self.native_arch) + @property def hostname(self): hn = socket.gethostname() @@ -61,48 +65,18 @@ class System(object): """ return os.uname()[4] - @property - def arch(self): - """ - Return the architecture of the host we are running on. - """ - if self.supported_arches and not self.native_arch in self.supported_arches: - return self.supported_arches[0] - - return self.native_arch - @property def supported_arches(self): """ Check what architectures can be built on this host. """ - host_can_build = { - # Host arch : Can build these arches. - - # x86 - "x86_64" : ["x86_64", "i686",], - "i686" : ["i686",], - - # ARM - "armv5tel" : ["armv5tel",], - "armv5tejl" : ["armv5tel",], - "armv6l" : ["armv5tel",], - "armv7l" : ["armv7hl", "armv5tel",], - "armv7hl" : ["armv7hl", "armv5tel",], - - "aarch64" : ["aarch64",], - } - - try: - return host_can_build[self.native_arch] - except KeyError: - return [] + return self.arch.compatible_arches def host_supports_arch(self, arch): """ Check if this host can build for the target architecture "arch". """ - return arch in self.supported_arches + return self.arch.is_compatible_with(arch) @property def cpu_count(self):