12 |
12 |
13 |
13 |
14 def initService(): |
14 def initService(): |
15 """ |
15 """ |
16 Initialize the service and return the entry point. |
16 Initialize the service and return the entry point. |
17 |
17 |
18 @return the entry point for the background client (function) |
18 @return the entry point for the background client (function) |
19 """ |
19 """ |
20 return maintainabilityIndex |
20 return maintainabilityIndex |
21 |
21 |
22 |
22 |
23 def initBatchService(): |
23 def initBatchService(): |
24 """ |
24 """ |
25 Initialize the batch service and return the entry point. |
25 Initialize the batch service and return the entry point. |
26 |
26 |
27 @return the entry point for the background client (function) |
27 @return the entry point for the background client (function) |
28 """ |
28 """ |
29 return batchMaintainabilityIndex |
29 return batchMaintainabilityIndex |
30 |
30 |
31 |
31 |
32 def maintainabilityIndex(file, text=""): |
32 def maintainabilityIndex(file, text=""): |
33 """ |
33 """ |
34 Private function to calculate the maintainability index of one file. |
34 Private function to calculate the maintainability index of one file. |
35 |
35 |
36 @param file source filename |
36 @param file source filename |
37 @type str |
37 @type str |
38 @param text source text |
38 @param text source text |
39 @type str |
39 @type str |
40 @return tuple containing the result dictionary |
40 @return tuple containing the result dictionary |
41 @rtype (tuple of dict) |
41 @rtype (tuple of dict) |
42 """ |
42 """ |
43 return __maintainabilityIndex(file, text) |
43 return __maintainabilityIndex(file, text) |
44 |
44 |
45 |
45 |
46 def batchMaintainabilityIndex(argumentsList, send, fx, cancelled, |
46 def batchMaintainabilityIndex(argumentsList, send, fx, cancelled, maxProcesses=0): |
47 maxProcesses=0): |
|
48 """ |
47 """ |
49 Module function to calculate the maintainability index for a batch of |
48 Module function to calculate the maintainability index for a batch of |
50 files. |
49 files. |
51 |
50 |
52 @param argumentsList list of arguments tuples as given for |
51 @param argumentsList list of arguments tuples as given for |
53 maintainabilityIndex |
52 maintainabilityIndex |
54 @type list |
53 @type list |
55 @param send reference to send function |
54 @param send reference to send function |
56 @type function |
55 @type function |
81 for task in argumentsList[:initialTasks]: |
80 for task in argumentsList[:initialTasks]: |
82 taskQueue.put(task) |
81 taskQueue.put(task) |
83 |
82 |
84 # Start worker processes |
83 # Start worker processes |
85 workers = [ |
84 workers = [ |
86 multiprocessing.Process( |
85 multiprocessing.Process(target=workerTask, args=(taskQueue, doneQueue)) |
87 target=workerTask, args=(taskQueue, doneQueue) |
86 for _ in range(NumberOfProcesses) |
88 ) for _ in range(NumberOfProcesses) |
|
89 ] |
87 ] |
90 for worker in workers: |
88 for worker in workers: |
91 worker.start() |
89 worker.start() |
92 |
90 |
93 # Get and send results |
91 # Get and send results |
94 endIndex = len(argumentsList) - initialTasks |
92 endIndex = len(argumentsList) - initialTasks |
95 for i in range(len(argumentsList)): |
93 for i in range(len(argumentsList)): |
96 resultSent = False |
94 resultSent = False |
97 wasCancelled = False |
95 wasCancelled = False |
98 |
96 |
99 while not resultSent: |
97 while not resultSent: |
100 try: |
98 try: |
101 # get result (waiting max. 3 seconds and send it to frontend |
99 # get result (waiting max. 3 seconds and send it to frontend |
102 filename, result = doneQueue.get() |
100 filename, result = doneQueue.get() |
103 send(fx, filename, result) |
101 send(fx, filename, result) |
105 except queue.Empty: |
103 except queue.Empty: |
106 # ignore empty queue, just carry on |
104 # ignore empty queue, just carry on |
107 if cancelled(): |
105 if cancelled(): |
108 wasCancelled = True |
106 wasCancelled = True |
109 break |
107 break |
110 |
108 |
111 if wasCancelled or cancelled(): |
109 if wasCancelled or cancelled(): |
112 # just exit the loop ignoring the results of queued tasks |
110 # just exit the loop ignoring the results of queued tasks |
113 break |
111 break |
114 |
112 |
115 if i < endIndex: |
113 if i < endIndex: |
116 taskQueue.put(argumentsList[i + initialTasks]) |
114 taskQueue.put(argumentsList[i + initialTasks]) |
117 |
115 |
118 # Tell child processes to stop |
116 # Tell child processes to stop |
119 for _ in range(NumberOfProcesses): |
117 for _ in range(NumberOfProcesses): |
120 taskQueue.put('STOP') |
118 taskQueue.put("STOP") |
121 |
119 |
122 for worker in workers: |
120 for worker in workers: |
123 worker.join() |
121 worker.join() |
124 worker.close() |
122 worker.close() |
125 |
123 |
126 |
124 |
127 def workerTask(inputQueue, outputQueue): |
125 def workerTask(inputQueue, outputQueue): |
128 """ |
126 """ |
129 Module function acting as the parallel worker for the maintainability |
127 Module function acting as the parallel worker for the maintainability |
130 index calculation. |
128 index calculation. |
131 |
129 |
132 @param inputQueue input queue |
130 @param inputQueue input queue |
133 @type multiprocessing.Queue |
131 @type multiprocessing.Queue |
134 @param outputQueue output queue |
132 @param outputQueue output queue |
135 @type multiprocessing.Queue |
133 @type multiprocessing.Queue |
136 """ |
134 """ |
137 for filename, source in iter(inputQueue.get, 'STOP'): |
135 for filename, source in iter(inputQueue.get, "STOP"): |
138 result = __maintainabilityIndex(filename, source) |
136 result = __maintainabilityIndex(filename, source) |
139 outputQueue.put((filename, result)) |
137 outputQueue.put((filename, result)) |
140 |
138 |
141 |
139 |
142 def __maintainabilityIndex(file, text=""): |
140 def __maintainabilityIndex(file, text=""): |
143 """ |
141 """ |
144 Private function to calculate the maintainability index for one Python |
142 Private function to calculate the maintainability index for one Python |
145 file. |
143 file. |
146 |
144 |
147 @param file source filename |
145 @param file source filename |
148 @type str |
146 @type str |
149 @param text source text |
147 @param text source text |
150 @type str |
148 @type str |
151 @return tuple containing the result dictionary |
149 @return tuple containing the result dictionary |
152 @rtype (tuple of dict) |
150 @rtype (tuple of dict) |
153 """ |
151 """ |
154 from radon.metrics import mi_visit, mi_rank |
152 from radon.metrics import mi_visit, mi_rank |
155 |
153 |
156 try: |
154 try: |
157 mi = mi_visit(text, True) |
155 mi = mi_visit(text, True) |
158 rank = mi_rank(mi) |
156 rank = mi_rank(mi) |
159 res = {"mi": mi, "rank": rank} |
157 res = {"mi": mi, "rank": rank} |
160 except Exception as err: |
158 except Exception as err: |
161 res = {"error": str(err)} |
159 res = {"error": str(err)} |
162 return (res, ) |
160 return (res,) |