2 # -*- coding: iso-8859-1 -*-
5 Run a Python script under hotshot's control.
7 Adapted from a posting on python-dev by Walter Dörwald
9 usage %prog [ %prog args ] filename [ filename args ]
11 Any arguments after the filename are used as sys.argv for the filename.
20 PROFILE = "hotshot.prof"
22 def run_hotshot(filename, profile, args):
23 prof = hotshot.Profile(profile)
24 sys.path.insert(0, os.path.dirname(filename))
25 sys.argv = [filename] + args
26 prof.run("execfile(%r)" % filename)
28 stats = hotshot.stats.load(profile)
29 stats.sort_stats("time", "calls")
31 # print_stats uses unadorned print statements, so the only way
32 # to force output to stderr is to reassign sys.stdout temporarily
33 save_stdout = sys.stdout
34 sys.stdout = sys.stderr
36 sys.stdout = save_stdout
41 parser = optparse.OptionParser(__doc__)
42 parser.disable_interspersed_args()
43 parser.add_option("-p", "--profile", action="store", default=PROFILE,
44 dest="profile", help='Specify profile file to use')
45 (options, args) = parser.parse_args(args)
48 parser.print_help("missing script to execute")
52 return run_hotshot(filename, options.profile, args[1:])
54 if __name__ == "__main__":
55 sys.exit(main(sys.argv[1:]))