hgbook
annotate en/examples/run-example @ 3:906d9021f9e5
Making progress on autogenerated example output.
author | Bryan O'Sullivan <bos@serpentine.com> |
---|---|
date | Sat Jun 24 17:42:40 2006 -0700 (2006-06-24) |
parents | |
children | 33a2e7b9978d |
rev | line source |
---|---|
bos@3 | 1 #!/usr/bin/python |
bos@3 | 2 |
bos@3 | 3 import cStringIO |
bos@3 | 4 import os |
bos@3 | 5 import pty |
bos@3 | 6 import re |
bos@3 | 7 import sys |
bos@3 | 8 |
bos@3 | 9 class example: |
bos@3 | 10 def __init__(self, name): |
bos@3 | 11 self.name = name |
bos@3 | 12 |
bos@3 | 13 def parse(self): |
bos@3 | 14 fp = open(self.name) |
bos@3 | 15 cfp = cStringIO.StringIO() |
bos@3 | 16 for line in fp: |
bos@3 | 17 cfp.write(line) |
bos@3 | 18 if not line.rstrip().endswith('\\'): |
bos@3 | 19 yield cfp.getvalue() |
bos@3 | 20 cfp.seek(0) |
bos@3 | 21 cfp.truncate() |
bos@3 | 22 |
bos@3 | 23 name_re = re.compile('#\s*name:\s*(.*)$') |
bos@3 | 24 |
bos@3 | 25 def status(self, s): |
bos@3 | 26 sys.stdout.write(s) |
bos@3 | 27 if not s.endswith('\n'): |
bos@3 | 28 sys.stdout.flush() |
bos@3 | 29 |
bos@3 | 30 def run(self): |
bos@3 | 31 ofp = None |
bos@3 | 32 self.status('running %s ' % os.path.basename(self.name)) |
bos@3 | 33 for hunk in self.parse(): |
bos@3 | 34 m = self.name_re.match(hunk) |
bos@3 | 35 if m: |
bos@3 | 36 self.status('.') |
bos@3 | 37 out = m.group(1) |
bos@3 | 38 assert os.sep not in out |
bos@3 | 39 if out: |
bos@3 | 40 ofp = open('%s.%s.out' % (self.name, out), 'w') |
bos@3 | 41 else: |
bos@3 | 42 ofp = None |
bos@3 | 43 elif ofp: ofp.write(hunk) |
bos@3 | 44 self.status('\n') |
bos@3 | 45 |
bos@3 | 46 def main(path='.'): |
bos@3 | 47 args = sys.argv[1:] |
bos@3 | 48 if args: |
bos@3 | 49 for a in args: |
bos@3 | 50 example(a).run() |
bos@3 | 51 return |
bos@3 | 52 for name in os.listdir(path): |
bos@3 | 53 if name == 'run-example' or name.startswith('.'): continue |
bos@3 | 54 if name.endswith('.out') or name.endswith('~'): continue |
bos@3 | 55 example(os.path.join(path, name)).run() |
bos@3 | 56 |
bos@3 | 57 if __name__ == '__main__': |
bos@3 | 58 main() |