changeset ad060d1f1037 in /z/repo/m5 details: http://repo.m5sim.org/m5?cmd=changeset;node=ad060d1f1037 summary: python: Separate the options parsing stuff. Remove options parsing stuff from
changeset 576aa675d4e5 in /z/repo/m5 details: http://repo.m5sim.org/m5?cmd=changeset;node=576aa675d4e5 summary: Add .m5 configuration directory changeset 5df361e08b81 in /z/repo/m5 details: http://repo.m5sim.org/m5?cmd=changeset;node=5df361e08b81 summary: main: add .m5/options.py processing. This file is processed before diffstat: 4 files changed, 96 insertions(+), 1 deletion(-) src/python/SConscript | 1 src/python/m5/config.py | 21 +++++++++++++ src/python/m5/main.py | 3 + src/python/m5/options.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++ diffs (truncated from 386 to 300 lines): diff -r 42719798884a -r 5df361e08b81 src/python/SConscript --- a/src/python/SConscript Sat Jun 14 20:42:45 2008 -0700 +++ b/src/python/SConscript Sat Jun 14 21:16:00 2008 -0700 @@ -36,9 +36,11 @@ Source('swig/pyobject.cc') PySource('m5', 'm5/__init__.py') PySource('m5', 'm5/SimObject.py') +PySource('m5', 'm5/config.py') PySource('m5', 'm5/convert.py') PySource('m5', 'm5/event.py') PySource('m5', 'm5/main.py') +PySource('m5', 'm5/options.py') PySource('m5', 'm5/params.py') PySource('m5', 'm5/proxy.py') PySource('m5', 'm5/simulate.py') diff -r 42719798884a -r 5df361e08b81 src/python/m5/config.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/python/m5/config.py Sat Jun 14 21:16:00 2008 -0700 @@ -0,0 +1,42 @@ +# Copyright (c) 2008 The Hewlett-Packard Development Company +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer; +# redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution; +# neither the name of the copyright holders nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# Authors: Nathan Binkert + +import os +from os.path import isdir, isfile, join as joinpath + +homedir = os.environ['HOME'] +confdir = os.environ.get('M5_CONFIG', joinpath(homedir, '.m5')) +def get(name): + if not isdir(confdir): + return None + conffile = joinpath(confdir, name) + if not isfile(conffile): + return None + + return conffile + diff -r 42719798884a -r 5df361e08b81 src/python/m5/main.py --- a/src/python/m5/main.py Sat Jun 14 20:42:45 2008 -0700 +++ b/src/python/m5/main.py Sat Jun 14 21:16:00 2008 -0700 @@ -28,24 +28,17 @@ import code import datetime -import optparse import os import socket import sys from util import attrdict +import config import defines +from options import OptionParser import traceflags __all__ = [ 'options', 'arguments', 'main' ] - -usage="%prog [m5 options] script.py [script options]" -version="%prog 2.0" -brief_copyright=''' -Copyright (c) 2001-2008 -The Regents of The University of Michigan -All Rights Reserved -''' def print_list(items, indent=4): line = ' ' * indent @@ -60,64 +53,19 @@ def print_list(items, indent=4): line += item print line -# there's only one option parsing done, so make it global and add some -# helper functions to make it work well. -parser = optparse.OptionParser(usage=usage, version=version, - description=brief_copyright, - formatter=optparse.TitledHelpFormatter()) -parser.disable_interspersed_args() +usage="%prog [m5 options] script.py [script options]" +version="%prog 2.0" +brief_copyright=''' +Copyright (c) 2001-2008 +The Regents of The University of Michigan +All Rights Reserved +''' -# current option group -group = None - -def set_group(*args, **kwargs): - '''set the current option group''' - global group - if not args and not kwargs: - group = None - else: - group = parser.add_option_group(*args, **kwargs) - -class splitter(object): - def __init__(self, split): - self.split = split - def __call__(self, option, opt_str, value, parser): - getattr(parser.values, option.dest).extend(value.split(self.split)) - -def add_option(*args, **kwargs): - '''add an option to the current option group, or global none set''' - - # if action=split, but allows the option arguments - # themselves to be lists separated by the split variable''' - - if kwargs.get('action', None) == 'append' and 'split' in kwargs: - split = kwargs.pop('split') - kwargs['default'] = [] - kwargs['type'] = 'string' - kwargs['action'] = 'callback' - kwargs['callback'] = splitter(split) - - if group: - return group.add_option(*args, **kwargs) - - return parser.add_option(*args, **kwargs) - -def bool_option(name, default, help): - '''add a boolean option called --name and --no-name. - Display help depending on which is the default''' - - tname = '--%s' % name - fname = '--no-%s' % name - dest = name.replace('-', '_') - if default: - thelp = optparse.SUPPRESS_HELP - fhelp = help - else: - thelp = help - fhelp = optparse.SUPPRESS_HELP - - add_option(tname, action="store_true", default=default, help=thelp) - add_option(fname, action="store_false", dest=dest, help=fhelp) +options = OptionParser(usage=usage, version=version, + description=brief_copyright) +add_option = options.add_option +set_group = options.set_group +usage = options.usage # Help options add_option('-A', "--authors", action="store_true", default=False, @@ -168,39 +116,20 @@ add_option("--trace-ignore", metavar="EX add_option("--trace-ignore", metavar="EXPR", action='append', split=':', help="Ignore EXPR sim objects") -options = attrdict() -arguments = [] - -def usage(exitcode=None): - parser.print_help() - if exitcode is not None: - sys.exit(exitcode) - -def parse_args(): - _opts,args = parser.parse_args() - opts = attrdict(_opts.__dict__) - - # setting verbose and quiet at the same time doesn't make sense - if opts.verbose > 0 and opts.quiet > 0: - usage(2) - - # store the verbosity in a single variable. 0 is default, - # negative numbers represent quiet and positive values indicate verbose - opts.verbose -= opts.quiet - - del opts.quiet - - options.update(opts) - arguments.extend(args) - return opts,args - def main(): import defines import event import info import internal - parse_args() + # load the options.py config file to allow people to set their own + # default options + options_file = config.get('options.py') + if options_file: + scope = { 'options' : options } + execfile(options_file, scope) + + arguments = options.parse_args() done = False @@ -261,6 +190,11 @@ def main(): if done: sys.exit(0) + # setting verbose and quiet at the same time doesn't make sense + if options.verbose > 0 and options.quiet > 0: + options.usage(2) + + verbose = options.verbose - options.quiet if options.verbose >= 0: print "M5 Simulator System" print brief_copyright @@ -282,7 +216,7 @@ def main(): if arguments and not os.path.isfile(arguments[0]): print "Script %s not found" % arguments[0] - usage(2) + options.usage(2) # tell C++ about output directory internal.core.setOutputDir(options.outdir) diff -r 42719798884a -r 5df361e08b81 src/python/m5/options.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/python/m5/options.py Sat Jun 14 21:16:00 2008 -0700 @@ -0,0 +1,145 @@ +# Copyright (c) 2005 The Regents of The University of Michigan +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer; +# redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution; +# neither the name of the copyright holders nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# Authors: Nathan Binkert + +import optparse +import sys +import util + +from optparse import * + +class nodefault(object): pass + +class splitter(object): + def __init__(self, split): + self.split = split + def __call__(self, option, opt_str, value, parser): + values = value.split(self.split) + dest = getattr(parser.values, option.dest) + if dest is None: + setattr(parser.values, option.dest, values) + else: + dest.extend(values) + +class OptionParser(object): + def __init__(self, *args, **kwargs): + kwargs.setdefault('formatter', optparse.TitledHelpFormatter()) + self._optparse = optparse.OptionParser(*args, **kwargs) + self._optparse.disable_interspersed_args() + + self._allopts = {} + self._defaults = {} + self._options = util.attrdict() + + # current option group + self._group = self._optparse _______________________________________________ m5-dev mailing list [email protected] http://m5sim.org/mailman/listinfo/m5-dev
