nodes: 0, module; 1, function_definition; 2, function_name:prof_main; 3, parameters; 4, block; 5, identifier:main; 6, expression_statement; 7, decorated_definition; 8, return_statement; 9, comment:"""
      Decorator for profiling main programs.
  
      Profiling is activated by prepending the command line options
      supported by the original main program with the keyword `prof`.
      Example:
  
          $ script.py arg --foo=1
  
      becomes
  
          $ script.py prof arg --foo=1
  
      The decorated main accepts two new arguments:
  
          prof_file: Name of the output file with profiling data
              If not given, a temporary file is created.
          sortby: Profiling data are sorted according to this value.
              default is "time". See sort_stats.
      """; 10, decorator; 11, function_definition; 12, identifier:wrapper; 13, call; 14, function_name:wrapper; 15, parameters; 16, block; 17, identifier:wraps; 18, argument_list; 19, list_splat_pattern; 20, dictionary_splat_pattern; 21, import_statement; 22, try_statement; 23, if_statement; 24, identifier:main; 25, identifier:args; 26, identifier:kwargs; 27, dotted_name; 28, block; 29, except_clause; 30, not_operator; 31, block; 32, else_clause; 33, identifier:sys; 34, expression_statement; 35, if_statement; 36, identifier:Exception; 37, block; 38, identifier:do_prof; 39, expression_statement; 40, block; 41, assignment; 42, identifier:do_prof; 43, block; 44, expression_statement; 45, call; 46, expression_statement; 47, import_statement; 48, expression_statement; 49, if_statement; 50, expression_statement; 51, expression_statement; 52, expression_statement; 53, expression_statement; 54, if_statement; 55, identifier:do_prof; 56, comparison_operator:sys.argv[1] == "prof"; 57, expression_statement; 58, assignment; 59, attribute; 60, argument_list; 61, call; 62, dotted_name; 63, dotted_name; 64, dotted_name; 65, assignment; 66, comparison_operator:prof_file is None; 67, block; 68, assignment; 69, call; 70, assignment; 71, call; 72, comparison_operator:"retval" not in kwargs; 73, block; 74, else_clause; 75, subscript; 76, string:"prof"; 77, call; 78, identifier:do_prof; 79, False; 80, identifier:sys; 81, identifier:exit; 82, call; 83, identifier:print; 84, argument_list; 85, identifier:pstats; 86, identifier:cProfile; 87, identifier:tempfile; 88, identifier:prof_file; 89, call; 90, identifier:prof_file; 91, None; 92, expression_statement; 93, expression_statement; 94, identifier:sortby; 95, call; 96, attribute; 97, argument_list; 98, identifier:s; 99, call; 100, attribute; 101, argument_list; 102, string:"retval"; 103, identifier:kwargs; 104, expression_statement; 105, block; 106, attribute; 107, integer:1; 108, attribute; 109, argument_list; 110, identifier:main; 111, argument_list; 112, string:"Entering profiling mode..."; 113, attribute; 114, argument_list; 115, assignment; 116, call; 117, attribute; 118, argument_list; 119, identifier:cProfile; 120, identifier:runctx; 121, string:"main()"; 122, call; 123, call; 124, identifier:prof_file; 125, attribute; 126, argument_list; 127, call; 128, identifier:print_stats; 129, call; 130, return_statement; 131, identifier:sys; 132, identifier:argv; 133, attribute; 134, identifier:pop; 135, integer:1; 136, identifier:kwargs; 137, identifier:get; 138, string:"prof_file"; 139, None; 140, pattern_list; 141, call; 142, identifier:print; 143, argument_list; 144, identifier:kwargs; 145, identifier:get; 146, string:"sortby"; 147, string:"time"; 148, identifier:globals; 149, argument_list; 150, identifier:locals; 151, argument_list; 152, identifier:pstats; 153, identifier:Stats; 154, identifier:prof_file; 155, attribute; 156, argument_list; 157, attribute; 158, argument_list; 159, subscript; 160, identifier:sys; 161, identifier:argv; 162, identifier:_; 163, identifier:prof_file; 164, attribute; 165, argument_list; 166, binary_operator:"Profiling data stored in %s" % prof_file; 167, call; 168, identifier:sort_stats; 169, identifier:sortby; 170, identifier:sys; 171, identifier:exit; 172, integer:0; 173, identifier:kwargs; 174, string:"retval"; 175, identifier:tempfile; 176, identifier:mkstemp; 177, string:"Profiling data stored in %s"; 178, identifier:prof_file; 179, attribute; 180, argument_list; 181, identifier:s; 182, identifier:strip_dirs
code: def prof_main(main):
      """
      Decorator for profiling main programs.
  
      Profiling is activated by prepending the command line options
      supported by the original main program with the keyword `prof`.
      Example:
  
          $ script.py arg --foo=1
  
      becomes
  
          $ script.py prof arg --foo=1
  
      The decorated main accepts two new arguments:
  
          prof_file: Name of the output file with profiling data
              If not given, a temporary file is created.
          sortby: Profiling data are sorted according to this value.
              default is "time". See sort_stats.
      """
      @wraps(main)
      def wrapper(*args, **kwargs):
          import sys
          try:
              do_prof = sys.argv[1] == "prof"
              if do_prof: sys.argv.pop(1)
          except Exception:
              do_prof = False
  
          if not do_prof:
              sys.exit(main())
          else:
              print("Entering profiling mode...")
              import pstats, cProfile, tempfile
              prof_file = kwargs.get("prof_file", None)
              if prof_file is None:
                  _, prof_file = tempfile.mkstemp()
                  print("Profiling data stored in %s" % prof_file)
  
              sortby = kwargs.get("sortby", "time")
              cProfile.runctx("main()", globals(), locals(), prof_file)
              s = pstats.Stats(prof_file)
              s.strip_dirs().sort_stats(sortby).print_stats()
              if "retval" not in kwargs:
                  sys.exit(0)
              else:
                  return kwargs["retval"]
  
      return wrapper