]> git.ipfire.org Git - pakfire.git/blob - pakfire/distro.py
Initial import.
[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
9 class Distribution(object):
10 def __init__(self, pakfire):
11 self.pakfire = pakfire
12
13 self._data = {
14 "arch" : self.host_arch,
15 "name" : "unknown",
16 "slogan" : "---",
17 "vendor" : "unknown",
18 "version" : "0.0",
19 }
20
21 if not self.pakfire.config._distro:
22 raise ConfigError, "No distribution data was provided in the configuration"
23
24 # Import settings from Config()
25 self._data.update(self.pakfire.config._distro)
26
27 # Dump all data
28 self.dump()
29
30 def dump(self):
31 logging.debug("Distribution configuration:")
32
33 attrs = ("name", "version", "release", "sname", "dist", "vendor", "machine",)
34
35 for attr in attrs:
36 logging.debug(" %s : %s" % (attr, getattr(self, attr)))
37
38 @property
39 def name(self):
40 return self._data.get("name")
41
42 @property
43 def version(self):
44 return self._data.get("version")
45
46 @property
47 def release(self):
48 m = re.match(r"^([0-9]+)\..*", self.version)
49
50 return m.group(1)
51
52 @property
53 def sname(self):
54 return self.name.strip().lower()
55
56 @property
57 def slogan(self):
58 return self._data.get("slogan")
59
60 @property
61 def vendor(self):
62 return self._data.get("vendor")
63
64 def get_arch(self):
65 return self._data.get("arch")
66
67 def set_arch(self, arch):
68 # XXX check if we are allowed to set this arch
69 self._data.set("arch", arch)
70
71 arch = property(get_arch, set_arch)
72
73 @property
74 def dist(self):
75 return self.sname[:2] + self.release
76
77 @property
78 def machine(self):
79 return "%s-%s-linux-gnu" % (self.arch, self.vendor)
80
81 @property
82 def host_arch(self):
83 """
84 Return the architecture of the host we are running on.
85 """
86 return os.uname()[4]
87
88 @property
89 def supported_arches(self):
90 host_arches = {
91 "i686" : [ "i686", "x86_64", ],
92 "i586" : [ "i586", "i686", "x86_64", ],
93 "i486" : [ "i486", "i586", "i686", "x86_64", ],
94 }
95
96 for host, can_be_built in host_arches.items():
97 if self.host_arch in can_be_built:
98 yield host
99
100 def host_supports_arch(self, arch):
101 """
102 Check if this host can build for the target architecture "arch".
103 """
104 return arch in self.supported_arches
105
106 @property
107 def environ(self):
108 """
109 An attribute that adds some environment variables to the
110 chroot environment.
111 """
112 env = {
113 "DISTRO_NAME" : self.name,
114 "DISTRO_SNAME" : self.sname,
115 "DISTRO_VERSION" : self.version,
116 "DISTRO_RELEASE" : self.release,
117 "DISTRO_DISTTAG" : self.dist,
118 "DISTRO_ARCH" : self.arch,
119 "DISTRO_MACHINE" : self.machine,
120 "DISTRO_VENDOR" : self.vendor,
121 }
122
123 return env
124
125 @property
126 def info(self):
127 info = {}
128
129 for k, v in self.environ.items():
130 info[k.lower()] = v
131
132 return info
133