mirror of
https://github.com/python/cpython.git
synced 2025-10-14 18:59:46 +00:00
Move code in __name__ == '__main__' block into main() function, rewrite code so
there's no need to subclass OptionParser.
This commit is contained in:
parent
97613ad02d
commit
2abe785fee
1 changed files with 14 additions and 16 deletions
|
@ -583,31 +583,29 @@ class Profile:
|
||||||
def Stats(*args):
|
def Stats(*args):
|
||||||
print 'Report generating functions are in the "pstats" module\a'
|
print 'Report generating functions are in the "pstats" module\a'
|
||||||
|
|
||||||
|
def main():
|
||||||
# When invoked as main program, invoke the profiler on a script
|
|
||||||
if __name__ == '__main__':
|
|
||||||
usage = "profile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
|
usage = "profile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
|
||||||
if not sys.argv[1:]:
|
parser = OptionParser(usage=usage)
|
||||||
print "Usage: ", usage
|
|
||||||
sys.exit(2)
|
|
||||||
|
|
||||||
class ProfileParser(OptionParser):
|
|
||||||
def __init__(self, usage):
|
|
||||||
OptionParser.__init__(self)
|
|
||||||
self.usage = usage
|
|
||||||
|
|
||||||
parser = ProfileParser(usage)
|
|
||||||
parser.allow_interspersed_args = False
|
parser.allow_interspersed_args = False
|
||||||
parser.add_option('-o', '--outfile', dest="outfile",
|
parser.add_option('-o', '--outfile', dest="outfile",
|
||||||
help="Save stats to <outfile>", default=None)
|
help="Save stats to <outfile>", default=None)
|
||||||
parser.add_option('-s', '--sort', dest="sort",
|
parser.add_option('-s', '--sort', dest="sort",
|
||||||
help="Sort order when printing to stdout, based on pstats.Stats class", default=-1)
|
help="Sort order when printing to stdout, based on pstats.Stats class", default=-1)
|
||||||
|
|
||||||
|
if not sys.argv[1:]:
|
||||||
|
parser.print_usage()
|
||||||
|
sys.exit(2)
|
||||||
|
|
||||||
(options, args) = parser.parse_args()
|
(options, args) = parser.parse_args()
|
||||||
sys.argv[:] = args
|
sys.argv[:] = args
|
||||||
|
|
||||||
if (len(sys.argv) > 0):
|
if (len(sys.argv) > 0):
|
||||||
sys.path.insert(0, os.path.dirname(sys.argv[0]))
|
sys.path.insert(0, os.path.dirname(sys.argv[0]))
|
||||||
run('execfile(%r)' % (sys.argv[0],), options.outfile, options.sort)
|
run('execfile(%r)' % (sys.argv[0],), options.outfile, options.sort)
|
||||||
else:
|
else:
|
||||||
print "Usage: ", usage
|
parser.print_usage()
|
||||||
|
return parser
|
||||||
|
|
||||||
|
# When invoked as main program, invoke the profiler on a script
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue