comparison en/examples/run-example @ 644:d8913b7869b5

Add --keep option to run-example
author Bryan O'Sullivan <bos@serpentine.com>
date Thu, 29 Jan 2009 22:11:10 -0800
parents 1a55ba6ceca1
children 0d5935744f87
comparison
equal deleted inserted replaced
643:f5ab40759789 644:d8913b7869b5
59 ps2 = '__run_example_ps2__ ' 59 ps2 = '__run_example_ps2__ '
60 pi_re = re.compile(r'#\$\s*(name|ignore):\s*(.*)$') 60 pi_re = re.compile(r'#\$\s*(name|ignore):\s*(.*)$')
61 61
62 timeout = 10 62 timeout = 10
63 63
64 def __init__(self, name, verbose): 64 def __init__(self, name, verbose, keep_change):
65 self.name = name 65 self.name = name
66 self.verbose = verbose 66 self.verbose = verbose
67 self.keep_change = keep_change
67 self.poll = select.poll() 68 self.poll = select.poll()
68 69
69 def parse(self): 70 def parse(self):
70 '''yield each hunk of input from the file.''' 71 '''yield each hunk of input from the file.'''
71 fp = open(self.name) 72 fp = open(self.name)
318 if matchfp(oldfp, errfp): 319 if matchfp(oldfp, errfp):
319 os.unlink(errname) 320 os.unlink(errname)
320 return False 321 return False
321 else: 322 else:
322 print >> sys.stderr, '\nOutput of %s has changed!' % base 323 print >> sys.stderr, '\nOutput of %s has changed!' % base
323 os.system('diff -u %s %s 1>&2' % (oldname, errname)) 324 if self.keep_change:
325 os.rename(errname, oldname)
326 return False
327 else:
328 os.system('diff -u %s %s 1>&2' % (oldname, errname))
324 return True 329 return True
325 330
326 def print_help(exit, msg=None): 331 def print_help(exit, msg=None):
327 if msg: 332 if msg:
328 print >> sys.stderr, 'Error:', msg 333 print >> sys.stderr, 'Error:', msg
329 print >> sys.stderr, 'Usage: run-example [options] [test...]' 334 print >> sys.stderr, 'Usage: run-example [options] [test...]'
330 print >> sys.stderr, 'Options:' 335 print >> sys.stderr, 'Options:'
331 print >> sys.stderr, ' -a --all run all tests in this directory' 336 print >> sys.stderr, ' -a --all run all tests in this directory'
332 print >> sys.stderr, ' -h --help print this help message' 337 print >> sys.stderr, ' -h --help print this help message'
338 print >> sys.stderr, ' --help keep new output as desired output'
333 print >> sys.stderr, ' -v --verbose display extra debug output' 339 print >> sys.stderr, ' -v --verbose display extra debug output'
334 sys.exit(exit) 340 sys.exit(exit)
335 341
336 def main(path='.'): 342 def main(path='.'):
337 opts, args = getopt.getopt(sys.argv[1:], '?ahv', 343 opts, args = getopt.getopt(sys.argv[1:], '?ahv',
338 ['all', 'help', 'verbose']) 344 ['all', 'help', 'keep', 'verbose'])
339 verbose = False 345 verbose = False
340 run_all = False 346 run_all = False
347 keep_change = False
341 for o, a in opts: 348 for o, a in opts:
342 if o in ('-h', '-?', '--help'): 349 if o in ('-h', '-?', '--help'):
343 print_help(0) 350 print_help(0)
344 if o in ('-a', '--all'): 351 if o in ('-a', '--all'):
345 run_all = True 352 run_all = True
353 if o in ('--keep',):
354 keep_change = True
346 if o in ('-v', '--verbose'): 355 if o in ('-v', '--verbose'):
347 verbose = True 356 verbose = True
348 errs = 0 357 errs = 0
349 if args: 358 if args:
350 for a in args: 359 for a in args:
353 except OSError, err: 362 except OSError, err:
354 print >> sys.stderr, '%s: %s' % (a, err.strerror) 363 print >> sys.stderr, '%s: %s' % (a, err.strerror)
355 errs += 1 364 errs += 1
356 continue 365 continue
357 if stat.S_ISREG(st.st_mode) and st.st_mode & 0111: 366 if stat.S_ISREG(st.st_mode) and st.st_mode & 0111:
358 if example(a, verbose).run(): 367 if example(a, verbose, keep_change).run():
359 errs += 1 368 errs += 1
360 else: 369 else:
361 print >> sys.stderr, '%s: not a file, or not executable' % a 370 print >> sys.stderr, '%s: not a file, or not executable' % a
362 errs += 1 371 errs += 1
363 elif run_all: 372 elif run_all:
374 # could be an output file that was removed while we ran 383 # could be an output file that was removed while we ran
375 if err.errno != errno.ENOENT: 384 if err.errno != errno.ENOENT:
376 raise 385 raise
377 continue 386 continue
378 if stat.S_ISREG(st.st_mode) and st.st_mode & 0111: 387 if stat.S_ISREG(st.st_mode) and st.st_mode & 0111:
379 if example(pathname, verbose).run(): 388 if example(pathname, verbose, keep_change).run():
380 errs += 1 389 errs += 1
381 print >> open(os.path.join(path, '.run'), 'w'), time.asctime() 390 print >> open(os.path.join(path, '.run'), 'w'), time.asctime()
382 else: 391 else:
383 print_help(1, msg='no test names given, and --all not provided') 392 print_help(1, msg='no test names given, and --all not provided')
384 return errs 393 return errs