[Secure-testing-commits] r32716 - bin

Holger Levsen holger at moszumanska.debian.org
Mon Mar 9 17:06:01 UTC 2015


Author: holger
Date: 2015-03-09 17:06:01 +0000 (Mon, 09 Mar 2015)
New Revision: 32716

Modified:
   bin/tracker_service.py
Log:
use python json library to create json

Modified: bin/tracker_service.py
===================================================================
--- bin/tracker_service.py	2015-03-09 17:05:59 UTC (rev 32715)
+++ bin/tracker_service.py	2015-03-09 17:06:01 UTC (rev 32716)
@@ -6,6 +6,7 @@
 import re
 import security_db
 from web_support import *
+import json
 
 if len(sys.argv) not in (3, 5):
     print "usage: python tracker_service.py SOCKET-PATH DATABASE-PATH"
@@ -23,6 +24,20 @@
     db_name = sys.argv[4]
     webservice_base_class = WebServiceHTTP
 
+def clean_dict(d):
+    """ taken from http://w3facility.org/question/exclude-emptynull-values-from-json-serialization/
+    Delete keys with the value ``None`` in a dictionary, recursively.
+
+    This alters the input so you may wish to ``copy`` the dict first.
+    """
+    # d.iteritems isn't used as you can't del or the iterator breaks.
+    for key, value in d.items():
+        if value is None:
+            del d[key]
+        elif isinstance(value, dict):
+            clean_dict(value)
+    return d  # For convenience
+
 class BugFilter:
     default_action_list = [('high_urgency', 'high', 'urgency'),
                            ('medium_urgency', 'medium', 'urgency'),
@@ -1315,34 +1330,33 @@
             if str(db_nodsa) != 'None':
                 nodsa[pkg][issue][repository] = db_nodsa
 
-        data = []
-        data.append('[\n')
+        data = {}
         for pkg in packages:
-            data.append('  {\n')
-            data.append('    "package": "'+pkg+'",\n')
-            data.append('    "issues": [\n')
+            pkg_issues = []
             for issue in issues[pkg]:
-                data.append('      {\n')
-                data.append('        "issue": "'+issue+'",\n')
+                description = None
+                debianbug = None
+                scope = None
+                repo = {}
+                suites = {}
+
                 if descriptions[issue]:
-                    data.append('        "description": "'+descriptions[issue]+'",\n')
+                    description = descriptions[issue]
                 if debianbugs[pkg][issue] != None:
-                    data.append('        "debian bug": '+str(debianbugs[pkg][issue])+',\n')
+                    debianbug = debianbugs[pkg][issue]
                 if str(remote[pkg][issue]) == 'None':
                     pass
-                    #data.append('        "range": null,\n')
                 elif remote[pkg][issue] == 1:
-                    data.append('        "range": "remote",\n')
+                    scope = "remote"
                 else:
-                    data.append('        "range": "local",\n')
-                data.append('        "repositories": [\n')
+                    scope = "local"
                 for repository in repositories[pkg][issue]:
-                    data.append('          {\n')
-                    data.append('            "'+repository+'": "'+version[pkg][issue][repository]+'"\n')
-                    data.append('          },\n')
-                data.append('        ],\n')
-                data.append('        "releases": [\n')
+                    repo[repository] = version [pkg][issue][repository]
                 for release in releases[pkg][issue]:
+                    state = None
+                    suite_version = None
+                    suite_urgency = None
+                    suite_nodsa = None
                     winner=''
                     for suffix in ('','-security','-lts'):
                         subrelease=release+suffix
@@ -1356,31 +1370,35 @@
                                 # keep looking for a real winner...
                                 winner=suffix
                     repository=release+winner
-                    data.append('          "'+release+'": [\n')
-                    data.append('            {\n')
-                    data.append('              "repository": "'+repository+'",\n')
                     if status[pkg][issue][repository] == 0:
                         # 1 = vulnerable, 2 = undetermined
-                        data.append('              "status": "resolved",\n')
-                        data.append('              "version": "'+fixed_version[pkg][issue][repository]+'",\n')
+                        state = "resolved"
+                        suite_version = fixed_version[pkg][issue][repository]
                     else:
-                        data.append('              "status": "open",\n')
-                        data.append('              "version": "'+version[pkg][issue][repository]+'",\n')
-                    # urgency really differs by suite for some CVEs
-                    # also, the urgency field holds the 'end-of-life' information
-                    data.append('              "urgency": "'+urgency[pkg][issue][repository]+'",\n')
+                        state = "open"
+                        suite_version = version[pkg][issue][repository]
+                    suite_urgency = urgency[pkg][issue][repository]
                     if nodsa[pkg][issue].has_key(repository):
-                        data.append('              "nodsa": "'+nodsa[pkg][issue][repository]+'",\n')
-                    data.append('            },\n')
-                    data.append('          ], \n')
-                data.append('        ],\n')
-                data.append('      },\n')
-            data.append('    ],\n')
-            data.append('  },\n')
-        data.append(']\n')
+                        suite_nodsa = nodsa[pkg][issue][repository]
+                    suites[repository] = { "status": state,
+                                          "version": suite_version,
+                                          "urgency": suite_urgency,
+                                          "nodsa": suite_nodsa }
+                    clean_dict(suites[repository])
+                pkg_issue = { "issue": issue,
+                              "description": description,
+                              "debianbug": debianbug,
+                              "scope": scope,
+                              "repositories": repo,
+                              "releases": suites }
+                clean_dict(pkg_issue)
 
-        return BinaryResult(''.join(data),'application/octet-stream')
+                pkg_issues.append(pkg_issue)
+            data[pkg] = pkg_issues
 
+        json_data = json.dumps(data, sort_keys=True, indent=True)
+        return BinaryResult(json_data,'application/octet-stream')
+
     def page_debsecan(self, path, params, url):
         obj = '/'.join(path)
         data = self.db.getDebsecan(obj)




More information about the Secure-testing-commits mailing list