Author: [email protected]
Date: Wed Feb 18 05:45:54 2009
New Revision: 1300

Modified:
    branches/bleeding_edge/tools/linux-tick-processor.py
    branches/bleeding_edge/tools/tickprocessor.py
    branches/bleeding_edge/tools/windows-tick-processor.py

Log:
Refactored command-line options handling in tick processor scripts
to remove code duplications. This makes easier adding new options.

Review URL: http://codereview.chromium.org/20452


Modified: branches/bleeding_edge/tools/linux-tick-processor.py
==============================================================================
--- branches/bleeding_edge/tools/linux-tick-processor.py        (original)
+++ branches/bleeding_edge/tools/linux-tick-processor.py        Wed Feb 18  
05:45:54 2009
@@ -30,7 +30,7 @@
  # Usage: process-ticks.py <logfile>
  # Where <logfile> is the log file name (eg, v8.log).

-import subprocess, re, sys, tickprocessor, getopt
+import subprocess, re, sys, tickprocessor

  class LinuxTickProcessor(tickprocessor.TickProcessor):

@@ -54,36 +54,25 @@
        pipe.close()


-def Usage():
-  print("Usage: linux-tick-processor.py --{js,gc,compiler,other}   
logfile-name");
-  sys.exit(2)
+class LinuxCmdLineProcessor(tickprocessor.CmdLineProcessor):
+
+  def GetRequiredArgsNames(self):
+    return 'log_file'
+
+  def ProcessRequiredArgs(self, args):
+    if len(args) != 1:
+      self.PrintUsageAndExit()
+    else:
+      self.log_file = args[0]
+

  def Main():
-  # parse command line options
-  ignore_unknown = False
-  state = None;
-  try:
-    opts, args = getopt.getopt(sys.argv[1:], "jgco",  
["js", "gc", "compiler", "other", "ignore-unknown"])
-  except getopt.GetoptError:
-    usage()
-  # process options.
-  for key, value in opts:
-    if key in ("-j", "--js"):
-      state = 0
-    if key in ("-g", "--gc"):
-      state = 1
-    if key in ("-c", "--compiler"):
-      state = 2
-    if key in ("-o", "--other"):
-      state = 3
-    if key in ("--ignore-unknown"):
-      ignore_unknown = True
-  # do the processing.
-  if len(args) != 1:
-      Usage();
+  cmdline_processor = LinuxCmdLineProcessor()
+  cmdline_processor.ProcessArguments()
    tick_processor = LinuxTickProcessor()
-  tick_processor.ProcessLogfile(args[0], state, ignore_unknown)
+  cmdline_processor.RunLogfileProcessing(tick_processor)
    tick_processor.PrintResults()
+

  if __name__ == '__main__':
    Main()

Modified: branches/bleeding_edge/tools/tickprocessor.py
==============================================================================
--- branches/bleeding_edge/tools/tickprocessor.py       (original)
+++ branches/bleeding_edge/tools/tickprocessor.py       Wed Feb 18 05:45:54 2009
@@ -27,7 +27,7 @@

  import csv, splaytree, sys
  from operator import itemgetter
-
+import getopt, os

  class CodeEntry(object):

@@ -367,6 +367,50 @@
          'total' : total_percentage,
          'call_path' : stack[0] + '  <-  ' + stack[1]
        })
+
+
+class CmdLineProcessor(object):
+
+  def __init__(self):
+    # default values
+    self.state = None
+    self.ignore_unknown = False
+    self.log_file = None
+
+  def ProcessArguments(self):
+    try:
+      opts, args = getopt.getopt(sys.argv[1:], "jgco",  
["js", "gc", "compiler", "other", "ignore-unknown"])
+    except getopt.GetoptError:
+      self.PrintUsageAndExit()
+    for key, value in opts:
+      if key in ("-j", "--js"):
+        self.state = 0
+      if key in ("-g", "--gc"):
+        self.state = 1
+      if key in ("-c", "--compiler"):
+        self.state = 2
+      if key in ("-o", "--other"):
+        self.state = 3
+      if key in ("--ignore-unknown"):
+        self.ignore_unknown = True
+    self.ProcessRequiredArgs(args)
+
+  def ProcessRequiredArgs(self, args):
+    return
+
+  def GetRequiredArgsNames(self):
+    return
+
+  def PrintUsageAndExit(self):
+    print('Usage: %(script_name)s --{js,gc,compiler,other} %(req_opts)s' %  
{
+        'script_name': os.path.basename(sys.argv[0]),
+        'req_opts': self.GetRequiredArgsNames()
+    })
+    sys.exit(2)
+
+  def RunLogfileProcessing(self, tick_processor):
+    tick_processor.ProcessLogfile(self.log_file, self.state,  
self.ignore_unknown)
+

  if __name__ == '__main__':
    sys.exit('You probably want to run windows-tick-processor.py or  
linux-tick-processor.py.')

Modified: branches/bleeding_edge/tools/windows-tick-processor.py
==============================================================================
--- branches/bleeding_edge/tools/windows-tick-processor.py      (original)
+++ branches/bleeding_edge/tools/windows-tick-processor.py      Wed Feb 18  
05:45:54 2009
@@ -38,7 +38,7 @@
  # only works for statically linked executables - no information about
  # shared libraries is logged from v8 on Windows.

-import os, re, sys, tickprocessor, getopt
+import os, re, sys, tickprocessor

  class WindowsTickProcessor(tickprocessor.TickProcessor):

@@ -107,36 +107,30 @@
      finally:
        map_file.close()

-def Usage():
-  print("Usage: windows-tick-processor.py binary logfile-name");
-  sys.exit(2)
+
+class WindowsCmdLineProcessor(tickprocessor.CmdLineProcessor):
+
+  def __init__(self):
+    super(WindowsCmdLineProcessor, self).__init__()
+    self.binary_file = None
+
+  def GetRequiredArgsNames(self):
+    return 'binary log_file'
+
+  def ProcessRequiredArgs(self, args):
+    if len(args) != 2:
+      self.PrintUsageAndExit()
+    else:
+      self.binary_file = args[0]
+      self.log_file = args[1]
+

  def Main():
-  # parse command line options
-  ignore_unknown = False
-  state = None;
-  try:
-    opts, args = getopt.getopt(sys.argv[1:], "jgco",  
["js", "gc", "compiler", "other", "ignore-unknown"])
-  except getopt.GetoptError:
-    usage()
-  # process options.
-  for key, value in opts:
-    if key in ("-j", "--js"):
-      state = 0
-    if key in ("-g", "--gc"):
-      state = 1
-    if key in ("-c", "--compiler"):
-      state = 2
-    if key in ("-o", "--other"):
-      state = 3
-    if key in ("--ignore-unknown"):
-      ignore_unknown = True
-  # do the processing.
-  if len(args) != 2:
-      Usage();
+  cmdline_processor = WindowsCmdLineProcessor()
+  cmdline_processor.ProcessArguments()
    tickprocessor = WindowsTickProcessor()
-  tickprocessor.ParseMapFile(args[0])
-  tickprocessor.ProcessLogfile(args[1], state, ignore_unknown)
+  tickprocessor.ParseMapFile(cmdline_processor.binary_file)
+  cmdline_processor.RunLogfileProcessing(tickprocessor)
    tickprocessor.PrintResults()

  if __name__ == '__main__':

--~--~---------~--~----~------------~-------~--~----~
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
-~----------~----~----~----~------~----~------~--~---

Reply via email to