From 4aba6f51cb5af7952ee2ff320a08c804545e3220 Mon Sep 17 00:00:00 2001 From: Skip Montanaro Date: Mon, 26 Jan 2004 19:44:48 +0000 Subject: =?UTF-8?q?The=20bagpipe=20didn't=20say=20"no"=20(*),=20so=20here'?= =?UTF-8?q?s=20a=20main=20program=20script=20useful=20for=20running=20an?= =?UTF-8?q?=20application=20under=20hotshot's=20control.=20=20Only=20sligh?= =?UTF-8?q?tly=20embellished=20from=20what=20Walter=20D=C3=B6rwald=20poste?= =?UTF-8?q?d=20to=20python-dev.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (*) http://www.icdc.com/~roadkill/silverstein/turtle.html --- Tools/scripts/hotshotmain.py | 54 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Tools/scripts/hotshotmain.py diff --git a/Tools/scripts/hotshotmain.py b/Tools/scripts/hotshotmain.py new file mode 100644 index 0000000..0912056 --- /dev/null +++ b/Tools/scripts/hotshotmain.py @@ -0,0 +1,54 @@ +#!/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 [ %prog args ] filename [ filename args ] + +Any arguments after the filename are used as sys.argv for the filename. +""" + +import sys +import optparse +import os +import hotshot +import hotshot.stats + +PROFILE = "hotshot.prof" + +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") + + # print_stats uses unadorned print statements, so the only way + # to force output to stderr is to reassign sys.stdout temporarily + save_stdout = sys.stdout + sys.stdout = sys.stderr + stats.print_stats() + sys.stdout = save_stdout + + return 0 + +def main(args): + parser = optparse.OptionParser(__doc__) + parser.add_option("-p", "--profile", action="store", default=PROFILE, + dest="profile", help='Specify profile file to use') + (options, args) = parser.parse_args(args) + + if len(args) == 0: + parser.print_help("missing script to execute") + return 1 + + filename = args[0] + return run_hotshot(filename, options.profile, args[1:]) + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) -- cgit v0.12