root/make_serverxml.sh @ 121

Revision 121, 1.9 kB (checked in by d0nut, 5 years ago)

initial release of python script for generating a server-package-xml

Line 
1# -*- coding: utf8 -*-
2#!/usr/bin/python
3
4import os
5import xml.dom.minidom
6
7
8def dive(mydir="."):
9    "searchs for all package.xml files"
10    l = []
11    for filename in os.listdir(mydir):
12        subdir = mydir+"/"+filename
13        if os.path.isdir(subdir):
14            tmp = dive(subdir)
15            if len(tmp):
16                l.extend(tmp)
17        elif filename == "package.xml":
18            l.append(mydir)
19    return l
20
21
22class Package(object):
23    "representating a package"
24
25    def __init__(self, path):
26        "construcor gets path, opens package.xml and extracts the relevant information"
27        dom = xml.dom.minidom.parse(path+"/package.xml").documentElement
28        self.name = dom.getAttribute("name")
29        self.packageinformation = dom.getElementsByTagName("packageinformation")
30        self.authorinformation = dom.getElementsByTagName("authorinformation")
31        self.requiredpackages = dom.getElementsByTagName("requiredpackages")
32        self.instructions = dom.getElementsByTagName("instructions")
33
34    def toxml(self):
35        "returns a dom object"
36        dom = xml.dom.minidom.Document().createElement("package")
37        dom.setAttribute("name", self.name)
38        return dom
39
40    def __str__(self):
41        "returns packagename"
42        return self.name
43
44
45class Section(object):
46    "representating a section"
47
48    def __init__(self):
49        "constructor inits new dom object with root elements"
50        self.tree = xml.dom.minidom.Document()
51        self.packages = xml.dom.minidom.Document().createElement("packages")
52        self.tree.appendChild(self.packages)
53
54    def addPackage(self, package):
55        "adds a new package"
56        self.packages.appendChild(package.toxml())
57
58    def __str__(self):
59        "returns xml output in utf-8 encoding"
60        return self.tree.toprettyxml(encoding='utf-8')
61
62
63#main applikation
64s = Section()
65for path in dive():
66    dom = Package(path)
67    s.addPackage(dom)
68
69print s
Note: See TracBrowser for help on using the browser.