summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoão Abecasis <joao.abecasis@nokia.com>2011-01-31 15:20:17 +0100
committerJoão Abecasis <joao.abecasis@nokia.com>2011-02-02 14:30:11 +0100
commitf5116d3872e65460577aa9a1e4aad2368279d05b (patch)
treec461cf4c2f9670fd0ed299b051c760e84d89d0ff
parent0889855543400b0ac8ec99cce4ebc6712f294289 (diff)
Use python's JSON library to generate output
... instead of hand-coding it. To that end, data structures are populated at the same time the database queries are made and the writeOutputAsJSON functions become thin wrappers over json.dump. Reviewed-by: jasplin
-rw-r--r--scripts/getsnapshots.py27
-rw-r--r--scripts/listcontexts.py42
2 files changed, 22 insertions, 47 deletions
diff --git a/scripts/getsnapshots.py b/scripts/getsnapshots.py
index 1e04615..ecbd7d8 100644
--- a/scripts/getsnapshots.py
+++ b/scripts/getsnapshots.py
@@ -1,3 +1,6 @@
+import sys
+import json
+
from misc import idToText, textToId, getSnapshots, printJSONHeader
class GetSnapshots:
@@ -16,28 +19,16 @@ class GetSnapshots:
self.sha12_id = textToId("sha1", sha12)
def execute(self):
- self.snapshots = getSnapshots(
- self.host_id, self.platform_id, self.branch_id, self.sha11_id,
- self.sha12_id)
+ self.snapshots = []
+ for sha1_id, timestamp in getSnapshots(self.host_id, self.platform_id,
+ self.branch_id, self.sha11_id, self.sha12_id):
+ self.snapshots.append((idToText('sha1', sha1_id), timestamp))
+
self.writeOutput()
def writeOutputAsJSON(self):
printJSONHeader()
- print "{"
-
- # Snapshots:
- print "\"snapshots\": ["
- first_row = True
- for sha1_id, timestamp in self.snapshots:
- if not first_row:
- print ",",
- first_row = False
- print (
- "[\"" + str(idToText("sha1", sha1_id)) + "\", " +
- str(timestamp) + "]")
- print "]"
-
- print "}"
+ json.dump({ 'snapshots': self.snapshots }, sys.stdout)
class GetSnapshotsAsJSON(GetSnapshots):
def writeOutput(self):
diff --git a/scripts/listcontexts.py b/scripts/listcontexts.py
index 97cb89b..f6b1291 100644
--- a/scripts/listcontexts.py
+++ b/scripts/listcontexts.py
@@ -1,3 +1,6 @@
+import sys
+import json
+
from dbaccess import execQuery
from misc import idToText, getAllSnapshots, printJSONHeader
@@ -9,40 +12,21 @@ class ListContexts:
"SELECT DISTINCT hostId, platformId, branchId FROM result " +
"ORDER BY hostId, platformId, branchId;")
- contexts = []
- for hpb in hpbs:
- snapshots = getAllSnapshots(hpb[0], hpb[1], hpb[2], True)
- contexts.append((hpb, snapshots))
+ self.contexts = []
+ for host, platform, branch in hpbs:
+ snapshots = getAllSnapshots(host, platform, branch, True)
+ self.contexts.append({
+ 'host' : idToText('host', host),
+ 'platform' : idToText('platform', platform),
+ 'branch' : idToText('branch', branch),
+ 'snapshots' : curSnapshots
+ })
- self.contexts = tuple(contexts)
self.writeOutput()
def writeOutputAsJSON(self):
printJSONHeader()
- print "{ \"contexts\": ["
- first_context = True
- for context in self.contexts:
- if not first_context:
- print ","
- first_context = False
-
- print "{"
- print "\"host\": \"" + idToText("host", context[0][0]) + "\",",
- print "\"platform\": \"" + idToText(
- "platform", context[0][1]) + "\",",
- print "\"branch\": \"" + idToText("branch", context[0][2]) + "\","
- print "\"snapshots\": [",
- first_snapshot = True
- for snapshot in context[1]:
- if not first_snapshot:
- print ",",
- first_snapshot = False
- print ("[\"" + str(idToText("sha1", snapshot[0])) + "\"," +
- str(snapshot[1]) + "]"),
-
- print "]"
- print "}"
- print "]}"
+ json.dump({ 'contexts' : self.contexts }, sys.stdout)
class ListContextsAsJSON(ListContexts):
def writeOutput(self):