Release coccinelle-0.1.3
[bpt/coccinelle.git] / python / coccilib / output.py
CommitLineData
34e49164
C
1import pygtk
2import gtk, gobject
3import coccilib.coccigui
4import coccilib.coccigui.coccigui
5from threading import Thread, Lock
6import time
7from copy import deepcopy
8
9class Output:
10 """In order to implement an output class for use with Coccinelle,
11 one can inherit from this class and overload register_match with
12 the same number of arguments.
13
14 include_match will be overwritten by inheriting from your actual
15 class, and thus if your class is a.b.C then Coccinelle will create
16 a Python class "class Coccinelle(a.b.C)" that hooks include_match
17 into the O'Caml internals.
18 """
19 def include_match(self, b):
20 pass
21
22 def register_match(self, include, messages):
23 pass
24
25 def combine(self, meta_variable, locations):
26 nmv = deepcopy(meta_variable)
27 nloc = [deepcopy(loc) for loc in locations]
28 nmv.location = nloc[0]
29 nmv.locations = nloc
30
31 return nmv
32
33 def finalise(self):
34 pass
35
91eba41f
C
36 def print_main(self, p) :
37 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)
38
39 def print_sec(self, msg, p) :
40 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)
41
34e49164
C
42class Console(Output):
43 def __init__(self):
44 pass
45
46 def register_match(self, include, messages):
47 self.include_match(include)
48 if include:
49 for variable, message in messages:
50 print "%s:%s:%s: %s - %s" % (variable.location.file, variable.location.line, variable.location.column, message, variable)
51
52class GtkRunner(Thread):
53 def __init__(self):
54 Thread.__init__(self)
55 self.lock = Lock()
56 self.rows = []
57
58 def add_row(self, cocci, l):
59 for i in xrange(0, len(l)):
60 l[i] = (l[i][1], l[i][0].location.file, l[i][0].location.line, l[i][0].location.column)
61
62 self.lock.acquire()
63 try:
64 self.rows.append((cocci, l))
65 finally:
66 self.lock.release()
67
68 def has_row(self):
69 self.lock.acquire()
70 try:
71 return len(self.rows) > 0
72 finally:
73 self.lock.release()
74
75 def get_row(self):
76 self.lock.acquire()
77 try:
78 return self.rows.pop(0)
79 finally:
80 self.lock.release()
81
82 def update(self):
83 while self.has_row():
84 cocci, l = self.get_row()
85 self.gui.add_result(cocci, l)
86 gobject.timeout_add(1000, self.update)
87
88 def run(self):
89 self.gui = coccilib.coccigui.coccigui.pycocci()
90 globals()['gtk_sock'] = self.gui
91 gobject.timeout_add(1000, self.update)
92
93 gtk.gdk.threads_init()
94 gtk.gdk.threads_enter()
95
96 gtk.main()
97
98 gtk.gdk.threads_leave()
99
100 globals().pop('gtk_thread')
101 globals().pop('gtk_sock')
102
103class Gtk(Output):
104 def check_availability(self):
105 if not globals().has_key('gtk_sock'):
106 t = GtkRunner()
107 globals()['gtk_thread'] = t
108 globals()['gtk_thread'].start()
109 time.sleep(2)
110
111 def register_match(self, include, messages):
112 self.check_availability()
113
114 self.include_match(include)
115 if include:
116 globals()['gtk_thread'].add_row(self.cocci_file, messages)
117
118 def finalise(self):
119 self.check_availability()
120
121 globals()['gtk_thread'].join()