14 |
14 |
15 |
15 |
16 def initService(): |
16 def initService(): |
17 """ |
17 """ |
18 Initialize the service and return the entry point. |
18 Initialize the service and return the entry point. |
19 |
19 |
20 @return the entry point for the background client (function) |
20 @return the entry point for the background client (function) |
21 """ |
21 """ |
22 return vultureCheck |
22 return vultureCheck |
23 |
23 |
24 |
24 |
25 def initBatchService(): |
25 def initBatchService(): |
26 """ |
26 """ |
27 Initialize the batch service and return the entry point. |
27 Initialize the batch service and return the entry point. |
28 |
28 |
29 @return the entry point for the background client (function) |
29 @return the entry point for the background client (function) |
30 """ |
30 """ |
31 return batchVultureCheck |
31 return batchVultureCheck |
32 |
32 |
33 |
33 |
34 def vultureCheck(file, text=""): |
34 def vultureCheck(file, text=""): |
35 """ |
35 """ |
36 Private function to analyze one file. |
36 Private function to analyze one file. |
37 |
37 |
38 @param file source filename |
38 @param file source filename |
39 @type str |
39 @type str |
40 @param text source text |
40 @param text source text |
41 @type str |
41 @type str |
42 @return tuple containing the result dictionary |
42 @return tuple containing the result dictionary |
46 |
46 |
47 |
47 |
48 def batchVultureCheck(argumentsList, send, fx, cancelled, maxProcesses=0): |
48 def batchVultureCheck(argumentsList, send, fx, cancelled, maxProcesses=0): |
49 """ |
49 """ |
50 Module function to analyze a batch of files. |
50 Module function to analyze a batch of files. |
51 |
51 |
52 @param argumentsList list of arguments tuples as given for vultureCheck |
52 @param argumentsList list of arguments tuples as given for vultureCheck |
53 @type list |
53 @type list |
54 @param send reference to send function |
54 @param send reference to send function |
55 @type function |
55 @type function |
56 @param fx registered service name |
56 @param fx registered service name |
80 for task in argumentsList[:initialTasks]: |
80 for task in argumentsList[:initialTasks]: |
81 taskQueue.put(task) |
81 taskQueue.put(task) |
82 |
82 |
83 # Start worker processes |
83 # Start worker processes |
84 workers = [ |
84 workers = [ |
85 multiprocessing.Process( |
85 multiprocessing.Process(target=workerTask, args=(taskQueue, doneQueue)) |
86 target=workerTask, args=(taskQueue, doneQueue) |
86 for _ in range(NumberOfProcesses) |
87 ) for _ in range(NumberOfProcesses) |
|
88 ] |
87 ] |
89 for worker in workers: |
88 for worker in workers: |
90 worker.start() |
89 worker.start() |
91 |
90 |
92 # Get and send results |
91 # Get and send results |
93 endIndex = len(argumentsList) - initialTasks |
92 endIndex = len(argumentsList) - initialTasks |
94 for i in range(len(argumentsList)): |
93 for i in range(len(argumentsList)): |
95 resultSent = False |
94 resultSent = False |
96 wasCancelled = False |
95 wasCancelled = False |
97 |
96 |
98 while not resultSent: |
97 while not resultSent: |
99 try: |
98 try: |
100 # get result (waiting max. 3 seconds and send it to frontend |
99 # get result (waiting max. 3 seconds and send it to frontend |
101 filename, result = doneQueue.get() |
100 filename, result = doneQueue.get() |
102 send(fx, filename, result) |
101 send(fx, filename, result) |
104 except queue.Empty: |
103 except queue.Empty: |
105 # ignore empty queue, just carry on |
104 # ignore empty queue, just carry on |
106 if cancelled(): |
105 if cancelled(): |
107 wasCancelled = True |
106 wasCancelled = True |
108 break |
107 break |
109 |
108 |
110 if wasCancelled or cancelled(): |
109 if wasCancelled or cancelled(): |
111 # just exit the loop ignoring the results of queued tasks |
110 # just exit the loop ignoring the results of queued tasks |
112 break |
111 break |
113 |
112 |
114 if i < endIndex: |
113 if i < endIndex: |
115 taskQueue.put(argumentsList[i + initialTasks]) |
114 taskQueue.put(argumentsList[i + initialTasks]) |
116 |
115 |
117 # Tell child processes to stop |
116 # Tell child processes to stop |
118 for _ in range(NumberOfProcesses): |
117 for _ in range(NumberOfProcesses): |
119 taskQueue.put('STOP') |
118 taskQueue.put("STOP") |
120 |
119 |
121 for worker in workers: |
120 for worker in workers: |
122 worker.join() |
121 worker.join() |
123 worker.close() |
122 worker.close() |
124 |
123 |
125 |
124 |
126 def workerTask(inputQueue, outputQueue): |
125 def workerTask(inputQueue, outputQueue): |
127 """ |
126 """ |
128 Module function acting as the parallel worker for the vulture check. |
127 Module function acting as the parallel worker for the vulture check. |
129 |
128 |
130 @param inputQueue input queue |
129 @param inputQueue input queue |
131 @type multiprocessing.Queue |
130 @type multiprocessing.Queue |
132 @param outputQueue output queue |
131 @param outputQueue output queue |
133 @type multiprocessing.Queue |
132 @type multiprocessing.Queue |
134 """ |
133 """ |
135 for filename, source in iter(inputQueue.get, 'STOP'): |
134 for filename, source in iter(inputQueue.get, "STOP"): |
136 result = __analyze(filename, source) |
135 result = __analyze(filename, source) |
137 outputQueue.put((filename, result)) |
136 outputQueue.put((filename, result)) |
138 |
137 |
139 |
138 |
140 def __analyze(filename, text=""): |
139 def __analyze(filename, text=""): |
141 """ |
140 """ |
142 Private function to analyze one Python file. |
141 Private function to analyze one Python file. |
143 |
142 |
144 @param filename source file name |
143 @param filename source file name |
145 @type str |
144 @type str |
146 @param text source text |
145 @param text source text |
147 @type str |
146 @type str |
148 @return tuple containing the result dictionary |
147 @return tuple containing the result dictionary |
152 v = EricVulture() |
151 v = EricVulture() |
153 v.scan(text, filename=filename) |
152 v.scan(text, filename=filename) |
154 res = v.getResults() |
153 res = v.getResults() |
155 except Exception as err: |
154 except Exception as err: |
156 res = {"error": str(err)} |
155 res = {"error": str(err)} |
157 return (res, ) |
156 return (res,) |
158 |
157 |
159 |
158 |
160 class EricVulture(Vulture): |
159 class EricVulture(Vulture): |
161 """ |
160 """ |
162 Class to adopt the Vulture class to the eric plug-in functionality. |
161 Class to adopt the Vulture class to the eric plug-in functionality. |
163 """ |
162 """ |
|
163 |
164 def __item2Dict(self, item): |
164 def __item2Dict(self, item): |
165 """ |
165 """ |
166 Private method to convert a vulture item to a dictionary. |
166 Private method to convert a vulture item to a dictionary. |
167 |
167 |
168 @param item vulture item |
168 @param item vulture item |
169 @type vulture.Item |
169 @type vulture.Item |
170 @return item dictionary |
170 @return item dictionary |
171 @rtype dict |
171 @rtype dict |
172 """ |
172 """ |
176 "file": str(item.filename), |
176 "file": str(item.filename), |
177 "first_line": item.first_lineno, |
177 "first_line": item.first_lineno, |
178 "last_line": item.last_lineno, |
178 "last_line": item.last_lineno, |
179 "confidence": item.confidence, |
179 "confidence": item.confidence, |
180 } |
180 } |
181 |
181 |
182 def getResults(self): |
182 def getResults(self): |
183 """ |
183 """ |
184 Public method to get the scan results. |
184 Public method to get the scan results. |
185 |
185 |
186 @return scan results |
186 @return scan results |
187 @rtype dict |
187 @rtype dict |
188 """ |
188 """ |
189 return { |
189 return { |
190 "UnusedAttributes": |
190 "UnusedAttributes": [self.__item2Dict(i) for i in self.unused_attrs], |
191 [self.__item2Dict(i) for i in self.unused_attrs], |
191 "UnusedClasses": [self.__item2Dict(i) for i in self.unused_classes], |
192 "UnusedClasses": |
192 "UnusedFunctions": [self.__item2Dict(i) for i in self.unused_funcs], |
193 [self.__item2Dict(i) for i in self.unused_classes], |
193 "UnusedMethods": [self.__item2Dict(i) for i in self.unused_methods], |
194 "UnusedFunctions": |
194 "UnusedImports": [self.__item2Dict(i) for i in self.unused_imports], |
195 [self.__item2Dict(i) for i in self.unused_funcs], |
195 "UnusedProperties": [self.__item2Dict(i) for i in self.unused_props], |
196 "UnusedMethods": |
196 "UnusedVariables": [self.__item2Dict(i) for i in self.unused_vars], |
197 [self.__item2Dict(i) for i in self.unused_methods], |
|
198 "UnusedImports": |
|
199 [self.__item2Dict(i) for i in self.unused_imports], |
|
200 "UnusedProperties": |
|
201 [self.__item2Dict(i) for i in self.unused_props], |
|
202 "UnusedVariables": |
|
203 [self.__item2Dict(i) for i in self.unused_vars], |
|
204 } |
197 } |