[Python-checkins] python/dist/src/Tools/scripts hotshotmain.py, NONE, 1.1

montanaro at projects.sourceforge.net montanaro at projects.sourceforge.net
Mon Jan 26 14:44:51 EST 2004

Update of /cvsroot/python/python/dist/src/Tools/scripts
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3865

Added Files:
Log Message:
The bagpipe didn't say "no" (*), so here's a main program script useful for
running an application under hotshot's control.  Only slightly embellished
from what Walter Dörwald posted to python-dev.

(*) http://www.icdc.com/~roadkill/silverstein/turtle.html

--- NEW FILE: hotshotmain.py ---
#!/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)
    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
    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__":

More information about the Python-checkins mailing list