]> git.ipfire.org Git - people/ms/pakfire.git/blob - pakfire/distro.py
Try to design a better API.
[people/ms/pakfire.git] / pakfire / distro.py
1 #!/usr/bin/python
2
3 import logging
4 import os
5 import re
6
7 from errors import ConfigError
8 from repository import Repositories
9
10 class Distribution(object):
11 def __init__(self, pakfire, distro_config=None):
12 self.pakfire = pakfire
13
14 self._data = {
15 "arch" : self.host_arch,
16 "name" : "unknown",
17 "slogan" : "---",
18 "vendor" : "unknown",
19 "version" : "0.0",
20 }
21
22 # Inherit configuration from Pakfire configuration.
23 self.update(self.pakfire.config._distro)
24
25 # Update my configuration from the constructor.
26 self.update(distro_config)
27
28 # Dump all data
29 self.dump()
30
31 def dump(self):
32 logging.debug("Distribution configuration:")
33
34 attrs = ("name", "version", "release", "sname", "dist", "vendor",
35 "arch", "machine",)
36
37 for attr in attrs:
38 logging.debug(" %s : %s" % (attr, getattr(self, attr)))
39
40 def update(self, config):
41 if not config:
42 return
43
44 # Exceptional handling for arch.
45 if config.has_key("arch"):
46 self.arch = config["arch"]
47 del config["arch"]
48
49 self._data.update(config)
50
51 @property
52 def name(self):
53 return self._data.get("name")
54
55 @property
56 def version(self):
57 return self._data.get("version")
58
59 @property
60 def release(self):
61 m = re.match(r"^([0-9]+)\..*", self.version)
62
63 return m.group(1)
64
65 @property
66 def sname(self):
67 return self.name.strip().lower()
68
69 @property
70 def slogan(self):
71 return self._data.get("slogan")
72
73 @property
74 def vendor(self):
75 return self._data.get("vendor")
76
77 def get_arch(self):
78 return self._data.get("arch") or self.host_arch
79
80 def set_arch(self, arch):
81 # XXX check if we are allowed to set this arch
82 if not arch:
83 return
84
85 self._data["arch"] = arch
86
87 arch = property(get_arch, set_arch)
88
89 @property
90 def dist(self):
91 return self.sname[:2] + self.release
92
93 @property
94 def machine(self):
95 vendor = self.vendor.split()[0]
96
97 return "%s-%s-linux-gnu" % (self.arch, vendor.lower())
98
99 @property
100 def host_arch(self):
101 """
102 Return the architecture of the host we are running on.
103 """
104 return os.uname()[4]
105
106 @property
107 def supported_arches(self):
108 host_arches = {
109 "i686" : [ "i686", "x86_64", ],
110 "i586" : [ "i586", "i686", "x86_64", ],
111 "i486" : [ "i486", "i586", "i686", "x86_64", ],
112 }
113
114 for host, can_be_built in host_arches.items():
115 if self.host_arch in can_be_built:
116 yield host
117
118 def host_supports_arch(self, arch):
119 """
120 Check if this host can build for the target architecture "arch".
121 """
122 return arch in self.supported_arches
123
124 @property
125 def environ(self):
126 """
127 An attribute that adds some environment variables to the
128 chroot environment.
129 """
130 env = {
131 "DISTRO_NAME" : self.name,
132 "DISTRO_SNAME" : self.sname,
133 "DISTRO_VERSION" : self.version,
134 "DISTRO_RELEASE" : self.release,
135 "DISTRO_DISTTAG" : self.dist,
136 "DISTRO_ARCH" : self.arch,
137 "DISTRO_MACHINE" : self.machine,
138 "DISTRO_VENDOR" : self.vendor,
139 "DISTRO_SLOGAN" : self.slogan,
140 }
141
142 return env
143
144 @property
145 def info(self):
146 info = {}
147
148 for k, v in self.environ.items():
149 info[k.lower()] = v
150
151 return info
152
153 @property
154 def personality(self):
155 """
156 Return the personality of the target system.
157
158 If host and target system are of the same architecture, we return
159 None to skip the setting of the personality in the build chroot.
160 """
161
162 if self.arch == self.host_arch:
163 return None
164
165 arch2personality = {
166 "x86_64" : "linux64",
167 "i686" : "linux32",
168 "i586" : "linux32",
169 "i486" : "linux32",
170 }
171
172 return arch2personality[self.arch]
173