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