From ddd2ac40157899b5a646b8d3b3dd644ad0515c69 Mon Sep 17 00:00:00 2001 From: "mikhail.naganov@gmail.com" Date: Wed, 18 Feb 2009 13:45:54 +0000 Subject: [PATCH] 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 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1300 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- tools/linux-tick-processor.py | 43 +++++++++++----------------- tools/tickprocessor.py | 46 +++++++++++++++++++++++++++++- tools/windows-tick-processor.py | 50 +++++++++++++++------------------ 3 files changed, 83 insertions(+), 56 deletions(-) diff --git a/tools/linux-tick-processor.py b/tools/linux-tick-processor.py index e529e0a50..67c3b9552 100755 --- a/tools/linux-tick-processor.py +++ b/tools/linux-tick-processor.py @@ -30,7 +30,7 @@ # Usage: process-ticks.py # Where 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() diff --git a/tools/tickprocessor.py b/tools/tickprocessor.py index bd432d21d..9459ae8c7 100644 --- a/tools/tickprocessor.py +++ b/tools/tickprocessor.py @@ -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.') diff --git a/tools/windows-tick-processor.py b/tools/windows-tick-processor.py index dc2578184..ade2bf27e 100755 --- a/tools/windows-tick-processor.py +++ b/tools/windows-tick-processor.py @@ -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__': -- 2.34.1