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
|
import sys
import json
from dbaccess import execQuery
from misc import idToText, printJSONHeader
class ListContexts:
def __init__(self):
pass
def execute(self):
# Get all host/platform/branch/sha1 combinations:
contexts = execQuery(
"SELECT id, hostId, platformId, branchId, sha1Id,"
" EXTRACT(EPOCH FROM timestamp)::INT"
" FROM context"
" ORDER BY hostId, platformId,"
" branchId, timestamp DESC;", ()) \
+ [(-1, -1, -1, -1, -1, -1)] # Sentinel value
self.contexts = []
if not len(contexts):
return
id, host, platform, branch, sha1, timestamp = contexts[0]
curHost, curPlatform, curBranch = host, platform, branch
curSnapshots = [(idToText('sha1', sha1), timestamp)]
for (id, host, platform, branch, sha1, timestamp) in contexts[1:]:
if (host, platform, branch) != (curHost, curPlatform, curBranch):
self.contexts.append({
'host' : idToText('host', curHost),
'platform' : idToText('platform', curPlatform),
'branch' : idToText('branch', curBranch),
'snapshots' : curSnapshots
})
curHost, curPlatform, curBranch = host, platform, branch
curSnapshots = []
curSnapshots.append((idToText('sha1', sha1), timestamp))
self.writeOutput()
def writeOutputAsJSON(self):
printJSONHeader()
json.dump({ 'contexts' : self.contexts }, sys.stdout)
class ListContextsAsJSON(ListContexts):
def writeOutput(self):
self.writeOutputAsJSON()
|