[Secure-testing-commits] r14626 - lib/python

Florian Weimer fw at alioth.debian.org
Fri May 7 20:37:27 UTC 2010


Author: fw
Date: 2010-05-07 20:37:27 +0000 (Fri, 07 May 2010)
New Revision: 14626

Modified:
   lib/python/parsers.py
Log:
parsers.cvelist(): preliminary CVE list parser


Modified: lib/python/parsers.py
===================================================================
--- lib/python/parsers.py	2010-05-07 19:26:36 UTC (rev 14625)
+++ lib/python/parsers.py	2010-05-07 20:37:27 UTC (rev 14626)
@@ -191,6 +191,73 @@
             addmessage(messages, file, line, "error", "invalid annotation"))
 _annotationdispatcher = _annotationdispatcher()
 
+List = xcollections.namedtuple("List", "list messages")
+Bug = xcollections.namedtuple("Bug", "file header annotations")
+Header = xcollections.namedtuple("Header", "line name description")
+
+def _cveuniquename(line, anns):
+    bug = 0
+    for ann in anns:
+        if ann.type == "package" and ann.debian_bugs:
+            bug = ann.debian_bugs[0]
+            break
+    return "TEMP-%07d-%06d" % (bug, line)
+
+_re_cve_header = re.compile(r'^(CVE-\d{4}-(?:\d{4}|XXXX))\s+(.*?)\s*$')
+ at xpickle.loader("CVE" + FORMAT)
+def cvelist(path, f):
+    lineno = 0
+    headerlineno = None
+    bugs = []
+    messages = []
+    name = desc = None
+    anns = []
+
+    def emit():
+        if name is None:
+            return
+
+        if name[-1] == "X":
+            name1 = _cveuniquename(headerlineno, anns)
+        else:
+            name1 = name
+        bugs.append(Bug(path, Header(headerlineno, name1, desc), tuple(anns)))
+        del anns[:]
+
+    for line in f.readlines():
+        lineno += 1
+        if line[:1] in " \t":
+            if name is None:
+                addmessage(messages, path, lineno, "error", "header expected")
+                continue
+            _annotationdispatcher(line, path, lineno, messages, anns)
+        else:
+            emit()
+            headerlineno = lineno
+        
+            match = _re_cve_header.match(line)
+            if match is None:
+                addmessage(message, path, lineno, "error", "malformed header")
+                name = desc = None
+                continue
+            name, desc = match.groups()
+            if desc:
+                if desc[0] == '(':
+                    if desc[-1] <> ')':
+                        addmessage(message, path, lineno, "error", 
+                                   "missing ')'")
+                    else:
+                        desc = desc[1:-1]
+                elif desc[0] == '[':
+                    if desc[-1] <> ']':
+                        addmessage(message, path, lineno, "error",
+                                   "missing ']'")
+                    else:
+                        desc = desc[1:-1]
+
+    emit()
+    return List(tuple(bugs), tuple(messages))
+
 def _test():
     o = binarypackages("../../data/packages/sid__main_i386_Packages")
     assert type(o) == type(())
@@ -200,6 +267,11 @@
     assert type(o) == type({})
     assert "bash" in o
 
+    xpickle.safeunlink("../../data/CVE/list" + xpickle.EXTENSION)
+    o = cvelist("../../data/CVE/list")
+    for err in o.messages:
+        print "%s:%d: %s: %s" % (err.file, err.line, err.level, err.message)
+
     for (line, res, xmsgs) in [
             (' - foo <unfixed>',
              PackageAnnotation(17, "package", None, "foo", "unfixed", None,




More information about the Secure-testing-commits mailing list