Mercurial > hgbook
annotate en/examples/run-example @ 47:6f37e6a7d8cd
Get Emacs to figure out what syntax highlighting to use for examples.
author | Bryan O'Sullivan <bos@serpentine.com> |
---|---|
date | Sun, 23 Jul 2006 23:38:41 -0700 |
parents | 6b7b0339e7d6 |
children | a2f0b010d6e3 |
rev | line source |
---|---|
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
1 #!/usr/bin/python |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
2 # |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
3 # This program takes something that resembles a shell script and runs |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
4 # it, spitting input (commands from the script) and output into text |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
5 # files, for use in examples. |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
6 |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
7 import cStringIO |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
8 import os |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
9 import pty |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
10 import re |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
11 import shutil |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
12 import signal |
36
5cee64874312
Require examples to be executable, so it's easier to see them with "ls".
Bryan O'Sullivan <bos@serpentine.com>
parents:
24
diff
changeset
|
13 import stat |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
14 import sys |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
15 import tempfile |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
16 import time |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
17 |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
18 def tex_escape(s): |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
19 if '\\' in s: |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
20 s = s.replace('\\', '\\\\') |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
21 if '{' in s: |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
22 s = s.replace('{', '\\{') |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
23 if '}' in s: |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
24 s = s.replace('}', '\\}') |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
25 return s |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
26 |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
27 class example: |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
28 shell = '/bin/bash' |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
29 prompt = '__run_example_prompt__\n' |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
30 pi_re = re.compile('#\$\s*(name):\s*(.*)$') |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
31 |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
32 def __init__(self, name): |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
33 self.name = name |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
34 |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
35 def parse(self): |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
36 '''yield each hunk of input from the file.''' |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
37 fp = open(self.name) |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
38 cfp = cStringIO.StringIO() |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
39 for line in fp: |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
40 cfp.write(line) |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
41 if not line.rstrip().endswith('\\'): |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
42 yield cfp.getvalue() |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
43 cfp.seek(0) |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
44 cfp.truncate() |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
45 |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
46 def status(self, s): |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
47 sys.stdout.write(s) |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
48 if not s.endswith('\n'): |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
49 sys.stdout.flush() |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
50 |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
51 def send(self, s): |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
52 self.cfp.write(s) |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
53 self.cfp.flush() |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
54 |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
55 def receive(self): |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
56 out = cStringIO.StringIO() |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
57 while True: |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
58 s = self.cfp.readline().replace('\r\n', '\n') |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
59 if not s or s == self.prompt: |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
60 break |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
61 out.write(s) |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
62 return out.getvalue() |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
63 |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
64 def sendreceive(self, s): |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
65 self.send(s) |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
66 r = self.receive() |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
67 if r.startswith(s): |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
68 r = r[len(s):] |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
69 return r |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
70 |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
71 def run(self): |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
72 ofp = None |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
73 basename = os.path.basename(self.name) |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
74 self.status('running %s ' % basename) |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
75 tmpdir = tempfile.mkdtemp(prefix=basename) |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
76 rcfile = os.path.join(tmpdir, '.bashrc') |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
77 rcfp = open(rcfile, 'w') |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
78 print >> rcfp, 'PS1="%s"' % self.prompt |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
79 print >> rcfp, 'unset HISTFILE' |
19
187702df428b
Piles of new content for MQ chapter - cookbook stuff.
Bryan O'Sullivan <bos@serpentine.com>
parents:
6
diff
changeset
|
80 print >> rcfp, 'export EXAMPLE_DIR="%s"' % os.getcwd() |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
81 print >> rcfp, 'export LANG=C' |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
82 print >> rcfp, 'export LC_ALL=C' |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
83 print >> rcfp, 'export TZ=GMT' |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
84 print >> rcfp, 'export HGRC="%s/.hgrc"' % tmpdir |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
85 print >> rcfp, 'export HGRCPATH=$HGRC' |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
86 print >> rcfp, 'cd %s' % tmpdir |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
87 rcfp.close() |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
88 pid, fd = pty.fork() |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
89 if pid == 0: |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
90 #os.execl(self.shell, self.shell) |
22
5ad16196cef4
Don't read system rc files
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6
diff
changeset
|
91 os.system('/bin/bash --noediting --noprofile --norc') |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
92 sys.exit(0) |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
93 self.cfp = os.fdopen(fd, 'w+') |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
94 try: |
22
5ad16196cef4
Don't read system rc files
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6
diff
changeset
|
95 # setup env and prompt |
5ad16196cef4
Don't read system rc files
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6
diff
changeset
|
96 self.sendreceive('source %s\n\n' % rcfile) |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
97 for hunk in self.parse(): |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
98 # is this line a processing instruction? |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
99 m = self.pi_re.match(hunk) |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
100 if m: |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
101 pi, rest = m.groups() |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
102 if pi == 'name': |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
103 self.status('.') |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
104 out = rest |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
105 assert os.sep not in out |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
106 if out: |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
107 ofp = open('%s.%s.out' % (self.name, out), 'w') |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
108 else: |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
109 ofp = None |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
110 elif hunk.strip(): |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
111 # it's something we should execute |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
112 output = self.sendreceive(hunk) |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
113 if not ofp: |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
114 continue |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
115 # first, print the command we ran |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
116 if not hunk.startswith('#'): |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
117 nl = hunk.endswith('\n') |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
118 hunk = ('$ \\textbf{%s}' % |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
119 tex_escape(hunk.rstrip('\n'))) |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
120 if nl: hunk += '\n' |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
121 ofp.write(hunk) |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
122 # then its output |
19
187702df428b
Piles of new content for MQ chapter - cookbook stuff.
Bryan O'Sullivan <bos@serpentine.com>
parents:
6
diff
changeset
|
123 ofp.write(tex_escape(output)) |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
124 self.status('\n') |
45
6b7b0339e7d6
Don't rerun examples unnecessarily.
Bryan O'Sullivan <bos@serpentine.com>
parents:
36
diff
changeset
|
125 open(self.name + '.run', 'w') |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
126 finally: |
6
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
127 try: |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
128 output = self.sendreceive('exit\n') |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
129 if ofp: |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
130 ofp.write(output) |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
131 self.cfp.close() |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
132 except IOError: |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
133 pass |
69d90ab9fd80
Really run example command sequences under a single shell.
Bryan O'Sullivan <bos@serpentine.com>
parents:
4
diff
changeset
|
134 os.kill(pid, signal.SIGTERM) |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
135 os.wait() |
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
136 shutil.rmtree(tmpdir) |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
137 |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
138 def main(path='.'): |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
139 args = sys.argv[1:] |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
140 if args: |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
141 for a in args: |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
142 example(a).run() |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
143 return |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
144 for name in os.listdir(path): |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
145 if name == 'run-example' or name.startswith('.'): continue |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
146 if name.endswith('.out') or name.endswith('~'): continue |
45
6b7b0339e7d6
Don't rerun examples unnecessarily.
Bryan O'Sullivan <bos@serpentine.com>
parents:
36
diff
changeset
|
147 if name.endswith('.run'): continue |
19
187702df428b
Piles of new content for MQ chapter - cookbook stuff.
Bryan O'Sullivan <bos@serpentine.com>
parents:
6
diff
changeset
|
148 pathname = os.path.join(path, name) |
36
5cee64874312
Require examples to be executable, so it's easier to see them with "ls".
Bryan O'Sullivan <bos@serpentine.com>
parents:
24
diff
changeset
|
149 st = os.lstat(pathname) |
5cee64874312
Require examples to be executable, so it's easier to see them with "ls".
Bryan O'Sullivan <bos@serpentine.com>
parents:
24
diff
changeset
|
150 if stat.S_ISREG(st.st_mode) and st.st_mode & 0111: |
19
187702df428b
Piles of new content for MQ chapter - cookbook stuff.
Bryan O'Sullivan <bos@serpentine.com>
parents:
6
diff
changeset
|
151 example(pathname).run() |
4
33a2e7b9978d
Make it possible to include example input and output from real programs.
Bryan O'Sullivan <bos@serpentine.com>
parents:
3
diff
changeset
|
152 print >> open(os.path.join(path, '.run'), 'w'), time.asctime() |
3
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
153 |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
154 if __name__ == '__main__': |
906d9021f9e5
Making progress on autogenerated example output.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
155 main() |