1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.
import re
import lxml.etree as ET
def _cache_property(fn):
"""
Decorator to use self._cache to cache property lookup results
"""
def _wrapper(*args):
self = args[0]
key = str(fn)
cache = self._cache # pylint: disable=protected-access
if key not in cache:
cache[key] = fn(*args)
return cache[key]
return property(_wrapper)
class _XMLBase:
"""
Simple base class for our XML parsers
"""
def __init__(self, root):
self._root = root
self._cache = {}
def _get_text(self, element_name, default=None):
node = self._root.find(element_name)
if node is not None:
return node.text
return default
def _get_int(self, element_name, default=None):
text = self._get_text(element_name)
if text is not None:
return int(text)
return default
class Os(_XMLBase):
def __init__(self, path):
self.path = path
root = ET.parse(self.path.open("r")).getroot().find("os")
super().__init__(root)
def __repr__(self):
return "<%s shortid=%s>" % (self.__class__.__name__, self.shortid)
@_cache_property
def internal_id(self):
return self._root.get("id")
@_cache_property
def derives_from(self):
derives_from = self._root.find("derives-from")
if derives_from is not None:
return derives_from.get("id")
return None
@_cache_property
def clones(self):
clones = self._root.find("clones")
if clones is not None:
return clones.get("id")
return None
@_cache_property
def upgrades(self):
upgrades = self._root.find("upgrades")
if upgrades is not None:
return upgrades.get("id")
return None
@_cache_property
def devices(self):
devices = []
devicelist = self._root.find("devices")
if devicelist is not None:
for device in devicelist.findall("device"):
if device.get("supported") != "false":
devices.append(device.get("id"))
return devices
@_cache_property
def devices_unsupported(self):
devices = []
devicelist = self._root.find("devices")
if devicelist is not None:
for device in devicelist.findall("device"):
if device.get("supported") == "false":
devices.append(device.get("id"))
return devices
@_cache_property
def images(self):
images = []
for image in self._root.findall("image"):
images.append(Image(image))
return images
@_cache_property
def medias(self):
medias = []
for media in self._root.findall("media"):
medias.append(Media(media))
return medias
@_cache_property
def trees(self):
trees = []
for tree in self._root.findall("tree"):
trees.append(Tree(tree))
return trees
@_cache_property
def shortid(self):
return self._get_text("short-id")
@_cache_property
def shortids(self):
shortids = []
for shortid in self._root.findall("short-id"):
shortids.append(shortid.text)
return shortids
@_cache_property
def distro(self):
return self._get_text("distro")
@_cache_property
def resources_list(self):
return self._root.findall("resources")
def _get_resources(self, node, resource_type):
valid_resources = ["minimum", "recommended", "maximum", "network-install"]
if resource_type not in valid_resources:
return None
# pylint: disable=unsupported-membership-test
if node not in self.resources_list:
return None
resource = node.find(resource_type)
if resource is not None:
return Resources(resource)
return None
def get_minimum_resources(self, node):
return self._get_resources(node, "minimum")
def get_recommended_resources(self, node):
return self._get_resources(node, "recommended")
def get_maximum_resources(self, node):
return self._get_resources(node, "maximum")
def get_network_install_resources(self, node):
return self._get_resources(node, "network-install")
@_cache_property
def release_date(self):
return self._get_text("release-date")
@_cache_property
def eol_date(self):
return self._get_text("eol-date")
@_cache_property
def variants(self):
return [v.attrib["id"] for v in self._root.findall("variant")]
@_cache_property
def firmwares(self):
firmwares = []
firmwarelist = self._root.findall("firmware")
for firmware in firmwarelist:
firmwares.append(Firmware(firmware))
return firmwares
@_cache_property
def installscripts(self):
return [
s.attrib["id"]
for i in self._root.findall("installer")
for s in i.findall("script")
]
class Resources(_XMLBase):
@_cache_property
def cpu(self):
return self._get_int("cpu")
@_cache_property
def n_cpus(self):
return self._get_int("n-cpus")
@_cache_property
def ram(self):
return self._get_int("ram")
@_cache_property
def storage(self):
return self._get_int("storage")
class Firmware(_XMLBase):
@_cache_property
def arch(self):
return self._root.attrib["arch"]
@_cache_property
def firmware_type(self):
return self._root.attrib["type"]
class Image(_XMLBase):
@_cache_property
def url(self):
return self._get_text("url")
@_cache_property
def variant(self):
variant = self._root.find("variant")
if variant is not None:
return variant.attrib["id"]
return None
@_cache_property
def format(self):
return self._root.attrib.get("format", None)
class Media(_XMLBase):
@_cache_property
def url(self):
return self._get_text("url")
@_cache_property
def iso(self):
iso = self._root.find("iso")
if iso is not None:
return ISO(iso)
return None
@_cache_property
def variant(self):
variant = self._root.find("variant")
if variant is not None:
return variant.attrib["id"]
return None
@_cache_property
def installscripts(self):
return [
s.attrib["id"]
for i in self._root.findall("installer")
for s in i.findall("script")
]
class Tree(_XMLBase):
@_cache_property
def url(self):
return self._get_text("url")
@_cache_property
def variant(self):
variant = self._root.find("variant")
if variant is not None:
return variant.attrib["id"]
return None
@_cache_property
def treeinfo(self):
treeinfo = self._root.find("treeinfo")
if treeinfo is not None:
return Treeinfo(treeinfo)
return None
@_cache_property
def kernel(self):
return self._get_text("kernel")
@_cache_property
def initrd(self):
return self._get_text("initrd")
class Treeinfo(_XMLBase):
@_cache_property
def arch(self):
return re.compile(self._get_text("arch", default=""))
@_cache_property
def family(self):
return re.compile(self._get_text("family", default=""))
@_cache_property
def variant(self):
return re.compile(self._get_text("variant", default=""))
@_cache_property
def version(self):
return re.compile(self._get_text("version", default=""))
class ISO(_XMLBase):
@_cache_property
def volumeid(self):
return re.compile(self._get_text("volume-id", default=""))
@_cache_property
def publisherid(self):
return re.compile(self._get_text("publisher-id", default=""))
@_cache_property
def applicationid(self):
return re.compile(self._get_text("application-id", default=""))
@_cache_property
def systemid(self):
return re.compile(self._get_text("system-id", default=""))
@_cache_property
def volumesize(self):
return self._get_int("volume-size", default=0)
class Device(_XMLBase):
def __init__(self, path):
self.path = path
root = ET.parse(self.path.open("r")).getroot().find("device")
super().__init__(root)
@_cache_property
def internal_id(self):
return self._root.attrib["id"]
@_cache_property
def name(self):
return self._get_text("name")
class Datamap(_XMLBase):
def __init__(self, path):
self.path = path
root = ET.parse(self.path.open("r")).getroot().find("datamap")
super().__init__(root)
def __repr__(self):
return "<%s path=%s>" % (self.__class__.__name__, self.path)
@_cache_property
def internal_id(self):
return self._root.get("id")
class InstallScript(_XMLBase):
def __init__(self, path):
self.path = path
root = ET.parse(self.path.open("r")).getroot().find("install-script")
super().__init__(root)
def __repr__(self):
return "<%s path=%s>" % (self.__class__.__name__, self.path)
@_cache_property
def internal_id(self):
return self._root.get("id")
class Platform(_XMLBase):
def __init__(self, path):
self.path = path
root = ET.parse(self.path.open("r")).getroot().find("platform")
super().__init__(root)
def __repr__(self):
return "<%s path=%s>" % (self.__class__.__name__, self.path)
@_cache_property
def internal_id(self):
return self._root.get("id")
|