- Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Copy pathhotshotmain.py
executable file
·55 lines (42 loc) · 1.45 KB
/
hotshotmain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/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.
"""
importsys
importoptparse
importos
importhotshot
importhotshot.stats
PROFILE="hotshot.prof"
defrun_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
return0
defmain(args):
parser=optparse.OptionParser(__doc__)
parser.disable_interspersed_args()
parser.add_option("-p", "--profile", action="store", default=PROFILE,
dest="profile", help='Specify profile file to use')
(options, args) =parser.parse_args(args)
iflen(args) ==0:
parser.print_help("missing script to execute")
return1
filename=args[0]
returnrun_hotshot(filename, options.profile, args[1:])
if__name__=="__main__":
sys.exit(main(sys.argv[1:]))