Commits

Konstantin Lopuhin  committed fd32ae1

handle options with argparse

  • Participants
  • Parent commits 8339f55

Comments (0)

Files changed (1)

File _jitviewer/app.py

 #!/usr/bin/env pypy
-""" A web-based browser of your log files. Run by
-
-    jitviewer.py <path to your log file> [port] [--qt]
+""" A web-based browser of your log files.
 
 By default the script will run a web server, point your browser to
 http://localhost:5000
 
 import sys
 import os.path
+import argparse
 
 try:
     import _jitviewer
     def index(self):
         all = flask.request.args.get('all', None)
         loops = []
+        # TODO - add back old sorting
         for index, loop in enumerate(reversed(sorted(
                 self.storage.loops, 
                 key=lambda l: getattr(l, 'statprof_ratio', 0)))):
     if not '__pypy__' in sys.builtin_module_names:
         print "Please run it using pypy-c"
         sys.exit(1)
-    #
-    server_mode = True
-    if '--qt' in argv:
-        server_mode = False
-        argv.remove('--qt')
-    #
-    if len(argv) != 2 and len(argv) != 3:
-        print __doc__
-        sys.exit(1)
-    filename = argv[1]
-    extra_path = os.path.dirname(filename)
-    if len(argv) != 3:
-        port = 5000
-    else:
-        port = int(argv[2])
+
+    parser = argparse.ArgumentParser(
+            description=__doc__,
+            formatter_class=argparse.RawDescriptionHelpFormatter)
+    parser.add_argument('filename', 
+            help='path to jit log file')
+    parser.add_argument('port', type=int, nargs='?', default=5000,
+            help='port for the server (default: 5000)')
+    parser.add_argument('--qt', dest='server_mode', action='store_false',
+            help='start lightweight browser')
+    parser.add_argument('--statlog', dest='statprof_filename',
+            help='path to statprof log file')
+    args = parser.parse_args()
+
+    extra_path = os.path.dirname(args.filename)
     storage = LoopStorage(extra_path)
-    log, loops = import_log(filename, ParserWithHtmlRepr)
+    log, loops = import_log(args.filename, ParserWithHtmlRepr)
 
-    # FIXME - filename
-    statprof_filename = os.path.join(
-            os.path.dirname(filename), 'statprof.log')
     jit_ratio = None
-    if os.path.exists(statprof_filename):
-        jit_ratio = parse_statprof_log(statprof_filename, loops) 
+    if args.statprof_filename:
+        jit_ratio = parse_statprof_log(args.statprof_filename, loops) 
 
     parse_log_counts(extract_category(log, 'jit-backend-count'), loops)
     storage.loops = [loop for loop in loops
     if jit_ratio is not None:
         storage.jit_ratio = jit_ratio
     app = OverrideFlask('_jitviewer')
-    server = Server(filename, storage)
+    server = Server(args.filename, storage)
     app.debug = True
     app.route('/')(server.index)
     app.route('/loop')(server.loop)
     if run_app:
         def run():
-            app.run(use_reloader=False, host='0.0.0.0', port=port)
+            app.run(use_reloader=False, host='0.0.0.0', port=args.port)
 
-        if server_mode:
+        if args.server_mode:
             run()
         else:
             url = "http://localhost:%d/" % port
-            run_server_and_browser(app, run, url, filename)
+            run_server_and_browser(app, run, url, args.filename)
     else:
         return app