Release coccinelle-0.1.10
[bpt/coccinelle.git] / python / coccilib / output_trac.py
CommitLineData
0708f913
C
1import pygtk
2import gtk, gobject
3import coccilib.coccigui
4import coccilib.coccigui.coccigui
5from threading import Thread, Lock
6import time
7from copy import deepcopy
8import psycopg2
9from datetime import date, datetime
10from trac.util.datefmt import to_timestamp, utc
11
12
13class Output:
14 """In order to implement an output class for use with Coccinelle,
15 one can inherit from this class and overload register_match with
16 the same number of arguments.
17
18 include_match will be overwritten by inheriting from your actual
19 class, and thus if your class is a.b.C then Coccinelle will create
20 a Python class "class Coccinelle(a.b.C)" that hooks include_match
21 into the O'Caml internals.
22 """
23 def include_match(self, b):
24 pass
25
26 def register_match(self, include, messages):
27 pass
28
29 def combine(self, meta_variable, locations):
30 nmv = deepcopy(meta_variable)
31 nloc = [deepcopy(loc) for loc in locations]
32 nmv.location = nloc[0]
33 nmv.locations = nloc
34
35 return nmv
36
37 def finalise(self):
38 pass
39
40 def print_main(self, p) :
41 print "* TODO [[view:%s::face=ovl-face1::linb=%s::colb=%s::cole=%s][%s::%s]]" % (p[0].file,p[0].line,p[0].column,p[0].column_end,p[0].file,p[0].line)
42
43 def print_sec(self, msg, p) :
44 print "[[view:%s::face=ovl-face2::linb=%s::colb=%s::cole=%s][%s]]" % (p[0].file,p[0].line,p[0].column,p[0].column_end,msg)
45
46 def print_secs(self, msg, ps) :
47 for i in ps:
48 print "[[view:%s::face=ovl-face2::linb=%s::colb=%s::cole=%s][%s]]" % (i.file,i.line,i.column,i.column_end,msg)
49
50 def add_ticket(self, dbinfo, summary, desc) :
51 conn = psycopg2.connect(dbinfo)
52 curs = conn.cursor()
53 created = to_timestamp(datetime.now(utc))
54 curs.execute("INSERT INTO ticket \
55 (type,time,changetime,component,priority,owner,reporter,cc,version,milestone,status,summary,description, keywords) \
56 VALUES \
57 ('defect', %s, %s, 'other','major','somebody','Coccinelle','','next','','new','%s','%s', '')" % (created, created, summary, desc) )
58 conn.commit()
59
60class Console(Output):
61 def __init__(self):
62 pass
63
64 def register_match(self, include, messages):
65 self.include_match(include)
66 if include:
67 for variable, message in messages:
68 print "%s:%s:%s: %s - %s" % (variable.location.file, variable.location.line, variable.location.column, message, variable)
69
70class GtkRunner(Thread):
71 def __init__(self):
72 Thread.__init__(self)
73 self.lock = Lock()
74 self.rows = []
75
76 def add_row(self, cocci, l):
77 for i in xrange(0, len(l)):
78 l[i] = (l[i][1], l[i][0].location.file, l[i][0].location.line, l[i][0].location.column)
79
80 self.lock.acquire()
81 try:
82 self.rows.append((cocci, l))
83 finally:
84 self.lock.release()
85
86 def has_row(self):
87 self.lock.acquire()
88 try:
89 return len(self.rows) > 0
90 finally:
91 self.lock.release()
92
93 def get_row(self):
94 self.lock.acquire()
95 try:
96 return self.rows.pop(0)
97 finally:
98 self.lock.release()
99
100 def update(self):
101 while self.has_row():
102 cocci, l = self.get_row()
103 self.gui.add_result(cocci, l)
104 gobject.timeout_add(1000, self.update)
105
106 def run(self):
107 self.gui = coccilib.coccigui.coccigui.pycocci()
108 globals()['gtk_sock'] = self.gui
109 gobject.timeout_add(1000, self.update)
110
111 gtk.gdk.threads_init()
112 gtk.gdk.threads_enter()
113
114 gtk.main()
115
116 gtk.gdk.threads_leave()
117
118 globals().pop('gtk_thread')
119 globals().pop('gtk_sock')
120
121class Gtk(Output):
122 def check_availability(self):
123 if not globals().has_key('gtk_sock'):
124 t = GtkRunner()
125 globals()['gtk_thread'] = t
126 globals()['gtk_thread'].start()
127 time.sleep(2)
128
129 def register_match(self, include, messages):
130 self.check_availability()
131
132 self.include_match(include)
133 if include:
134 globals()['gtk_thread'].add_row(self.cocci_file, messages)
135
136 def finalise(self):
137 self.check_availability()
138
139 globals()['gtk_thread'].join()