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