|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2014 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the syntax check for JavaScript. |
|
8 """ |
|
9 |
|
10 import queue |
|
11 import os |
|
12 import sys |
|
13 import multiprocessing |
|
14 |
|
15 |
|
16 def initService(): |
|
17 """ |
|
18 Initialize the service and return the entry point. |
|
19 |
|
20 @return the entry point for the background client (function) |
|
21 """ |
|
22 path = __file__ |
|
23 for _ in range(4): |
|
24 path = os.path.dirname(path) |
|
25 sys.path.insert(2, os.path.join(path, "ThirdParty", "Jasy")) |
|
26 return jsSyntaxCheck |
|
27 |
|
28 |
|
29 def initBatchService(): |
|
30 """ |
|
31 Initialize the batch service and return the entry point. |
|
32 |
|
33 @return the entry point for the background client (function) |
|
34 """ |
|
35 return jsSyntaxBatchCheck |
|
36 |
|
37 |
|
38 def normalizeCode(codestring): |
|
39 """ |
|
40 Function to normalize the given code. |
|
41 |
|
42 @param codestring code to be normalized (string) |
|
43 @return normalized code (string) |
|
44 """ |
|
45 codestring = codestring.replace("\r\n", "\n").replace("\r", "\n") |
|
46 |
|
47 if codestring and codestring[-1] != '\n': |
|
48 codestring += '\n' |
|
49 |
|
50 return codestring |
|
51 |
|
52 |
|
53 def jsSyntaxCheck(file, codestring): |
|
54 """ |
|
55 Function to check a Javascript source file for syntax errors. |
|
56 |
|
57 @param file source filename (string) |
|
58 @param codestring string containing the code to check (string) |
|
59 @return dictionary with the keys 'error' and 'warnings' which |
|
60 hold a list containing details about the error/ warnings |
|
61 (file name, line number, column, codestring (only at syntax |
|
62 errors), the message, a list with arguments for the message) |
|
63 """ |
|
64 return __jsSyntaxCheck(file, codestring) |
|
65 |
|
66 |
|
67 def jsSyntaxBatchCheck(argumentsList, send, fx, cancelled, maxProcesses=0): |
|
68 """ |
|
69 Module function to check syntax for a batch of files. |
|
70 |
|
71 @param argumentsList list of arguments tuples as given for jsSyntaxCheck |
|
72 @type list |
|
73 @param send reference to send function |
|
74 @type func |
|
75 @param fx registered service name |
|
76 @type str |
|
77 @param cancelled reference to function checking for a cancellation |
|
78 @type func |
|
79 @param maxProcesses number of processes to be used |
|
80 @type int |
|
81 """ |
|
82 if maxProcesses == 0: |
|
83 # determine based on CPU count |
|
84 try: |
|
85 NumberOfProcesses = multiprocessing.cpu_count() |
|
86 if NumberOfProcesses >= 1: |
|
87 NumberOfProcesses -= 1 |
|
88 except NotImplementedError: |
|
89 NumberOfProcesses = 1 |
|
90 else: |
|
91 NumberOfProcesses = maxProcesses |
|
92 |
|
93 # Create queues |
|
94 taskQueue = multiprocessing.Queue() |
|
95 doneQueue = multiprocessing.Queue() |
|
96 |
|
97 # Submit tasks (initially two time number of processes |
|
98 initialTasks = 2 * NumberOfProcesses |
|
99 for task in argumentsList[:initialTasks]: |
|
100 taskQueue.put(task) |
|
101 |
|
102 # Start worker processes |
|
103 for _ in range(NumberOfProcesses): |
|
104 multiprocessing.Process( |
|
105 target=worker, args=(taskQueue, doneQueue) |
|
106 ).start() |
|
107 |
|
108 # Get and send results |
|
109 endIndex = len(argumentsList) - initialTasks |
|
110 for i in range(len(argumentsList)): |
|
111 resultSent = False |
|
112 wasCancelled = False |
|
113 |
|
114 while not resultSent: |
|
115 try: |
|
116 # get result (waiting max. 3 seconds and send it to frontend |
|
117 filename, result = doneQueue.get() |
|
118 send(fx, filename, result) |
|
119 resultSent = True |
|
120 except queue.Empty: |
|
121 # ignore empty queue, just carry on |
|
122 if cancelled(): |
|
123 wasCancelled = True |
|
124 break |
|
125 |
|
126 if wasCancelled or cancelled(): |
|
127 # just exit the loop ignoring the results of queued tasks |
|
128 break |
|
129 |
|
130 if i < endIndex: |
|
131 taskQueue.put(argumentsList[i + initialTasks]) |
|
132 |
|
133 # Tell child processes to stop |
|
134 for _ in range(NumberOfProcesses): |
|
135 taskQueue.put('STOP') |
|
136 |
|
137 |
|
138 def worker(inputQueue, outputQueue): |
|
139 """ |
|
140 Module function acting as the parallel worker for the syntax check. |
|
141 |
|
142 @param inputQueue input queue (multiprocessing.Queue) |
|
143 @param outputQueue output queue (multiprocessing.Queue) |
|
144 """ |
|
145 for filename, args in iter(inputQueue.get, 'STOP'): |
|
146 source = args[0] |
|
147 result = __jsSyntaxCheck(filename, source) |
|
148 outputQueue.put((filename, result)) |
|
149 |
|
150 |
|
151 def __jsSyntaxCheck(file, codestring): |
|
152 """ |
|
153 Function to check a Javascript source file for syntax errors. |
|
154 |
|
155 @param file source filename (string) |
|
156 @param codestring string containing the code to check (string) |
|
157 @return dictionary with the keys 'error' and 'warnings' which |
|
158 hold a list containing details about the error/ warnings |
|
159 (file name, line number, column, codestring (only at syntax |
|
160 errors), the message, a list with arguments for the message) |
|
161 """ |
|
162 import jasy.script.parse.Parser as jsParser |
|
163 import jasy.script.tokenize.Tokenizer as jsTokenizer |
|
164 |
|
165 codestring = normalizeCode(codestring) |
|
166 |
|
167 try: |
|
168 jsParser.parse(codestring, file) |
|
169 except (jsParser.SyntaxError, jsTokenizer.ParseError) as exc: |
|
170 details = exc.args[0] |
|
171 error, details = details.splitlines() |
|
172 fn, line = details.strip().rsplit(":", 1) |
|
173 error = error.split(":", 1)[1].strip() |
|
174 |
|
175 cline = min(len(codestring.splitlines()), int(line)) - 1 |
|
176 code = codestring.splitlines()[cline] |
|
177 return [{'error': (fn, int(line), 0, code, error)}] |
|
178 except IndexError: |
|
179 error = "Incomplete source file" |
|
180 splittedCode = codestring.splitlines() |
|
181 return [{'error': (file, len(splittedCode) + 1, len(splittedCode[-1]), |
|
182 splittedCode[-1], error)}] |
|
183 |
|
184 return [{}] |