(original) (raw)
#!/usr/bin/env python # -*- coding: iso-8859-1 -*- """ Run a Python script under hotshot's control. Adapted from a posting on python-dev by Walter D�rwald usage %(prog)s [ -h ] [ -p file ] filename [ args ] -h - print this documentation and exit. -p file - record profiling data in file (default %(PROFILE)s). Any arguments after the filename are used as sys.argv for the filename. """ import sys import getopt import os import hotshot import hotshot.stats print hotshot.stats.__file__ prog = sys.argv[0] PROFILE = "hotshot.prof" def usage(msg=None): if msg is not None: print >> sys.stderr, msg print >> sys.stderr, __doc__.strip() % globals() def run_hotshot(filename, profile, args): prof = hotshot.Profile(profile) sys.path.insert(0, os.path.dirname(filename)) sys.argv = [filename] + args prof.run("execfile(%r)" % filename) prof.close() stats = hotshot.stats.load(profile) stats.sort_stats("time", "calls") stats.print_stats() return 0 def main(args): try: opts, args = getopt.getopt(args, "hp:", ["help", "profile="]) except getopt.GetoptError, msg: usage(msg) return 1 profile = PROFILE for opt, arg in opts: if opt in ("-h", "--help"): usage() return 0 if opt in ("-p", "--profile"): profile = arg return 0 if len(args) == 0: usage("missing script to execute") return 1 filename = args[0] return run_hotshot(filename, profile, args[1:]) if __name__ == "__main__": sys.exit(main(sys.argv[1:]))