Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 184 lines (144 sloc) 5.52 kb
362db37 @dagwieers Initial import
authored
1 #!/usr/bin/python
2
5af56ec @dagwieers Updates
authored
3 import sys, os, glob, sqlite
dc5da36 @dagwieers Updates
authored
4 import sarahlib
5
362db37 @dagwieers Initial import
authored
6 from xml.dom.ext.reader import Sax2
7 from xml.dom.NodeFilter import NodeFilter
41d0377 @dagwieers Updates and few docs
authored
8 import xml.sax
362db37 @dagwieers Initial import
authored
9
81b337c @dagwieers Updates to sarahdbo.py
authored
10 def handle_walker(walker):
11 next = walker.nextNode()
12 node = walker.currentNode
13 return node, next
14
362db37 @dagwieers Initial import
authored
15 sys.stdout = os.fdopen(1, 'w', 0)
16
03f8ef9 @dagwieers Important updates
authored
17 con, cur = sarahlib.opendb()
18 sarahlib.createtb(cur, 'adv')
19 sarahlib.createtb(cur, 'ref')
20 sarahlib.createtb(cur, 'rpm')
21 sarahlib.createtb(cur, 'pro')
22
362db37 @dagwieers Initial import
authored
23 reader = Sax2.Reader()
24
0c4ef75 @dagwieers Big update
authored
25 filelist = glob.glob('advisories/RH?A-*.xml')
9e6665e @dagwieers Updates
authored
26 #filelist = glob.glob('advisories/RHSA-*.xml')
27 #filelist = ['advisories/RHSA-2005-791.xml', ]
362db37 @dagwieers Initial import
authored
28 filelist.sort()
29
30 for file in filelist:
41d0377 @dagwieers Updates and few docs
authored
31 try:
32 doc = reader.fromStream(open(file))
33 walker = doc.createTreeWalker(doc.documentElement, NodeFilter.SHOW_ELEMENT, None, 0)
362db37 @dagwieers Initial import
authored
34
0c4ef75 @dagwieers Big update
authored
35 next = True
81b337c @dagwieers Updates to sarahdbo.py
authored
36 node = walker.currentNode
0c4ef75 @dagwieers Big update
authored
37 advrec = {};
38 while next is not None:
81b337c @dagwieers Updates to sarahdbo.py
authored
39 # print node.tagName
0c4ef75 @dagwieers Big update
authored
40
81b337c @dagwieers Updates to sarahdbo.py
authored
41 if node.tagName == 'advisory':
42 advrec['sender'] = node.getAttribute('sender')
43 advrec['version'] = node.getAttribute('version')
0c4ef75 @dagwieers Big update
authored
44
81b337c @dagwieers Updates to sarahdbo.py
authored
45 elif node.tagName == 'id':
46 advrec['advid'] = node.firstChild.data
0c4ef75 @dagwieers Big update
authored
47
81b337c @dagwieers Updates to sarahdbo.py
authored
48 elif node.tagName == 'pushcount':
49 advrec['pushcount'] = int(node.firstChild.data)
0c4ef75 @dagwieers Big update
authored
50
81b337c @dagwieers Updates to sarahdbo.py
authored
51 elif node.tagName == 'type':
52 advrec['type'] = node.getAttribute('short')
0c4ef75 @dagwieers Big update
authored
53
81b337c @dagwieers Updates to sarahdbo.py
authored
54 elif node.tagName == 'keywords':
55 if hasattr(node.firstChild, 'data'):
56 advrec['keywords'] = node.firstChild.data
417ac71 @dagwieers Added python-elementtree based sarahdb and changes to database layout
authored
57
81b337c @dagwieers Updates to sarahdbo.py
authored
58 elif node.tagName == 'obsoletes':
59 advrec['obsoletes'] = node.firstChild.data
417ac71 @dagwieers Added python-elementtree based sarahdb and changes to database layout
authored
60
81b337c @dagwieers Updates to sarahdbo.py
authored
61 elif node.tagName == 'group':
62 advrec['rhgroup'] = node.getAttribute('name')
417ac71 @dagwieers Added python-elementtree based sarahdb and changes to database layout
authored
63
81b337c @dagwieers Updates to sarahdbo.py
authored
64 elif node.tagName == 'severity':
65 if node.hasAttribute('level'):
66 advrec['severity'] = node.getAttribute('level')
67 elif node.firstChild.data:
68 advrec['severity'] = node.firstChild.data
0c4ef75 @dagwieers Big update
authored
69 else:
417ac71 @dagwieers Added python-elementtree based sarahdb and changes to database layout
authored
70 advrec['severity'] = 'error'
0c4ef75 @dagwieers Big update
authored
71
81b337c @dagwieers Updates to sarahdbo.py
authored
72 elif node.tagName == 'synopsis':
73 advrec['synopsis'] = node.firstChild.data
0c4ef75 @dagwieers Big update
authored
74
81b337c @dagwieers Updates to sarahdbo.py
authored
75 elif node.tagName == 'issued':
76 advrec['issued'] = node.getAttribute('date')
0c4ef75 @dagwieers Big update
authored
77
81b337c @dagwieers Updates to sarahdbo.py
authored
78 elif node.tagName == 'updated':
79 advrec['updated'] = node.getAttribute('date')
0c4ef75 @dagwieers Big update
authored
80
81b337c @dagwieers Updates to sarahdbo.py
authored
81 elif node.tagName == 'references':
82 node, next = handle_walker(walker)
83 while node.tagName == 'reference':
0c4ef75 @dagwieers Big update
authored
84 refrec = advrec.copy()
9e6665e @dagwieers Updates
authored
85
81b337c @dagwieers Updates to sarahdbo.py
authored
86 if node.hasAttribute('type'):
87 refrec['reftype'] = node.getAttribute('type')
9e6665e @dagwieers Updates
authored
88
81b337c @dagwieers Updates to sarahdbo.py
authored
89 if node.hasAttribute('href'):
90 refrec['reference'] = node.getAttribute('href')
9e6665e @dagwieers Updates
authored
91
81b337c @dagwieers Updates to sarahdbo.py
authored
92 node, next = handle_walker(walker)
93 while node.tagName in ('advisory', 'bugzilla', 'cve', 'summary'):
94 if node.tagName in ('advisory', 'bugzilla', 'cve'):
95 refrec['refid'] = node.firstChild.data
96 elif node.tagName == 'summary':
97 refrec['summary'] = node.firstChild.data
9e6665e @dagwieers Updates
authored
98 else:
81b337c @dagwieers Updates to sarahdbo.py
authored
99 raise 'Unknown tag <%s> in reference node' % node.tagName
100 node, next = handle_walker(walker)
9e6665e @dagwieers Updates
authored
101
102 if refrec['reftype'] == 'self':
417ac71 @dagwieers Added python-elementtree based sarahdb and changes to database layout
authored
103 refrec['refid'] = advrec['advid']
9e6665e @dagwieers Updates
authored
104
105 if not refrec.has_key('summary'):
0c4ef75 @dagwieers Big update
authored
106 refrec['summary'] = None
9e6665e @dagwieers Updates
authored
107
108 if not refrec.has_key('id'):
417ac71 @dagwieers Added python-elementtree based sarahdb and changes to database layout
authored
109 refrec['refid'] = None
9e6665e @dagwieers Updates
authored
110
0c4ef75 @dagwieers Big update
authored
111 sarahlib.insertrec(cur, 'ref', refrec)
9e6665e @dagwieers Updates
authored
112 con.commit()
0c4ef75 @dagwieers Big update
authored
113 continue
114
81b337c @dagwieers Updates to sarahdbo.py
authored
115 elif node.tagName == 'topic':
116 advrec['topic'] = node.firstChild.data
0c4ef75 @dagwieers Big update
authored
117
81b337c @dagwieers Updates to sarahdbo.py
authored
118 elif node.tagName == 'description':
119 advrec['description'] = node.firstChild.data
0c4ef75 @dagwieers Big update
authored
120
81b337c @dagwieers Updates to sarahdbo.py
authored
121 elif node.tagName == 'rpmlist':
122 node, next = handle_walker(walker)
123 while node.tagName == 'product':
0c4ef75 @dagwieers Big update
authored
124 prorec = advrec.copy()
81b337c @dagwieers Updates to sarahdbo.py
authored
125 prorec['prodshort'] = node.getAttribute('short')
77d9170 @dagwieers Updates
authored
126 ### FIXME: Do proper nested parsing
81b337c @dagwieers Updates to sarahdbo.py
authored
127 node, next = handle_walker(walker)
128 prorec['product'] = node.firstChild.data
0c4ef75 @dagwieers Big update
authored
129 ### FIXME: Create a unique insert function
130 try: sarahlib.insertrec(cur, 'pro', prorec)
131 except: pass
81b337c @dagwieers Updates to sarahdbo.py
authored
132 node, next = handle_walker(walker)
133 while node.tagName == 'file':
9e6665e @dagwieers Updates
authored
134 rpmrec = advrec.copy()
81b337c @dagwieers Updates to sarahdbo.py
authored
135 rpmrec['arch'] = node.getAttribute('arch')
0c4ef75 @dagwieers Big update
authored
136 rpmrec['prodshort'] = prorec['prodshort']
417ac71 @dagwieers Added python-elementtree based sarahdb and changes to database layout
authored
137 rpmrec['channels'] = []
0c4ef75 @dagwieers Big update
authored
138 ### FIXME: Do proper nested parsing
81b337c @dagwieers Updates to sarahdbo.py
authored
139 node, next = handle_walker(walker)
140 while node.tagName in ('filename', 'sum', 'channel'):
141 if node.tagName == 'filename':
142 rpmrec['filename'] = node.firstChild.data
143 elif node.tagName == 'sum':
144 rpmrec['md5'] = node.firstChild.data
145 elif node.tagName == 'channel':
146 rpmrec['channels'].append(node.getAttribute('name'))
0c4ef75 @dagwieers Big update
authored
147 else:
81b337c @dagwieers Updates to sarahdbo.py
authored
148 raise 'Unknown tag <%s> in file node' % node.tagName
149 node, next = handle_walker(walker)
0c4ef75 @dagwieers Big update
authored
150 sarahlib.insertrec(cur, 'rpm', rpmrec)
03f8ef9 @dagwieers Important updates
authored
151 con.commit()
0c4ef75 @dagwieers Big update
authored
152 continue
417ac71 @dagwieers Added python-elementtree based sarahdb and changes to database layout
authored
153
81b337c @dagwieers Updates to sarahdbo.py
authored
154 elif node.tagName in ('a', 'contact', 'p', 'product', 'rights', 'rpmtext', 'solution'):
417ac71 @dagwieers Added python-elementtree based sarahdb and changes to database layout
authored
155 pass
156
157 else:
81b337c @dagwieers Updates to sarahdbo.py
authored
158 print 'Unknown tag <%s> in advisory node' % str(node.tagName)
0c4ef75 @dagwieers Big update
authored
159
81b337c @dagwieers Updates to sarahdbo.py
authored
160 node, next = handle_walker(walker)
362db37 @dagwieers Initial import
authored
161
0c4ef75 @dagwieers Big update
authored
162 ### RHBAs and RHEAs do not have a severity level
163 if advrec['type'] in ('RHBA', 'RHEA'):
616b237 @dagwieers Leftover references to severitylevel
authored
164 advrec['severity'] = None
81b337c @dagwieers Updates to sarahdbo.py
authored
165 if not advrec.has_key('severity'):
166 advrec['severity'] = 'unknown'
417ac71 @dagwieers Added python-elementtree based sarahdb and changes to database layout
authored
167
616b237 @dagwieers Leftover references to severitylevel
authored
168 if not advrec.has_key('severity'): advrec['severity'] = 'unknown'
417ac71 @dagwieers Added python-elementtree based sarahdb and changes to database layout
authored
169 if not advrec.has_key('rhgroup'): advrec['rhgroup'] = None
170 if not advrec.has_key('keywords'): advrec['keywords'] = None
171 if not advrec.has_key('obsoletes'): advrec['obsoletes'] = None
0c4ef75 @dagwieers Big update
authored
172
173 sarahlib.insertrec(cur, 'adv', advrec)
174 # con.commit()
362db37 @dagwieers Initial import
authored
175
0c4ef75 @dagwieers Big update
authored
176 print '\033[0;32m%s\033[0;0m' % os.path.basename(file).strip('.xml'),
177
178 except (xml.sax._exceptions.SAXParseException, AttributeError, KeyError), e:
179 print '\033[0;31m%s\033[0;0m' % os.path.basename(file),
180 print e
c12201d @dagwieers Updates
authored
181 raise
182 # continue
0c4ef75 @dagwieers Big update
authored
183 con.commit()
Something went wrong with that request. Please try again.