()
| 561 | #**************************************************************************** |
| 562 | |
| 563 | def main(): |
| 564 | import os |
| 565 | from optparse import OptionParser |
| 566 | |
| 567 | usage = "profile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ..." |
| 568 | parser = OptionParser(usage=usage) |
| 569 | parser.allow_interspersed_args = False |
| 570 | parser.add_option('-o', '--outfile', dest="outfile", |
| 571 | help="Save stats to <outfile>", default=None) |
| 572 | parser.add_option('-m', dest="module", action="store_true", |
| 573 | help="Profile a library module.", default=False) |
| 574 | parser.add_option('-s', '--sort', dest="sort", |
| 575 | help="Sort order when printing to stdout, based on pstats.Stats class", |
| 576 | default=-1) |
| 577 | |
| 578 | if not sys.argv[1:]: |
| 579 | parser.print_usage() |
| 580 | sys.exit(2) |
| 581 | |
| 582 | (options, args) = parser.parse_args() |
| 583 | sys.argv[:] = args |
| 584 | |
| 585 | # The script that we're profiling may chdir, so capture the absolute path |
| 586 | # to the output file at startup. |
| 587 | if options.outfile is not None: |
| 588 | options.outfile = os.path.abspath(options.outfile) |
| 589 | |
| 590 | if len(args) > 0: |
| 591 | if options.module: |
| 592 | import runpy |
| 593 | code = "run_module(modname, run_name='__main__')" |
| 594 | globs = { |
| 595 | 'run_module': runpy.run_module, |
| 596 | 'modname': args[0] |
| 597 | } |
| 598 | else: |
| 599 | progname = args[0] |
| 600 | sys.path.insert(0, os.path.dirname(progname)) |
| 601 | with io.open_code(progname) as fp: |
| 602 | code = compile(fp.read(), progname, 'exec') |
| 603 | spec = importlib.machinery.ModuleSpec(name='__main__', loader=None, |
| 604 | origin=progname) |
| 605 | globs = { |
| 606 | '__spec__': spec, |
| 607 | '__file__': spec.origin, |
| 608 | '__name__': spec.name, |
| 609 | '__package__': None, |
| 610 | } |
| 611 | try: |
| 612 | runctx(code, globs, None, options.outfile, options.sort) |
| 613 | except BrokenPipeError as exc: |
| 614 | # Prevent "Exception ignored" during interpreter shutdown. |
| 615 | sys.stdout = None |
| 616 | sys.exit(exc.errno) |
| 617 | else: |
| 618 | parser.print_usage() |
| 619 | return parser |
| 620 |
no test coverage detected
searching dependent graphs…