1ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao#!/usr/bin/env python
2ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao#
3ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao#                     The LLVM Compiler Infrastructure
4ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao#
5ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao# This file is distributed under the University of Illinois Open Source
6ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao# License. See LICENSE.TXT for details.
7ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao#
8ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao##===----------------------------------------------------------------------===##
9ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao#
10ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao# This script reads visualization data emitted by the static analyzer for
11ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao# display in Ubigraph.
12ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao#
13ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao##===----------------------------------------------------------------------===##
14ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao
15ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liaoimport xmlrpclib
16ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liaoimport sys
17ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao
18ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liaodef Error(message):
19ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    print >> sys.stderr, 'ubiviz: ' + message
20ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    sys.exit(1)
21ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    
22ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liaodef StreamData(filename):
23ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao  file = open(filename)
24ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao  for ln in file:
25ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    yield eval(ln)
26ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao  file.close()
27ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao
28ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liaodef Display(G, data):
29ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao  action = data[0]
30ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao  if action == 'vertex':
31ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    vertex = data[1]
32ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    G.new_vertex_w_id(vertex)
33ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    for attribute in data[2:]:
34ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao      G.set_vertex_attribute(vertex, attribute[0], attribute[1])
35ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao  elif action == 'edge':
36ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    src = data[1]
37ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    dst = data[2]
38ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    edge = G.new_edge(src,dst)
39ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    for attribute in data[3:]:
40ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao      G.set_edge_attribute(edge, attribute[0], attribute[1])
41ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao  elif action == "vertex_style":
42ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    style_id = data[1]
43ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    parent_id = data[2]
44ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    G.new_vertex_style_w_id(style_id, parent_id)
45ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    for attribute in data[3:]:
46ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao      G.set_vertex_style_attribute(style_id, attribute[0], attribute[1])
47ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao  elif action == "vertex_style_attribute":
48ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    style_id = data[1]
49ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    for attribute in data[2:]:
50ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao      G.set_vertex_style_attribute(style_id, attribute[0], attribute[1])
51ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao  elif action == "change_vertex_style":
52ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao     vertex_id = data[1]
53ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao     style_id = data[2]
54ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao     G.change_vertex_style(vertex_id,style_id)
55ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao
56ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liaodef main(args):
57ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao  if len(args) == 0:
58ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    Error('no input files')    
59ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao
60ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao  server = xmlrpclib.Server('http://127.0.0.1:20738/RPC2')
61ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao  G = server.ubigraph
62ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao            
63ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao  for arg in args:
64ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    G.clear()
65ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    for x in StreamData(arg):
66ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao      Display(G,x)
67ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao  
68ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao  sys.exit(0)
69ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao  
70ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao
71ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liaoif __name__ == '__main__':
72ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    main(sys.argv[1:])
73ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao    
74ea285162342df160e7860e26528bc7110bc6c0cdShih-wei Liao