Showing
5 changed files
with
66 additions
and
32 deletions
.gitignore
0 → 100644
Makefile
0 → 100644
1 | +all: | ||
2 | + | ||
3 | +test: clean test-with-shell test-without-shell | ||
4 | + | ||
5 | +clean: | ||
6 | + rm -rf with-shell without-shell | ||
7 | + | ||
8 | +test-with-shell: | ||
9 | + mkdir with-shell | ||
10 | + cp test.tex pytex.sty Makefile with-shell/ | ||
11 | + cp pytex.py with-shell/pytex | ||
12 | + chmod +x with-shell/pytex | ||
13 | + PATH=.:$$PATH make -C with-shell run-test-with-shell | ||
14 | + | ||
15 | +run-test-with-shell: | ||
16 | + pdflatex -shell-escape test | ||
17 | + | ||
18 | +test-without-shell: | ||
19 | + mkdir without-shell | ||
20 | + cp test.tex pytex.sty Makefile without-shell/ | ||
21 | + cp pytex.py without-shell/pytex | ||
22 | + chmod +x without-shell/pytex | ||
23 | + PATH=.:$$PATH make -C without-shell run-test-without-shell | ||
24 | + | ||
25 | +run-test-without-shell: | ||
26 | + pdflatex test | ||
27 | + pytex test | ||
28 | + pdflatex test |
... | @@ -9,6 +9,10 @@ try : | ... | @@ -9,6 +9,10 @@ try : |
9 | except : | 9 | except : |
10 | pygments = None | 10 | pygments = None |
11 | 11 | ||
12 | +def log (message) : | ||
13 | + sys.stdout.write(message) | ||
14 | + sys.stdout.flush() | ||
15 | + | ||
12 | class Interpreter (object) : | 16 | class Interpreter (object) : |
13 | def __init__ (self, jobname) : | 17 | def __init__ (self, jobname) : |
14 | self._t = [] | 18 | self._t = [] |
... | @@ -163,7 +167,7 @@ class Handler (SocketServer.BaseRequestHandler) : | ... | @@ -163,7 +167,7 @@ class Handler (SocketServer.BaseRequestHandler) : |
163 | self.client_address) | 167 | self.client_address) |
164 | return | 168 | return |
165 | sock.sendto(repr({"status" : "ok", | 169 | sock.sendto(repr({"status" : "ok", |
166 | - "message" : "file proceeded successfully"}), | 170 | + "message" : "<%(method)s %(path)s>" % req}), |
167 | self.client_address) | 171 | self.client_address) |
168 | 172 | ||
169 | class Server (multiprocessing.Process) : | 173 | class Server (multiprocessing.Process) : |
... | @@ -172,14 +176,14 @@ class Server (multiprocessing.Process) : | ... | @@ -172,14 +176,14 @@ class Server (multiprocessing.Process) : |
172 | child = cls(port, jobname) | 176 | child = cls(port, jobname) |
173 | child.start() | 177 | child.start() |
174 | multiprocessing.process._current_process._children.discard(child) | 178 | multiprocessing.process._current_process._children.discard(child) |
175 | - print("<server started at %s>" % child.pid) | 179 | + log("<server started at %s>" % child.pid) |
176 | def __init__ (self, port, jobname) : | 180 | def __init__ (self, port, jobname) : |
177 | multiprocessing.Process.__init__(self) | 181 | multiprocessing.Process.__init__(self) |
178 | self.port = port | 182 | self.port = port |
179 | self.jobname = jobname | 183 | self.jobname = jobname |
180 | def run (self) : | 184 | def run (self) : |
181 | self.server = SocketServer.UDPServer(("", self.port), Handler) | 185 | self.server = SocketServer.UDPServer(("", self.port), Handler) |
182 | - self.py = Interpreter(self.jobname) | 186 | + self.server.py = Interpreter(self.jobname) |
183 | self.server.serve_forever() | 187 | self.server.serve_forever() |
184 | @classmethod | 188 | @classmethod |
185 | def quit (cls, port) : | 189 | def quit (cls, port) : |
... | @@ -228,56 +232,61 @@ def main () : | ... | @@ -228,56 +232,61 @@ def main () : |
228 | args = parser.parse_args([a.strip("{}") for a in sys.argv[1:]]) | 232 | args = parser.parse_args([a.strip("{}") for a in sys.argv[1:]]) |
229 | if args.listen : | 233 | if args.listen : |
230 | if len(args.args) != 1 : | 234 | if len(args.args) != 1 : |
231 | - print("usage: pytex [-b BASE] --listen PORT JOBNAME") | 235 | + log("usage: pytex [-b BASE] --listen PORT JOBNAME") |
232 | sys.exit(1) | 236 | sys.exit(1) |
233 | Server.daemonize(args.listen, *args.args) | 237 | Server.daemonize(args.listen, *args.args) |
234 | return | 238 | return |
235 | elif args.call : | 239 | elif args.call : |
236 | if len(args.args) != 2 : | 240 | if len(args.args) != 2 : |
237 | - print("usage: pytex [-b BASE] --call PORT MODE FILE") | 241 | + log("usage: pytex [-b BASE] --call PORT MODE FILE") |
238 | sys.exit(1) | 242 | sys.exit(1) |
239 | elif args.args[0] not in ("eval", "exec") : | 243 | elif args.args[0] not in ("eval", "exec") : |
240 | - print("pytex: expected 'exec' or 'eval', but got %r" % args.args[0]) | 244 | + log("pytex: expected 'exec' or 'eval', but got %r" % args.args[0]) |
241 | sys.exit(1) | 245 | sys.exit(1) |
242 | - print Server.process(args.call, *args.args) | 246 | + resp = Server.process(args.call, *args.args) |
247 | + try : | ||
248 | + resp = ast.literal_eval(resp) | ||
249 | + except : | ||
250 | + resp = {"message": "<invalid answer from server>"} | ||
251 | + log(resp.get("message", "<invalid answer from server>")) | ||
243 | return | 252 | return |
244 | elif args.shutdown : | 253 | elif args.shutdown : |
245 | if len(args.args) > 0 : | 254 | if len(args.args) > 0 : |
246 | - print("usage: pytex [-b BASE] --shutdown PORT") | 255 | + log("usage: pytex [-b BASE] --shutdown PORT") |
247 | sys.exit(1) | 256 | sys.exit(1) |
248 | try : | 257 | try : |
249 | resp = Server.quit(args.shutdown) | 258 | resp = Server.quit(args.shutdown) |
250 | except socket.timeout : | 259 | except socket.timeout : |
251 | - print("<could not reach server to shutdown>") | 260 | + log("<could not reach server to shutdown>") |
252 | sys.exit(1) | 261 | sys.exit(1) |
253 | try : | 262 | try : |
254 | resp = ast.literal_eval(resp) | 263 | resp = ast.literal_eval(resp) |
255 | pid = int(resp["pid"]) | 264 | pid = int(resp["pid"]) |
256 | except : | 265 | except : |
257 | - print("<invalid answer from server>") | 266 | + log("<invalid answer from server>") |
258 | sys.exit(1) | 267 | sys.exit(1) |
259 | child = psutil.Process(pid) | 268 | child = psutil.Process(pid) |
260 | try : | 269 | try : |
261 | child.wait(1) | 270 | child.wait(1) |
262 | - print("<server at %s has sutdown>" % child.pid) | 271 | + log("<server at %s has shutdown>" % child.pid) |
263 | return | 272 | return |
264 | except : | 273 | except : |
265 | child.terminate() | 274 | child.terminate() |
266 | try : | 275 | try : |
267 | child.wait(1) | 276 | child.wait(1) |
268 | - print("<server at %s terminated>" % child.pid) | 277 | + log("<server at %s terminated>" % child.pid) |
269 | return | 278 | return |
270 | except : | 279 | except : |
271 | child.kill() | 280 | child.kill() |
272 | - print("<server at %s killed>" % child.pid) | 281 | + log("<server at %s killed>" % child.pid) |
273 | return | 282 | return |
274 | elif len(args.args) != 1 : | 283 | elif len(args.args) != 1 : |
275 | - print("usage: pytex [-b BASE] [--clean] JOBNAME") | 284 | + log("usage: pytex [-b BASE] [--clean] JOBNAME") |
276 | sys.exit(1) | 285 | sys.exit(1) |
277 | try : | 286 | try : |
278 | jobname, jobs = getjobs(args.args[0]) | 287 | jobname, jobs = getjobs(args.args[0]) |
279 | except : | 288 | except : |
280 | - print("pytex: invalid file %r" % args.args[0]) | 289 | + log("pytex: invalid file %r" % args.args[0]) |
281 | sys.exit(2) | 290 | sys.exit(2) |
282 | if args.base is None : | 291 | if args.base is None : |
283 | if jobs : | 292 | if jobs : |
... | @@ -303,14 +312,14 @@ def main () : | ... | @@ -303,14 +312,14 @@ def main () : |
303 | py = Interpreter(jobname) | 312 | py = Interpreter(jobname) |
304 | for mode, path in jobs : | 313 | for mode, path in jobs : |
305 | if not os.path.exists(path) : | 314 | if not os.path.exists(path) : |
306 | - print("not found: %s" % path) | 315 | + log("not found: %s" % path) |
307 | elif mode in ("eval", "exec") : | 316 | elif mode in ("eval", "exec") : |
308 | - print("%s: %s" % (mode, path)) | 317 | + log("%s: %s" % (mode, path)) |
309 | py(path, mode) | 318 | py(path, mode) |
310 | elif mode == "code" : | 319 | elif mode == "code" : |
311 | - print("skip: %s" % path) | 320 | + log("skip: %s" % path) |
312 | else : | 321 | else : |
313 | - print("pytex: invalid mode %r in file %r" % (mode, jobname + ".pytex")) | 322 | + log("pytex: invalid mode %r in file %r" % (mode, jobname + ".pytex")) |
314 | 323 | ||
315 | if __name__ == "__main__" : | 324 | if __name__ == "__main__" : |
316 | main() | 325 | main() | ... | ... |
1 | \ProvidesPackage{pytex} | 1 | \ProvidesPackage{pytex} |
2 | 2 | ||
3 | -\RequirePackage{pgfopts} | ||
4 | \RequirePackage{sverb} | 3 | \RequirePackage{sverb} |
5 | \RequirePackage{fancyvrb} | 4 | \RequirePackage{fancyvrb} |
6 | 5 | ||
... | @@ -12,21 +11,15 @@ | ... | @@ -12,21 +11,15 @@ |
12 | \edef\py@port{12345} | 11 | \edef\py@port{12345} |
13 | \edef\py@base{\jobname.pytmp} | 12 | \edef\py@base{\jobname.pytmp} |
14 | 13 | ||
15 | -\pgfkeys{ | 14 | +\immediate\write18{pytex --clean {\jobname}} |
16 | - /pytex/.cd, | ||
17 | - base/.store in=\py@base, | ||
18 | - port/.store in=\py@port, | ||
19 | -} | ||
20 | - | ||
21 | -\ProcessPgfOptions{/pytex} | ||
22 | - | ||
23 | -\immediate\write18{pytex -b {\py@base} --listen {\py@port} {\jobname}} | ||
24 | -\AtEndDocument{\immediate\write18{pytex -b {\py@base} --quit {\py@port}}} | ||
25 | 15 | ||
26 | \IfFileExists{\py@base/\jobname} | 16 | \IfFileExists{\py@base/\jobname} |
27 | {\global\edef\py@base{\py@base/}} | 17 | {\global\edef\py@base{\py@base/}} |
28 | {\global\edef\py@base{\py@base.}} | 18 | {\global\edef\py@base{\py@base.}} |
29 | 19 | ||
20 | +\immediate\write18{pytex -b {\py@base} --listen {\py@port} {\jobname}} | ||
21 | +\AtEndDocument{\immediate\write18{pytex -b {\py@base} --shutdown {\py@port}}} | ||
22 | + | ||
30 | %% | 23 | %% |
31 | %% | 24 | %% |
32 | 25 | ... | ... |
... | @@ -3,6 +3,8 @@ | ... | @@ -3,6 +3,8 @@ |
3 | \usepackage{pytex} | 3 | \usepackage{pytex} |
4 | \parindent=0pt | 4 | \parindent=0pt |
5 | 5 | ||
6 | +\def\hello{NOT DEFINED} | ||
7 | + | ||
6 | \begin{document} | 8 | \begin{document} |
7 | 9 | ||
8 | ***\pyv{" ".join(s.capitalize() for s in "hello world!".split())}*** | 10 | ***\pyv{" ".join(s.capitalize() for s in "hello world!".split())}*** |
... | @@ -38,6 +40,4 @@ def fact (n) : | ... | @@ -38,6 +40,4 @@ def fact (n) : |
38 | return f | 40 | return f |
39 | \end{pycode} | 41 | \end{pycode} |
40 | 42 | ||
41 | -\IfFileExists{pytex.tmp}{found}{NO}. | ||
42 | - | ||
43 | \end{document} | 43 | \end{document} | ... | ... |
-
Please register or login to post a comment