Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4aba6f5

Browse files
author
Skip Montanaro
committed
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
1 parent ee3c607 commit 4aba6f5

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

Tools/scripts/hotshotmain.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python
2+
# -*- coding: iso-8859-1 -*-
3+
4+
"""
5+
Run a Python script under hotshot's control.
6+
7+
Adapted from a posting on python-dev by Walter Dörwald
8+
9+
usage %prog [ %prog args ] filename [ filename args ]
10+
11+
Any arguments after the filename are used as sys.argv for the filename.
12+
"""
13+
14+
import sys
15+
import optparse
16+
import os
17+
import hotshot
18+
import hotshot.stats
19+
20+
PROFILE = "hotshot.prof"
21+
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)
27+
prof.close()
28+
stats = hotshot.stats.load(profile)
29+
stats.sort_stats("time", "calls")
30+
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
35+
stats.print_stats()
36+
sys.stdout = save_stdout
37+
38+
return 0
39+
40+
def main(args):
41+
parser = optparse.OptionParser(__doc__)
42+
parser.add_option("-p", "--profile", action="store", default=PROFILE,
43+
dest="profile", help='Specify profile file to use')
44+
(options, args) = parser.parse_args(args)
45+
46+
if len(args) == 0:
47+
parser.print_help("missing script to execute")
48+
return 1
49+
50+
filename = args[0]
51+
return run_hotshot(filename, options.profile, args[1:])
52+
53+
if __name__ == "__main__":
54+
sys.exit(main(sys.argv[1:]))

0 commit comments

Comments
 (0)