Refactored command-line options handling in tick processor scripts
authormikhail.naganov@gmail.com <mikhail.naganov@gmail.com@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Wed, 18 Feb 2009 13:45:54 +0000 (13:45 +0000)
committermikhail.naganov@gmail.com <mikhail.naganov@gmail.com@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Wed, 18 Feb 2009 13:45:54 +0000 (13:45 +0000)
to remove code duplications. This makes easier adding new options.

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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1300 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

tools/linux-tick-processor.py
tools/tickprocessor.py
tools/windows-tick-processor.py

index e529e0a50d102565630e92c8177693ab141790d2..67c3b95525941ed5d2ba542a74185531e78c31a1 100755 (executable)
@@ -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 @@ class LinuxTickProcessor(tickprocessor.TickProcessor):
       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()
index bd432d21d5492ee0988046b59ec7e9c73f646e72..9459ae8c71db485bc1906dd846d2be2d49ecda90 100644 (file)
@@ -27,7 +27,7 @@
 
 import csv, splaytree, sys
 from operator import itemgetter
-
+import getopt, os
 
 class CodeEntry(object):
 
@@ -368,5 +368,49 @@ class TickProcessor(object):
         '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.')
index dc2578184554835b3dd66519efc00c12b4b7a352..ade2bf27e17f860f213adccfe9312feaa9e49652 100755 (executable)
@@ -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 @@ class WindowsTickProcessor(tickprocessor.TickProcessor):
     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__':