Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheck.py

changeset 4231
0b38613388c9
parent 4207
a0c8a7f9839c
child 4236
8d4e498a7af8
equal deleted inserted replaced
4230:1117b60c1f9d 4231:0b38613388c9
9 """ 9 """
10 import ast 10 import ast
11 import re 11 import re
12 import sys 12 import sys
13 import traceback 13 import traceback
14 import multiprocessing
14 15
15 try: 16 try:
16 from pyflakes.checker import Checker 17 from pyflakes.checker import Checker
17 from pyflakes.messages import ImportStarUsed 18 from pyflakes.messages import ImportStarUsed
18 except ImportError: 19 except ImportError:
28 Initialize the service and return the entry point. 29 Initialize the service and return the entry point.
29 30
30 @return the entry point for the background client (function) 31 @return the entry point for the background client (function)
31 """ 32 """
32 return syntaxAndPyflakesCheck 33 return syntaxAndPyflakesCheck
34
35
36 def initBatchService():
37 """
38 Initialize the batch service and return the entry point.
39
40 @return the entry point for the background client (function)
41 """
42 return syntaxAndPyflakesBatchCheck
33 43
34 44
35 def normalizeCode(codestring): 45 def normalizeCode(codestring):
36 """ 46 """
37 Function to normalize the given code. 47 Function to normalize the given code.
76 return flags 86 return flags
77 87
78 88
79 def syntaxAndPyflakesCheck(filename, codestring, checkFlakes=True, 89 def syntaxAndPyflakesCheck(filename, codestring, checkFlakes=True,
80 ignoreStarImportWarnings=False): 90 ignoreStarImportWarnings=False):
91 """
92 Function to compile one Python source file to Python bytecode
93 and to perform a pyflakes check.
94
95 @param filename source filename (string)
96 @param codestring string containing the code to compile (string)
97 @keyparam checkFlakes flag indicating to do a pyflakes check (boolean)
98 @keyparam ignoreStarImportWarnings flag indicating to
99 ignore 'star import' warnings (boolean)
100 @return dictionary with the keys 'error' and 'warnings' which
101 hold a list containing details about the error/ warnings
102 (file name, line number, column, codestring (only at syntax
103 errors), the message, a list with arguments for the message)
104 """
105 return __syntaxAndPyflakesCheck(filename, codestring, checkFlakes,
106 ignoreStarImportWarnings)
107
108
109 def syntaxAndPyflakesBatchCheck(argumentsList, send, fx, cancelled):
110 """
111 Module function to check syntax for a batch of files.
112
113 @param argumentsList list of arguments tuples as given for
114 syntaxAndPyflakesCheck
115 @param send reference to send function (function)
116 @param fx registered service name (string)
117 @param cancelled reference to function checking for a cancellation
118 (function)
119 """
120 try:
121 NumberOfProcesses = multiprocessing.cpu_count()
122 if NumberOfProcesses >= 1:
123 NumberOfProcesses -= 1
124 except NotImplementedError:
125 NumberOfProcesses = 1
126
127 # Create queues
128 taskQueue = multiprocessing.Queue()
129 doneQueue = multiprocessing.Queue()
130
131 # Submit tasks (initially two time number of processes
132 initialTasks = 2 * NumberOfProcesses
133 for task in argumentsList[:initialTasks]:
134 taskQueue.put(task)
135
136 # Start worker processes
137 for i in range(NumberOfProcesses):
138 multiprocessing.Process(target=worker, args=(taskQueue, doneQueue))\
139 .start()
140
141 # Get and send results
142 endIndex = len(argumentsList) - initialTasks
143 for i in range(len(argumentsList)):
144 filename, result = doneQueue.get()
145 send(fx, filename, result)
146 if cancelled():
147 # just exit the loop ignoring the results of queued tasks
148 break
149 if i < endIndex:
150 taskQueue.put(argumentsList[i + initialTasks])
151
152 # Tell child processes to stop
153 for i in range(NumberOfProcesses):
154 taskQueue.put('STOP')
155
156
157 def worker(input, output):
158 """
159 Module function acting as the parallel worker for the style check.
160
161 @param input input queue (multiprocessing.Queue)
162 @param output output queue (multiprocessing.Queue)
163 """
164 for filename, args in iter(input.get, 'STOP'):
165 source, checkFlakes, ignoreStarImportWarnings = args
166 result = __syntaxAndPyflakesCheck(filename, source, checkFlakes,
167 ignoreStarImportWarnings)
168 output.put((filename, result))
169
170
171 def __syntaxAndPyflakesCheck(filename, codestring, checkFlakes=True,
172 ignoreStarImportWarnings=False):
81 """ 173 """
82 Function to compile one Python source file to Python bytecode 174 Function to compile one Python source file to Python bytecode
83 and to perform a pyflakes check. 175 and to perform a pyflakes check.
84 176
85 @param filename source filename (string) 177 @param filename source filename (string)

eric ide

mercurial