This repository has been archived by the owner on May 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpdateXML.py
executable file
·83 lines (72 loc) · 1.93 KB
/
UpdateXML.py
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
#!/usr/bin/python
'''
This little gem parses the XML file that describes where to download the
updates from/
'''
__author__ = "Patrick Butler <pbutler@killertux.org>"
__version__ = "$Revision: 31 $"
__copyright__ = "Copyright 2005,2006 Patrick Butler"
__license__ = "GPL Version 2"
import sys
import os
import xml
from xml.dom.minidom import parse, parseString
from xml.dom import minidom, Node
def readFileNode(file):
ret = { 'source': None, 'dir': None, 'name': None, 'md5': None, 'version' : -1 }
for node in file.childNodes:
if node.nodeType == Node.ELEMENT_NODE and node.nodeName in ret.keys():
if node.childNodes == []:
continue
if node.childNodes[0].nodeType == Node.TEXT_NODE:
ret[node.nodeName] = node.childNodes[0].nodeValue
if node.nodeName == 'version':
ret[node.nodeName] = float( ret[node.nodeName] )
if node.nodeName == 'dir':
ret['dir'] = ret['dir'].replace("/", os.sep)
ret['dir'] = ret['dir'].replace("\\", os.sep)
return ret
def walkNodes(doc):
ret = {}
listFiles = doc.getElementsByTagName('files')
#if no root node or no file nodes
if listFiles == None:
return None
for files in listFiles:
for f in files.childNodes:
data = readFileNode(f)
key = data['name']
if key == None:
continue
if data['dir'] != None and data['dir'] != '':
key = os.path.join(data['dir'], key)
ret[key] = data
return ret
def parse(s):
try :
doc = minidom.parse(s)
return walkNodes(doc)
except xml.parsers.expat.ExpatError:
return None
def parseString(fn):
try:
doc = minidom.parseString(fn)
return walkNodes(doc)
except xml.parsers.expat.ExpatError:
return None
if __name__ == '__main__':
s ='''
<files>
<file>
<dir></dir>
<name>test.py</name>
<source>http://nowhere.com/test.py</source>
<md5>blahbalh</md5>
<version>1.1</version>
</file>
</files>
'''
if len(sys.argv) > 1:
print parse(sys.argv[1])
else:
print parseString(s)