comparison en/examples/run-example @ 258:1a55ba6ceca1

Make run-example a bit more user friendly.
author Bryan O'Sullivan <bos@serpentine.com>
date Wed, 06 Jun 2007 08:11:08 -0700
parents 06ab90119fa6
children 73aa295a40da d8913b7869b5
comparison
equal deleted inserted replaced
257:d9d29e7cf5bd 258:1a55ba6ceca1
321 else: 321 else:
322 print >> sys.stderr, '\nOutput of %s has changed!' % base 322 print >> sys.stderr, '\nOutput of %s has changed!' % base
323 os.system('diff -u %s %s 1>&2' % (oldname, errname)) 323 os.system('diff -u %s %s 1>&2' % (oldname, errname))
324 return True 324 return True
325 325
326 def print_help(exit, msg=None):
327 if msg:
328 print >> sys.stderr, 'Error:', msg
329 print >> sys.stderr, 'Usage: run-example [options] [test...]'
330 print >> sys.stderr, 'Options:'
331 print >> sys.stderr, ' -a --all run all tests in this directory'
332 print >> sys.stderr, ' -h --help print this help message'
333 print >> sys.stderr, ' -v --verbose display extra debug output'
334 sys.exit(exit)
335
326 def main(path='.'): 336 def main(path='.'):
327 opts, args = getopt.getopt(sys.argv[1:], 'v', ['verbose']) 337 opts, args = getopt.getopt(sys.argv[1:], '?ahv',
338 ['all', 'help', 'verbose'])
328 verbose = False 339 verbose = False
340 run_all = False
329 for o, a in opts: 341 for o, a in opts:
342 if o in ('-h', '-?', '--help'):
343 print_help(0)
344 if o in ('-a', '--all'):
345 run_all = True
330 if o in ('-v', '--verbose'): 346 if o in ('-v', '--verbose'):
331 verbose = True 347 verbose = True
332 errs = 0 348 errs = 0
333 if args: 349 if args:
334 for a in args: 350 for a in args:
342 if example(a, verbose).run(): 358 if example(a, verbose).run():
343 errs += 1 359 errs += 1
344 else: 360 else:
345 print >> sys.stderr, '%s: not a file, or not executable' % a 361 print >> sys.stderr, '%s: not a file, or not executable' % a
346 errs += 1 362 errs += 1
347 return errs 363 elif run_all:
348 names = os.listdir(path) 364 names = os.listdir(path)
349 names.sort() 365 names.sort()
350 for name in names: 366 for name in names:
351 if name == 'run-example' or name.startswith('.'): continue 367 if name == 'run-example' or name.startswith('.'): continue
352 if name.endswith('.out') or name.endswith('~'): continue 368 if name.endswith('.out') or name.endswith('~'): continue
353 if name.endswith('.run'): continue 369 if name.endswith('.run'): continue
354 pathname = os.path.join(path, name) 370 pathname = os.path.join(path, name)
355 try: 371 try:
356 st = os.lstat(pathname) 372 st = os.lstat(pathname)
357 except OSError, err: 373 except OSError, err:
358 # could be an output file that was removed while we ran 374 # could be an output file that was removed while we ran
359 if err.errno != errno.ENOENT: 375 if err.errno != errno.ENOENT:
360 raise 376 raise
361 continue 377 continue
362 if stat.S_ISREG(st.st_mode) and st.st_mode & 0111: 378 if stat.S_ISREG(st.st_mode) and st.st_mode & 0111:
363 if example(pathname, verbose).run(): 379 if example(pathname, verbose).run():
364 errs += 1 380 errs += 1
365 print >> open(os.path.join(path, '.run'), 'w'), time.asctime() 381 print >> open(os.path.join(path, '.run'), 'w'), time.asctime()
382 else:
383 print_help(1, msg='no test names given, and --all not provided')
366 return errs 384 return errs
367 385
368 if __name__ == '__main__': 386 if __name__ == '__main__':
369 sys.exit(main()) 387 try:
388 sys.exit(main())
389 except KeyboardInterrupt:
390 print >> sys.stderr, 'interrupted!'
391 sys.exit(1)