import repository from arizona
[raven.git] / apps / importyum / yumxmlparse.py
1 from xml.parsers.xmlproc import xmlproc,xmlapp
2
3 def UTOA(x):
4     """ un-unicode a string """
5     return x.encode("utf8")
6
7 class YumXmlApp(xmlproc.Application):
8     """This class parses the groups.xml file."""
9     def __init__(self):
10         #
11         # Do this the old way (instead of using super) because xmlproc is written using
12         # old-style classes that don't work with super.
13         #
14         xmlproc.Application.__init__(self)
15         self.curtag = "root"
16         self.loc = None
17         self.packages = []
18
19     def set_locator(self, locator):
20         self.loc = locator
21
22     def doc_start(self):
23         self.group = None
24
25     def format_dep(self, attrs):
26         name = UTOA(attrs.get("name"))
27         sign = None
28         ver = None
29         rel = None
30         if "flags" in attrs:
31             flags = UTOA(attrs.get("flags"))
32             ver = UTOA(attrs.get("ver"))
33             if "rel" in attrs:
34                rel = UTOA(attrs.get("rel"))
35
36             if flags == "EQ":
37                 sign = "="
38             elif flags == "LE":
39                 sign = "<="
40             elif flags == "GE":
41                 sign = ">="
42             elif flags == "LT":
43                 sign = "<"
44             elif flags == "GT":
45                 sign = ">"
46             else:
47                 print "XXX unknown flags", flags
48
49         if sign:
50             dep = name + " " + sign + " " + ver
51             if rel:
52                 dep = dep + "-" + rel
53         else:
54             dep = name
55
56         return dep
57
58     def handle_start_tag(self, tag, attrs):
59         #print "start:",tag
60         line = self.loc.get_line()
61         file = self.loc.get_current_sysid()
62         self.curtag = self.curtag + "." + tag
63         if self.curtag == "root.metadata.package":
64             self.package = {}
65         elif self.curtag == "root.metadata.package.version":
66             self.package["version"] = UTOA(attrs.get("ver"))
67             self.package["release"] = UTOA(attrs.get("rel"))
68         elif self.curtag == "root.metadata.package.size":
69             self.package["size"] = UTOA(attrs.get("archive")) # XXX which size ??
70         elif self.curtag == "root.metadata.package.format.rpm:provides":
71             self.package["provides"] = []
72         elif self.curtag == "root.metadata.package.format.rpm:provides.rpm:entry":
73             self.package["provides"].append( self.format_dep(attrs) )
74         elif self.curtag == "root.metadata.package.format.rpm:requires":
75             self.package["requires"] = []
76         elif self.curtag == "root.metadata.package.format.rpm:requires.rpm:entry":
77             self.package["requires"].append( self.format_dep(attrs) )
78         elif self.curtag == "root.metadata.package.location":
79             self.package["filename"] = UTOA(attrs.get("href")).split("/")[-1]
80             self.package["url"] = UTOA(attrs.get("href"))
81         else:
82             pass
83
84     def handle_data(self, data, start, end):
85         data = data[start:end]
86         if self.curtag == "root.metadata.package.name":
87             self.package["name"] = UTOA(data)
88         elif self.curtag == "root.metadata.package.checksum":
89             self.package["hash"] = UTOA(data)
90         elif self.curtag == "root.metadata.package.format.file":
91             if not ("files" in self.package):
92                 self.package["files"] = []
93             self.package["files"].append(UTOA(data))
94         else:
95             pass
96
97     def handle_end_tag(self,tag):
98         #print "end:",tag
99         if self.curtag == "root.metadata.package":
100             #print "package:", self.package
101             self.packages.append(self.package)
102         else:
103             pass
104         self.curtag = ".".join(self.curtag.split(".")[:-1])
105
106
107 def YumXmlParse(filename):
108     app = YumXmlApp()
109     parser = xmlproc.XMLProcessor()
110     parser.set_application(app)
111     parser.parse_resource(filename)
112
113     return app.packages