提交 949010f8 编写于 作者: T Tyler Ramer 提交者: Tyler Ramer

Remove unused yaml class in mainUtils

It seems this yaml class is dead code. Removing it for this reason.
Co-authored-by: NTyler Ramer <tramer@vmware.com>
Co-authored-by: NJamie McAtamney <jmcatamney@vmware.com>
上级 8d6c3059
......@@ -16,7 +16,7 @@ extend common functions of our gp utilities. Please keep this in mind
and try to avoid placing logic for a specific utility here.
"""
import errno, os, sys, shutil, yaml
import errno, os, sys, shutil
gProgramName = os.path.split(sys.argv[0])[-1]
if sys.version_info < (2, 5, 0):
......@@ -421,270 +421,3 @@ def addMasterDirectoryOptionForSingleClusterProgram(addTo):
"for $MASTER_DATA_DIRECTORY will be used.")
#
# YamlMain
#
def get_yaml(targetclass):
"get_yaml"
# doc - class's doc string
# pos - where YAML starts in doc
# ystr - YAML string extracted from doc
if not hasattr(targetclass, '_yaml') or targetclass._yaml is None:
doc = targetclass.__doc__
pos = doc.find('%YAML')
assert pos >= 0, "targetclass doc string is missing %YAML plan"
ystr = doc[pos:].replace('\n ', '\n')
targetclass._yaml = yaml.load(ystr)
return targetclass._yaml
class YamlMain:
"YamlMain"
def __init__(self):
"Parse arguments based on yaml docstring"
self.current = None
self.plan = None
self.scenario_name = None
self.logger = None
self.logfilename = None
self.errmsg = None
self.parser = YamlOptions(self).parser
self.options, self.args = self.parser.parse_args()
self.options.quiet = self.options.q
self.options.verbose = self.options.v
#
# simple_main interface
#
def __call__(self, *args):
"Allows us to use self as the create_parser and create_program functions in call to simple_main"
return self
def parse_args(self):
"Called by simple_main to obtain results from parser returned by create_parser"
return self.options, self.args
def run(self):
"Called by simple_main to execute the program returned by create_program"
self.plan = Plan(self)
self.scenario_name = self.plan.name
self.logger = self.plan.logger
self.logfilename = self.plan.logfilename
self.errmsg = self.plan.errmsg
self.current = []
self.plan.run()
def cleanup(self):
"Called by simple_main to cleanup after program returned by create_program finishes"
pass
def simple(self):
"Delegates setup and control to mainUtils.simple_main"
simple_main(self, self)
#
# option parsing
#
class YamlOptions:
"YamlOptions"
def __init__(self, target):
"""
Scan the class doc string of the given object, looking for the %YAML
containing the option specification. Parse the YAML and setup the
corresponding OptionParser object.
"""
# target - options object (input)
# gname - option group name
self.y = get_yaml(target.__class__)
self.parser = OptionParser(description=self.y['Description'], version='%prog version $Revision$')
self.parser.remove_option('-h')
self.parser.set_usage(self.y['Usage'])
self.opty = self.y['Options']
for gname in self.opty.get('Groups', []):
self._register_group(gname)
def _register_group(self, gname):
"""
Register options for the specified option group name to the OptionParser
using an OptionGroup unless the group name starts with 'Help' in which
case we just register the options with the top level OptionParser object.
"""
# gname - option group name (input)
# gy - option group YAML object
# grp - option group object
# tgt - where to add options (parser or option group)
# optkey - comma separated list of option flags
# optval - help string or dict with detailed option settings
# listargs - list of option flags (e.g. ['-h', '--help'])
# dictargs - key/value arguments to add_option
gy = self.opty.get(gname, None)
if gname.startswith('Help'):
grp = None
tgt = self.parser
else:
grp = OptionGroup(self.parser, gname)
tgt = grp
for optkey, optval in gy.items():
listargs = optkey.split(',')
if type(optval) == type(''):
# short form: optval is just a help string
dictargs = {
'action': 'store_true',
'help': optval
}
else:
# optval is the complete option specification
dictargs = optval
# hide hidden options
if dictargs.get('help', '').startswith('hidden'):
dictargs['help'] = SUPPRESS_HELP
# print 'adding', listargs, dictargs
tgt.add_option(*listargs, **dictargs)
if grp is not None:
self.parser.add_option_group(grp)
#
# plan execution
#
class Task:
"Task"
def __init__(self, key, name, subtasks=None):
self.Key = key # task key
self.Name = name # task name
self.SubTasks = subtasks # subtasks, if any
self.Func = None # task function, set by _task
def _print(self, main, prefix):
print '%s %s %s:' % (prefix, self.Key, self.Name)
def _debug(self, main, prefix):
main.logger.debug('Execution Plan:%s %s %s%s' % (prefix, self.Key, self.Name, ':' if self.SubTasks else ''))
def _run(self, main, prefix):
main.logger.debug(' Now Executing:%s %s %s' % (prefix, self.Key, self.Name))
if self.Func:
self.Func()
class Exit(Exception):
def __init__(self, rc, code=None, call_support=False):
Exception.__init__(self)
self.code = code
self.prm = sys._getframe(1).f_locals
self.rc = rc
self.call_support = call_support
class Plan:
"Plan"
def __init__(self, main):
"""
Create cached yaml from class doc string of the given object,
looking for the %YAML indicating the beginning of the object's YAML plan and parse it.
Build the plan stages and tasks for the specified scenario.
"""
# main - object with yaml scenarios (input)
# sy - Stage yaml
self.logger = gplog.get_default_logger()
self.logfilename = gplog.get_logfile()
self.main = main
self.y = get_yaml(main.__class__)
self.name = main.options.scenario
if not self.name:
self.name = self.y['Default Scenario']
self.scenario = self.y['Scenarios'][self.name]
self.errors = self.y['Errors']
self.Tasks = [self._task(ty) for ty in self.scenario]
def _task(self, ty):
"Invoked by __init__ to build a top-level task from the YAML"
# ty - Task yaml (input)
# tyk - Task yaml key
# tyv - Task yaml value
# sty - Sub Task yaml
# t - Task (returned)
for tyk, tyv in ty.items():
key, workers = tyk.split(None, 1)
subtasks = [self._subtask(sty) for sty in tyv]
t = Task(key, workers, subtasks)
return t
def _subtask(self, sty):
"Invoked by _stage to build a task from the YAML"
# sty - Sub Task yaml (input)
# st - Sub Task (returned)
key, rest = sty.split(None, 1)
st = Task(key, rest)
fn = st.Name.lower().replace(' ', '_')
try:
st.Func = getattr(self.main, fn)
except AttributeError, e:
raise Exception("Failed to lookup '%s' for sub task '%s': %s" % (fn, st.Name, str(e)))
return st
def _dotasks(self, subtasks, prefix, action):
"Apply an action to each subtask recursively"
# st - Sub Task
for st in subtasks or []:
self.main.current.append(st)
action(st, self.main, prefix)
self._dotasks(st.SubTasks, ' ' + prefix, action)
self.main.current.pop()
def _print(self):
"Print in YAML form."
print '%s:' % self.name
self._dotasks(self.Tasks, ' -', lambda t, m, p: t._print(m, p))
def run(self):
"Run the stages and tasks."
self.logger.debug('Execution Plan: %s' % self.name)
self._dotasks(self.Tasks, ' -', lambda t, m, p: t._debug(m, p))
self.logger.debug(' Now Executing: %s' % self.name)
try:
self._dotasks(self.Tasks, ' -', lambda t, m, p: t._run(m, p))
except Exit, e:
self.exit(e.code, e.prm, e.rc, e.call_support)
def errmsg(self, code, prm={}):
"Return a formatted error message"
return self.errors[code] % prm
def exit(self, code=None, prm={}, rc=1, call_support=False):
"Terminate the application"
if code:
msg = self.errmsg(code, prm)
self.logger.error(msg)
if call_support:
self.logger.error('Please send %s to Greenplum support.' % self.logfilename)
self.logger.debug('exiting with status %(rc)s' % locals())
sys.exit(rc)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册