Commit | Line | Data |
---|---|---|
0708f913 C |
1 | import pygtk |
2 | import gtk, gobject | |
3 | import coccilib.coccigui | |
4 | import coccilib.coccigui.coccigui | |
5 | from threading import Thread, Lock | |
6 | import time | |
7 | from copy import deepcopy | |
8 | import psycopg2 | |
9 | from datetime import date, datetime | |
10 | from trac.util.datefmt import to_timestamp, utc | |
11 | ||
12 | ||
13 | class 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 | ||
60 | class 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 | ||
70 | class 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 | ||
121 | class 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() |