setup.py
上传用户:quxuerui
上传日期:2018-01-08
资源大小:41811k
文件大小:45k
源码类别:

网格计算

开发平台:

Java

  1. #Licensed to the Apache Software Foundation (ASF) under one
  2. #or more contributor license agreements.  See the NOTICE file
  3. #distributed with this work for additional information
  4. #regarding copyright ownership.  The ASF licenses this file
  5. #to you under the Apache License, Version 2.0 (the
  6. #"License"); you may not use this file except in compliance
  7. #with the License.  You may obtain a copy of the License at
  8. #     http://www.apache.org/licenses/LICENSE-2.0
  9. #Unless required by applicable law or agreed to in writing, software
  10. #distributed under the License is distributed on an "AS IS" BASIS,
  11. #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. #See the License for the specific language governing permissions and
  13. #limitations under the License.
  14. # $Id:setup.py 5158 2007-04-09 00:14:35Z zim $
  15. # $Id:setup.py 5158 2007-04-09 00:14:35Z zim $
  16. #
  17. #------------------------------------------------------------------------------
  18. """'setup' provides for reading and verifing configuration files based on
  19.    Python's SafeConfigParser class."""
  20. import sys, os, re, pprint
  21. from ConfigParser import SafeConfigParser
  22. from optparse import OptionParser, IndentedHelpFormatter, OptionGroup
  23. from util import get_perms, replace_escapes
  24. from types import typeValidator, typeValidatorInstance, is_valid_type, 
  25.                   typeToString
  26. from hodlib.Hod.hod import hodHelp
  27. reEmailAddress = re.compile("^.*@.*$")
  28. reEmailDelimit = re.compile("@")
  29. reComma = re.compile("s*,s*")
  30. reDot = re.compile(".")
  31. reCommentHack = re.compile("^.*?s+#|;.*", flags=re.S)
  32. reCommentNewline = re.compile("n|r$")
  33. reKeyVal = r"(?<!\)="
  34. reKeyVal = re.compile(reKeyVal)
  35. reKeyValList = r"(?<!\),"
  36. reKeyValList = re.compile(reKeyValList)
  37. errorPrefix = 'error'
  38. requiredPerms = '0660'
  39. class definition:
  40.     def __init__(self):
  41.         """Generates a configuration definition object."""
  42.         self.__def = {}
  43.         self.__defOrder = []
  44.     def __repr__(self):
  45.         return pprint.pformat(self.__def)  
  46.     def __getitem__(self, section):
  47.         return self.__def[section]
  48.     def __iter__(self):
  49.         return iter(self.__def)
  50.     def sections(self):
  51.         """Returns a list of sections/groups."""
  52.         
  53.         if len(self.__defOrder):
  54.             return self.__defOrder
  55.         else:  
  56.             return self.__def.keys()
  57.       
  58.     def add_section(self, section):
  59.         """Add a configuration section / option group."""
  60.         
  61.         if self.__def.has_key(section):
  62.             raise Exception("Section already exists: '%s'" % section)
  63.         else:
  64.             self.__def[section] = {}
  65.     def add_def(self, section, var, type, desc, help = True, default = None, 
  66.                 req = True, validate = True, short = None):
  67.         """ Add a variable definition.
  68.         
  69.             section  - section name
  70.             var      - variable name
  71.             type     - valid hodlib.types
  72.             desc     - description of variable
  73.             help     - display help for this variable
  74.             default  - default value
  75.             req      - bool, requried?
  76.             validate - bool, validate type value?
  77.             short    - short symbol (1 character),
  78.             help     - bool, display help?"""
  79.             
  80.         if self.__def.has_key(section):
  81.             if not is_valid_type(type):
  82.                 raise Exception("Type (type) is invalid: %s.%s - '%s'" % (section, var, 
  83.                                                                 type))
  84.             if not isinstance(desc, str):
  85.                 raise Exception("Description (desc) must be a string: %s.%s - '%s'" % (
  86.                     section, var, desc))
  87.             if not isinstance(req, bool):
  88.                 raise Exception("Required (req) must be a bool: %s.%s - '%s'" % (section, 
  89.                                                                        var, 
  90.                                                                        req))
  91.             if not isinstance(validate, bool):
  92.                 raise Exception("Validate (validate) must be a bool: %s.%s - '%s'" % (
  93.                     section, var, validate))
  94.               
  95.             if self.__def[section].has_key(var):
  96.                 raise Exception("Variable name already defined: '%s'" % var)
  97.             else:
  98.                 self.__def[section][var] = { 'type'     : type,
  99.                                              'desc'     : desc,
  100.                                              'help'     : help,
  101.                                              'default'  : default,
  102.                                              'req'      : req,
  103.                                              'validate' : validate,
  104.                                              'short'    : short }                
  105.         else:    
  106.             raise Exception("Section does not exist: '%s'" % section)
  107.           
  108.     def add_defs(self, defList, defOrder=None):
  109.         """ Add a series of definitions.
  110.         
  111.             defList = { section0 : ((name0, 
  112.                                      type0, 
  113.                                      desc0, 
  114.                                      help0,
  115.                                      default0,
  116.                                      req0, 
  117.                                      validate0,
  118.                                      short0),
  119.                                   ....
  120.                                     (nameN, 
  121.                                      typeN, 
  122.                                      descN,
  123.                                      helpN, 
  124.                                      defaultN, 
  125.                                      reqN, 
  126.                                      validateN,
  127.                                      shortN)),             
  128.                            ....
  129.                            
  130.                         sectionN : ... }
  131.                         
  132.             Where the short synmbol is optional and can only be one char."""
  133.                         
  134.         for section in defList.keys():
  135.             self.add_section(section)
  136.             for defTuple in defList[section]:
  137.                 if isinstance(defTuple, tuple): 
  138.                     if len(defTuple) < 7:
  139.                         raise Exception(
  140.                             "section %s is missing an element: %s" % (
  141.                             section, pprint.pformat(defTuple)))
  142.                 else:
  143.                     raise Exception("section %s of defList is not a tuple" % 
  144.                                     section)
  145.                 
  146.                 if len(defTuple) == 7:
  147.                     self.add_def(section, defTuple[0], defTuple[1], 
  148.                                  defTuple[2], defTuple[3], defTuple[4], 
  149.                                  defTuple[5], defTuple[6])
  150.                 else:
  151.                     self.add_def(section, defTuple[0], defTuple[1], 
  152.                                  defTuple[2], defTuple[3], defTuple[4], 
  153.                                  defTuple[5], defTuple[6], defTuple[7])                     
  154.         if defOrder:
  155.             for section in defOrder:
  156.                 if section in self.__def:
  157.                     self.__defOrder.append(section)
  158.                     
  159.             for section in self.__def:
  160.                 if not section in defOrder:
  161.                     raise Exception(
  162.                         "section %s is missing from specified defOrder." % 
  163.                         section)
  164.             
  165. class baseConfig:
  166.     def __init__(self, configDef, originalDir=None):
  167.         self.__toString = typeToString()
  168.         self.__validated = False
  169.         self._configDef = configDef
  170.         self._options = None
  171.         self._mySections = []
  172.         self._dict = {}
  173.         self.configFile = None
  174.         self.__originalDir = originalDir
  175.         if self._configDef:
  176.             self._mySections = configDef.sections()
  177.     def __repr__(self):
  178.         """Returns a string representation of a config object including all
  179.            normalizations."""
  180.         print_string = '';
  181.         for section in self._mySections:
  182.             print_string = "%s[%s]n" % (print_string, section)
  183.             options = self._dict[section].keys()
  184.             for option in options:
  185.                 print_string = "%s%s = %sn" % (print_string, option,
  186.                     self._dict[section][option])
  187.             print_string = "%sn" % (print_string)
  188.         print_string = re.sub("nn$", "", print_string)
  189.         return print_string
  190.     def __getitem__(self, section):
  191.         """ Returns a dictionary of configuration name and values by section.
  192.         """
  193.         return self._dict[section]
  194.     def __setitem__(self, section, value):
  195.         self._dict[section] = value
  196.     def __iter__(self):
  197.         return iter(self._dict)
  198.     def has_key(self, section):
  199.         status = False
  200.         if section in self._dict:
  201.             status = True
  202.             
  203.         return status
  204.     # Prints configuration error messages
  205.     def var_error(self, section, option, *addData):
  206.         errorStrings = []  
  207.         if not self._dict[section].has_key(option):
  208.           self._dict[section][option] = None
  209.         errorStrings.append("%s: invalid '%s' specified in section %s (--%s.%s): %s" % (
  210.             errorPrefix, option, section, section, option, self._dict[section][option]))
  211.         if addData:
  212.             errorStrings.append("%s: additional info: %sn" % (errorPrefix,
  213.                 addData[0]))
  214.         return errorStrings
  215.     def var_error_suggest(self, errorStrings):
  216.         if self.configFile:
  217.             errorStrings.append("Check your command line options and/or " + 
  218.                               "your configuration file %s" % self.configFile)
  219.     
  220.     def __get_args(self, section):
  221.         def __dummyToString(type, value):
  222.             return value
  223.         
  224.         toString = __dummyToString
  225.         if self.__validated:
  226.             toString = self.__toString
  227.             
  228.         args = []
  229.         if isinstance(self._dict[section], dict):
  230.             for option in self._dict[section]:
  231.                 if section in self._configDef and 
  232.                 option in self._configDef[section]:
  233.                   if self._configDef[section][option]['type'] == 'bool':
  234.                     if self._dict[section][option] == 'True' or 
  235.                         self._dict[section][option] == True:
  236.                         args.append("--%s.%s" % (section, option))
  237.                   else:
  238.                     args.append("--%s.%s" % (section, option))
  239.                     args.append(toString(
  240.                            self._configDef[section][option]['type'], 
  241.                            self._dict[section][option]))
  242.         else:
  243.             if section in self._configDef:
  244.               if self._configDef[section][option]['type'] == 'bool':
  245.                 if self._dict[section] == 'True' or 
  246.                     self._dict[section] == True:
  247.                     args.append("--%s" % section)
  248.               else:
  249.                 if self._dict[section] != 'config':
  250.                   args.append("--%s" % section)
  251.                   args.append(toString(self._configDef[section]['type'], 
  252.                                              self._dict[section]))
  253.                     
  254.         return args
  255.                 
  256.     def values(self):
  257.         return self._dict.values()
  258.       
  259.     def keys(self):
  260.         return self._dict.keys()
  261.     
  262.     def get_args(self, exclude=None, section=None):
  263.         """Retrieve a tuple of config arguments."""
  264.         
  265.         args = []
  266.         if section:
  267.             args = self.__get_args(section)
  268.         else:
  269.             for section in self._dict:
  270.                 if exclude:
  271.                     if not section in exclude:
  272.                         args.extend(self.__get_args(section))
  273.                 else:
  274.                     args.extend(self.__get_args(section))
  275.         
  276.         return tuple(args)
  277.         
  278.     def verify(self):
  279.         """Verifies each configuration variable, using the configValidator
  280.            class, based on its type as defined by the dictionary configDef.
  281.            Upon encountering a problem an error is printed to STDERR and
  282.            false is returned."""
  283.         
  284.         oldDir = os.getcwd()
  285.         if self.__originalDir:
  286.           os.chdir(self.__originalDir)
  287.         
  288.         status = True
  289.         statusMsgs = []
  290.         if self._configDef:
  291.             errorCount = 0
  292.             configValidator = typeValidator(self.__originalDir)
  293.             # foreach section and option by type string as defined in configDef
  294.             #   add value to be validated to validator
  295.             for section in self._mySections:
  296.                 for option in self._configDef[section].keys():
  297.                     configVarName = "%s.%s" % (section, option)
  298.                     if self._dict[section].has_key(option):
  299.                         if self._configDef[section][option].has_key('validate'):
  300.                             if self._configDef[section][option]['validate']:
  301.                                 # is the section.option needed to be validated?
  302.                                 configValidator.add(configVarName,
  303.                                     self._configDef[section][option]['type'],
  304.                                     self._dict[section][option])
  305.                             else:
  306.                                 # If asked not to validate, just normalize
  307.                                 self[section][option] = 
  308.                                     configValidator.normalize(
  309.                                     self._configDef[section][option]['type'], 
  310.                                     self._dict[section][option])
  311.                             if self._configDef[section][option]['default'] != 
  312.                                 None:
  313.                                 self._configDef[section][option]['default'] = 
  314.                                     configValidator.normalize(
  315.                                     self._configDef[section][option]['type'],
  316.                                     self._configDef[section][option]['default']
  317.                                     )
  318.                                 self._configDef[section][option]['default'] = 
  319.                                     self.__toString(
  320.                                     self._configDef[section][option]['type'], 
  321.                                     self._configDef[section][option]['default']
  322.                                     )
  323.                         else:        
  324.                             # This should not happen. Just in case, take this as 'to be validated' case.
  325.                             configValidator.add(configVarName,
  326.                                 self._configDef[section][option]['type'],
  327.                                 self._dict[section][option])
  328.                     elif self._configDef[section][option]['req']:
  329.                         statusMsgs.append("%s: %s.%s is not defined."
  330.                              % (errorPrefix, section, option))
  331.                         errorCount = errorCount + 1                         
  332.             configValidator.validate()
  333.             for valueInfo in configValidator.validatedInfo:
  334.                 sectionsOptions = reDot.split(valueInfo['name'])
  335.                 if valueInfo['isValid'] == 1:
  336.                     self._dict[sectionsOptions[0]][sectionsOptions[1]] = 
  337.                         valueInfo['normalized']
  338.                 else:
  339.                     if valueInfo['errorData']:
  340.                         statusMsgs.extend(self.var_error(sectionsOptions[0],
  341.                             sectionsOptions[1], valueInfo['errorData']))
  342.                     else:
  343.                         statusMsgs.extend(self.var_error(sectionsOptions[0],
  344.                             sectionsOptions[1]))
  345.                     errorCount = errorCount + 1
  346.             if errorCount > 1:
  347.                 statusMsgs.append( "%s: %s problems found." % (
  348.                     errorPrefix, errorCount))
  349.                 self.var_error_suggest(statusMsgs)
  350.                 status = False
  351.             elif errorCount > 0:
  352.                 statusMsgs.append( "%s: %s problem found." % (
  353.                     errorPrefix, errorCount))
  354.                 self.var_error_suggest(statusMsgs)
  355.                 status = False
  356.         
  357.         self.__validated = True
  358.         if self.__originalDir:
  359.           os.chdir(oldDir)
  360.         return status,statusMsgs
  361.     def normalizeValue(self, section, option)  :
  362.       return typeValidatorInstance.normalize(
  363.                                   self._configDef[section][option]['type'],
  364.                                   self[section][option])
  365.     def validateValue(self, section, option):
  366.       # Validates a section.option and exits on error
  367.       valueInfo = typeValidatorInstance.verify(
  368.                                   self._configDef[section][option]['type'],
  369.                                   self[section][option])
  370.       if valueInfo['isValid'] == 1:
  371.         return []
  372.       else:
  373.         if valueInfo['errorData']:
  374.           return self.var_error(section, option, valueInfo['errorData'])
  375.         else:
  376.           return self.var_error(section, option)
  377. class config(SafeConfigParser, baseConfig):
  378.     def __init__(self, configFile, configDef=None, originalDir=None, 
  379.                  options=None, checkPerms=False):
  380.         """Constructs config object.
  381.            configFile - configuration file to read
  382.            configDef  - definition object
  383.            options    - options object
  384.            checkPerms - check file permission on config file, 0660
  385.            sample configuration file:
  386.             [snis]
  387.             modules_dir  = modules/       ; location of infoModules
  388.             md5_defs_dir = etc/md5_defs   ; location of infoTree md5 defs
  389.             info_store   = var/info       ; location of nodeInfo store
  390.             cam_daemon   = localhost:8200 ; cam daemon address"""
  391.         SafeConfigParser.__init__(self)
  392.         baseConfig.__init__(self, configDef, originalDir)
  393.         if(os.path.exists(configFile)):
  394.           self.configFile = configFile
  395.         else:
  396.           raise IOError
  397.         
  398.         self._options = options
  399.         
  400. ## UNUSED CODE : checkPerms is never True
  401.   ## zim: this code is used if one instantiates config() with checkPerms set to
  402.   ## True.
  403.         if checkPerms: self.__check_perms()
  404.         self.read(configFile)
  405.         self._configDef = configDef
  406.         if not self._configDef:
  407.             self._mySections = self.sections()
  408.         self.__initialize_config_dict()
  409.     def __initialize_config_dict(self):
  410.         """ build a dictionary of config vars keyed by section name defined in
  411.            configDef, if options defined override config"""
  412.         for section in self._mySections:
  413.             items = self.items(section)
  414.             self._dict[section] = {}
  415.             # First fill self._dict with whatever is given in hodrc.
  416.             # Going by this, options given at the command line either override
  417.             # options in hodrc, or get appended to the list, like for
  418.             # hod.client-params. Note that after this dict has _only_ hodrc
  419.             # params
  420.             for keyValuePair in items:
  421.                 # stupid commenting bug in ConfigParser class, lines without an
  422.                 #  option value pair or section required that ; or # are at the
  423.                 #  beginning of the line, :(
  424.                 newValue = reCommentHack.sub("", keyValuePair[1])
  425.                 newValue = reCommentNewline.sub("", newValue)
  426.                 self._dict[section][keyValuePair[0]] = newValue
  427.             # end of filling with options given in hodrc
  428.             # now start filling in command line options
  429.             if self._options:    
  430.                 for option in self._configDef[section].keys():
  431.                     if self._options[section].has_key(option):
  432.                         # the user has given an option
  433.                         compoundOpt = "%s.%s" %(section,option)
  434.                         if ( compoundOpt == 
  435.                               'gridservice-mapred.final-server-params' 
  436.                               or compoundOpt == 
  437.                                     'gridservice-hdfs.final-server-params' 
  438.                               or compoundOpt == 
  439.                                     'gridservice-mapred.server-params' 
  440.                               or compoundOpt == 
  441.                                     'gridservice-hdfs.server-params' 
  442.                               or compoundOpt == 
  443.                                     'hod.client-params' ):
  444.                  
  445.                            if ( compoundOpt == 
  446.                               'gridservice-mapred.final-server-params' 
  447.                               or compoundOpt == 
  448.                                     'gridservice-hdfs.final-server-params' ):
  449.                               overwrite = False
  450.                            else: overwrite = True
  451.                            # Append to the current list of values in self._dict
  452.                            if not self._dict[section].has_key(option):
  453.                              self._dict[section][option] = ""
  454.                            dictOpts = reKeyValList.split(self._dict[section][option])
  455.                            dictOptsKeyVals = {}
  456.                            for opt in dictOpts:
  457.                               if opt != '':
  458.                                 # when dict _has_ params from hodrc
  459.                                 if reKeyVal.search(opt):
  460.                                   (key, val) = reKeyVal.split(opt,1)
  461.                                   # we only consider the first '=' for splitting
  462.                                   # we do this to support passing params like
  463.                                   # mapred.child.java.opts=-Djava.library.path=some_dir
  464.                                   # Even in case of an invalid error like unescaped '=',
  465.                                   # we don't want to fail here itself. We leave such errors 
  466.                                   # to be caught during validation which happens after this
  467.                                   dictOptsKeyVals[key] = val
  468.                                 else: 
  469.                                   # this means an invalid option. Leaving it
  470.                                   #for config.verify to catch
  471.                                   dictOptsKeyVals[opt] = None
  472.                                 
  473.                            cmdLineOpts = reKeyValList.split(self._options[section][option])
  474.                            for opt in cmdLineOpts:
  475.                               if reKeyVal.search(opt):
  476.                                 # Same as for hodrc options. only consider
  477.                                 # the first =
  478.                                 ( key, val ) = reKeyVal.split(opt,1)
  479.                               else:
  480.                                 key = opt
  481.                                 val = None
  482.                               # whatever is given at cmdline overrides
  483.                               # what is given in hodrc only for non-final params
  484.                               if dictOptsKeyVals.has_key(key):
  485.                                 if overwrite:
  486.                                   dictOptsKeyVals[key] = val
  487.                               else: dictOptsKeyVals[key] = val
  488.                               
  489.                            self._dict[section][option] = ""
  490.                            for key in dictOptsKeyVals:
  491.                               if self._dict[section][option] == "":
  492.                                 if dictOptsKeyVals[key]:
  493.                                   self._dict[section][option] = key + "=" + 
  494.                                     dictOptsKeyVals[key]
  495.                                 else: #invalid option. let config.verify catch
  496.                                   self._dict[section][option] = key
  497.                               else:
  498.                                 if dictOptsKeyVals[key]:
  499.                                   self._dict[section][option] = 
  500.                                     self._dict[section][option] + "," + key + 
  501.                                       "=" + dictOptsKeyVals[key]
  502.                                 else:  #invalid option. let config.verify catch
  503.                                   self._dict[section][option] = 
  504.                                     self._dict[section][option] + "," + key
  505.                         else:
  506.                              # for rest of the options, that don't need
  507.                             # appending business.
  508.                             # options = cmdline opts + defaults
  509.                             # dict    = hodrc opts only
  510.                             # only non default opts can overwrite any opt
  511.                             # currently in dict
  512.                            if not self._dict[section].has_key(option):
  513.                               # options not mentioned in hodrc
  514.                               self._dict[section][option] = 
  515.                                                self._options[section][option]
  516.                            elif self._configDef[section][option]['default'] != 
  517.                                                self._options[section][option]:
  518.                               # option mentioned in hodrc but user has given a
  519.                               # non-default option
  520.                               self._dict[section][option] = 
  521.                                                self._options[section][option]
  522.     ## UNUSED METHOD
  523.     ## zim: is too :)
  524.     def __check_perms(self):
  525.         perms = None
  526.         if self._options:  
  527.             try:
  528.                 perms = get_perms(self.configFile)
  529.             except OSError, data:
  530.                 self._options.print_help()
  531.                 raise Exception("*** could not find config file: %s" % data)
  532.                 sys.exit(1)
  533.         else:
  534.             perms = get_perms(self.configFile)
  535.                
  536.         if perms != requiredPerms:
  537.             error = "*** '%s' has invalid permission: %s should be %sn" % 
  538.                 (self.configFile, perms, requiredPerms)
  539.             raise Exception( error)
  540.             sys.exit(1)
  541.     def replace_escape_seqs(self):
  542.       """ replace any escaped characters """
  543.       replace_escapes(self)
  544. class formatter(IndentedHelpFormatter):
  545.     def format_option_strings(self, option):
  546.         """Return a comma-separated list of option strings & metavariables."""
  547.         if option.takes_value():
  548.             metavar = option.metavar or option.dest.upper()
  549.             short_opts = [sopt
  550.                           for sopt in option._short_opts]
  551.             long_opts = [self._long_opt_fmt % (lopt, metavar)
  552.                          for lopt in option._long_opts]
  553.         else:
  554.             short_opts = option._short_opts
  555.             long_opts = option._long_opts
  556.         if self.short_first:
  557.             opts = short_opts + long_opts
  558.         else:
  559.             opts = long_opts + short_opts
  560.         return ", ".join(opts)    
  561. class options(OptionParser, baseConfig):
  562.     def __init__(self, optionDef, usage, version, originalDir=None, 
  563.                  withConfig=False, defaultConfig=None, defaultLocation=None,
  564.                  name=None):
  565.         """Constructs and options object.
  566.          
  567.            optionDef     - definition object
  568.            usage         - usage statement
  569.            version       - version string
  570.            withConfig    - used in conjunction with a configuration file
  571.            defaultConfig - default configuration file
  572.         
  573.         """
  574.         OptionParser.__init__(self, usage=usage)
  575.         baseConfig.__init__(self, optionDef, originalDir)
  576.         
  577.         self.formatter = formatter(4, max_help_position=100, width=180, 
  578.                                    short_first=1)
  579.         
  580.         self.__name = name
  581.         self.__version = version
  582.         self.__withConfig = withConfig
  583.         self.__defaultConfig = defaultConfig
  584.         self.__defaultLoc = defaultLocation
  585.         self.args = []
  586.         self.__optionList = []
  587.         self.__compoundOpts = []
  588.         self.__shortMap = {}
  589.         self.__alphaString = 'abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ1234567890'
  590.         self.__alpha = []
  591.         self.__parsedOptions = {}
  592.         self.__reserved = [ 'h' ]
  593.         
  594.         self.__orig_grps = []
  595.         self.__orig_grp_lists = {}
  596.         self.__orig_option_list = []
  597.         
  598.         self.__display_grps = []
  599.         self.__display_grp_lists = {}
  600.         self.__display_option_list = [] 
  601.         
  602.         self.config = None
  603.         
  604.         if self.__withConfig:
  605.             self.__reserved.append('c')
  606.         self.__reserved.append('v')
  607.         
  608.         self.__gen_alpha()            
  609.         # build self.__optionList, so it contains all the options that are
  610.         # possible. the list elements are of the form section.option
  611.         for section in self._mySections:
  612.             if self.__withConfig and section == 'config':
  613.                 raise Exception(
  614.                     "withConfig set 'config' cannot be used as a section name")
  615.             for option in self._configDef[section].keys():
  616.                 if '.' in option:
  617.                     raise Exception("Options cannot contain: '.'")
  618.                 elif self.__withConfig and option == 'config':
  619.                     raise Exception(
  620.                         "With config set, option config is not allowed.")
  621.                 elif self.__withConfig and option == 'verbose-help':
  622.                     raise Exception(
  623.                         "With config set, option verbose-help is not allowed.")                 
  624.                 self.__optionList.append(self.__splice_compound(section, 
  625.                                                                 option))
  626.         self.__build_short_map()
  627.         self.__add_options()
  628.         self.__init_display_options() 
  629.         
  630.         (self.__parsedOptions, self.args) = self.parse_args()
  631.         # Now process the positional arguments only for the client side
  632.         if self.__name == 'hod':
  633.           hodhelp = hodHelp()
  634.           _operation = getattr(self.__parsedOptions,'hod.operation')
  635.           _script = getattr(self.__parsedOptions, 'hod.script')
  636.           nArgs = self.args.__len__()
  637.           if _operation:
  638.             # -o option is given
  639.             if nArgs != 0:
  640.               self.error('invalid syntax : command and operation(-o) cannot coexist')
  641.           elif nArgs == 0 and _script:
  642.             # for a script option, without subcommand: hod -s script ...
  643.             pass
  644.           elif nArgs == 0:
  645.             print "Usage: ",hodhelp.help()
  646.             sys.exit(0)
  647.           else:
  648.             # subcommand is given
  649.             cmdstr = self.args[0] # the subcommand itself
  650.             cmdlist = hodhelp.ops
  651.             if cmdstr not in cmdlist:
  652.               print "Usage: ", hodhelp.help()
  653.               sys.exit(2)
  654.             numNodes = None
  655.             clusterDir = None
  656.             # Check which subcommand. cmdstr  = subcommand itself now.
  657.             if cmdstr == "allocate":
  658.               clusterDir = getattr(self.__parsedOptions, 'hod.clusterdir')
  659.               numNodes = getattr(self.__parsedOptions, 'hod.nodecount')
  660.  
  661.               if not clusterDir or not numNodes:
  662.                 print hodhelp.usage(cmdstr)
  663.                 sys.exit(3)
  664.               cmdstr = cmdstr + ' ' + clusterDir + ' ' + numNodes
  665.               setattr(self.__parsedOptions,'hod.operation', cmdstr)
  666.  
  667.             elif cmdstr == "deallocate" or cmdstr == "info":
  668.               clusterDir = getattr(self.__parsedOptions, 'hod.clusterdir')
  669.               if not clusterDir:
  670.                 print hodhelp.usage(cmdstr)
  671.                 sys.exit(3)
  672.  
  673.               cmdstr = cmdstr + ' ' + clusterDir
  674.               setattr(self.__parsedOptions,'hod.operation', cmdstr)
  675.             elif cmdstr == "list":
  676.               setattr(self.__parsedOptions,'hod.operation', cmdstr)
  677.               pass
  678.  
  679.             elif cmdstr == "script":
  680.               clusterDir = getattr(self.__parsedOptions, 'hod.clusterdir')
  681.               numNodes = getattr(self.__parsedOptions, 'hod.nodecount')
  682.               originalDir = getattr(self.__parsedOptions, 'hod.original-dir')
  683.               if originalDir and clusterDir:
  684.                 self.remove_exit_code_file(originalDir, clusterDir)
  685.               if not _script or not clusterDir or not numNodes:
  686.                 print hodhelp.usage(cmdstr)
  687.                 sys.exit(3)
  688.               pass
  689.             elif cmdstr == "help":
  690.               if nArgs == 1:
  691.                 self.print_help()
  692.                 sys.exit(0)
  693.               elif nArgs != 2:
  694.                 self.print_help()
  695.                 sys.exit(3)
  696.               elif self.args[1] == 'options':
  697.                 self.print_options()
  698.                 sys.exit(0)
  699.               cmdstr = cmdstr + ' ' + self.args[1]
  700.               setattr(self.__parsedOptions,'hod.operation', cmdstr)
  701.         # end of processing for arguments on the client side
  702.         if self.__withConfig:
  703.             self.config = self.__parsedOptions.config
  704.             if not self.config:
  705.                 self.error("configuration file must be specified")
  706.             if not os.path.isabs(self.config):
  707.                 # A relative path. Append the original directory which would be the
  708.                 # current directory at the time of launch
  709.                 try:  
  710.                     origDir = getattr(self.__parsedOptions, 'hod.original-dir')
  711.                     if origDir is not None:
  712.                         self.config = os.path.join(origDir, self.config)
  713.                         self.__parsedOptions.config = self.config
  714.                 except AttributeError, e:
  715.                     self.error("hod.original-dir is not defined.
  716.                                    Cannot get current directory")
  717.             if not os.path.exists(self.config):
  718.                 if self.__defaultLoc and not re.search("/", self.config):
  719.                     self.__parsedOptions.config = os.path.join(
  720.                         self.__defaultLoc, self.config)
  721.         self.__build_dict()   
  722.     def norm_cluster_dir(self, orig_dir, directory):
  723.         directory = os.path.expanduser(directory)
  724.         if not os.path.isabs(directory):
  725.             directory = os.path.join(orig_dir, directory)
  726.         directory = os.path.abspath(directory)
  727.         return directory
  728.     def remove_exit_code_file(self, orig_dir, dir):
  729.         try:
  730.             dir = self.norm_cluster_dir(orig_dir, dir)
  731.             if os.path.exists(dir):
  732.                 exit_code_file = os.path.join(dir, "script.exitcode")
  733.                 if os.path.exists(exit_code_file):
  734.                     os.remove(exit_code_file)
  735.         except:
  736.             print >>sys.stderr, "Could not remove the script.exitcode file."
  737.     
  738.     def __init_display_options(self):
  739.         self.__orig_option_list = self.option_list[:]
  740.         optionListTitleMap = {}
  741.         for option in self.option_list:
  742.             optionListTitleMap[option._long_opts[0]] = option
  743.       
  744.         self.__orig_grps = self.option_groups[:]
  745.         for group in self.option_groups:
  746.             self.__orig_grp_lists[group.title] = group.option_list[:]
  747.                                     
  748.         groupTitleMap = {}
  749.         optionTitleMap = {}
  750.         for group in self.option_groups:
  751.             groupTitleMap[group.title] = group
  752.             optionTitleMap[group.title] = {}
  753.             for option in group.option_list:
  754.                 (sectionName, optionName) = 
  755.                     self.__split_compound(option._long_opts[0])
  756.                 optionTitleMap[group.title][optionName] = option
  757.           
  758.         for section in self._mySections:
  759.             for option in self._configDef[section]:
  760.                 if self._configDef[section][option]['help']:
  761.                     if groupTitleMap.has_key(section):
  762.                         if not self.__display_grp_lists.has_key(section):
  763.                             self.__display_grp_lists[section] = []
  764.                         self.__display_grp_lists[section].append(
  765.                             optionTitleMap[section][option])
  766.                     
  767.                     try:    
  768.                         self.__display_option_list.append(
  769.                             optionListTitleMap["--" + self.__splice_compound(
  770.                             section, option)])
  771.                     except KeyError:
  772.                         pass
  773.         try:
  774.             self.__display_option_list.append(optionListTitleMap['--config'])
  775.         except KeyError:
  776.             pass
  777.           
  778.         self.__display_option_list.append(optionListTitleMap['--help'])
  779.         self.__display_option_list.append(optionListTitleMap['--verbose-help'])
  780.         self.__display_option_list.append(optionListTitleMap['--version'])
  781.                     
  782.         self.__display_grps = self.option_groups[:]             
  783.         for section in self._mySections:
  784.             if self.__display_grp_lists.has_key(section):
  785.                 self.__orig_grp_lists[section] = 
  786.                     groupTitleMap[section].option_list
  787.             else:
  788.                 try:
  789.                     self.__display_grps.remove(groupTitleMap[section])
  790.                 except KeyError:
  791.                     pass
  792.                 
  793.     def __gen_alpha(self):
  794.         assignedOptions = []
  795.         for section in self._configDef:
  796.             for option in self._configDef[section]:
  797.                 if self._configDef[section][option]['short']:
  798.                     assignedOptions.append(
  799.                         self._configDef[section][option]['short'])
  800.         
  801.         for symbol in self.__alphaString:
  802.             if not symbol in assignedOptions:
  803.                 self.__alpha.append(symbol)
  804.     def __splice_compound(self, section, option):
  805.         return "%s.%s" % (section, option)
  806.         
  807.     def __split_compound(self, compound):    
  808.         return compound.split('.')
  809.         
  810.     def __build_short_map(self):
  811.         """ build a short_map of parametername : short_option. This is done
  812.         only for those parameters that don't have short options already
  813.         defined in configDef.
  814.         If possible, the first letter in the option that is not already
  815.         used/reserved as a short option is allotted. Otherwise the first
  816.         letter in __alpha that isn't still used is allotted.
  817.         e.g. { 'hodring.java-home': 'T', 'resource_manager.batch-home': 'B' }
  818.         """
  819.         optionsKey = {}
  820.         for compound in self.__optionList:
  821.             (section, option) = self.__split_compound(compound)
  822.             if not optionsKey.has_key(section):
  823.                 optionsKey[section] = []
  824.             optionsKey[section].append(option)
  825.         
  826.         for section in self._configDef.sections():
  827.             options = optionsKey[section]
  828.             options.sort()
  829.             for option in options:
  830.                 if not self._configDef[section][option]['short']:
  831.                     compound = self.__splice_compound(section, option)
  832.                     shortOptions = self.__shortMap.values()
  833.                     for i in range(0, len(option)):
  834.                         letter = option[i]
  835.                         letter = letter.lower()
  836.                         if letter in self.__alpha:
  837.                             if not letter in shortOptions and 
  838.                                 not letter in self.__reserved:
  839.                                 self.__shortMap[compound] = letter
  840.                                 break
  841.                     if not self.__shortMap.has_key(compound):
  842.                         for i in range(0, len(self.__alpha)):
  843.                             letter = self.__alpha[i]
  844.                             if not letter in shortOptions and 
  845.                                 not letter in self.__reserved:
  846.                                 self.__shortMap[compound] = letter
  847.     def __add_option(self, config, compoundOpt, section, option, group=None):
  848.         addMethod = self.add_option
  849.         if group: addMethod=group.add_option
  850.         
  851.         self.__compoundOpts.append(compoundOpt)
  852.         
  853.         if compoundOpt == 'gridservice-mapred.final-server-params' or 
  854.            compoundOpt == 'gridservice-hdfs.final-server-params' or 
  855.            compoundOpt == 'gridservice-mapred.server-params' or 
  856.            compoundOpt == 'gridservice-hdfs.server-params' or 
  857.            compoundOpt == 'hod.client-params':
  858.           _action = 'append'
  859.         elif config[section][option]['type'] == 'bool':
  860.           _action = 'store_true'
  861.         else:
  862.           _action = 'store'
  863.         if self.__shortMap.has_key(compoundOpt):
  864.           addMethod("-" + self.__shortMap[compoundOpt],
  865.                           "--" + compoundOpt, dest=compoundOpt, 
  866.                           action= _action, 
  867.                           metavar=config[section][option]['type'],
  868.                           default=config[section][option]['default'],
  869.                           help=config[section][option]['desc'])
  870.         else:
  871.           if config[section][option]['short']:
  872.             addMethod("-" + config[section][option]['short'], 
  873.                               "--" + compoundOpt, dest=compoundOpt, 
  874.                               action= _action,
  875.                               metavar=config[section][option]['type'],
  876.                               default=config[section][option]['default'],
  877.                               help=config[section][option]['desc'])   
  878.           else:
  879.             addMethod('', "--" + compoundOpt, dest=compoundOpt, 
  880.                               action= _action, 
  881.                               metavar=config[section][option]['type'],
  882.                               default=config[section][option]['default'],
  883.                               help=config[section][option]['desc'])   
  884.                            
  885.     def __add_options(self):
  886.         if self.__withConfig:
  887.             self.add_option("-c", "--config", dest='config', 
  888.                 action='store', default=self.__defaultConfig, 
  889.                 metavar='config_file',
  890.                 help="Full path to configuration file.")
  891.         self.add_option("", "--verbose-help", 
  892.             action='help', default=None, 
  893.             metavar='flag',
  894.             help="Display verbose help information.")
  895.         
  896.         self.add_option("-v", "--version", 
  897.             action='version', default=None, 
  898.             metavar='flag',
  899.             help="Display version information.")
  900.         
  901.         self.version = self.__version
  902.   
  903.         if len(self._mySections) > 1:
  904.             for section in self._mySections:
  905.                 group = OptionGroup(self, section)
  906.                 for option in self._configDef[section]:
  907.                     compoundOpt = self.__splice_compound(section, option)
  908.                     self.__add_option(self._configDef, compoundOpt, section, 
  909.                                       option, group)
  910.                 self.add_option_group(group)
  911.         else:
  912.             for section in self._mySections:
  913.                 for option in self._configDef[section]:
  914.                     compoundOpt = self.__splice_compound(section, option)
  915.                     self.__add_option(self._configDef, compoundOpt, section, 
  916.                                       option)
  917.                     
  918.     def __build_dict(self):
  919.         if self.__withConfig:
  920.             self._dict['config'] = str(getattr(self.__parsedOptions, 'config'))
  921.         for compoundOption in dir(self.__parsedOptions):
  922.             if compoundOption in self.__compoundOpts:
  923.                 (section, option) = self.__split_compound(compoundOption)
  924.                 if not self._dict.has_key(section):
  925.                     self._dict[section] = {}
  926.                 
  927.                 if getattr(self.__parsedOptions, compoundOption):
  928.                     _attr = getattr(self.__parsedOptions, compoundOption)
  929.                     # when we have multi-valued parameters passed separately
  930.                     # from command line, python optparser pushes them into a
  931.                     # list. So converting all such lists to strings
  932.                     if type(_attr) == type([]):
  933.                       import string
  934.                       _attr = string.join(_attr,',')
  935.                     self._dict[section][option] = _attr
  936.                     
  937.         for section in self._configDef:
  938.             for option in self._configDef[section]: 
  939.                 if self._configDef[section][option]['type'] == 'bool':
  940.                     compoundOption = self.__splice_compound(section, option)
  941.                     if not self._dict.has_key(section):
  942.                         self._dict[section] = {}
  943.                     
  944.                     if option not in self._dict[section]:
  945.                         self._dict[section][option] = False
  946.  
  947.     def __set_display_groups(self):
  948.         if not '--verbose-help' in sys.argv:
  949.             self.option_groups = self.__display_grps
  950.             self.option_list = self.__display_option_list
  951.             for group in self.option_groups:
  952.                 group.option_list = self.__display_grp_lists[group.title]
  953.  
  954.     def __unset_display_groups(self):
  955.         if not '--verbose-help' in sys.argv:
  956.             self.option_groups = self.__orig_grps
  957.             self.option_list = self.__orig_option_list
  958.             for group in self.option_groups:
  959.                 group.option_list = self.__orig_grp_lists[group.title]      
  960.  
  961.     def print_help(self, file=None):
  962.         self.__set_display_groups()
  963.         OptionParser.print_help(self, file)
  964.         self.__unset_display_groups()
  965.     def print_options(self):
  966.         _usage = self.usage
  967.         self.set_usage('')
  968.         self.print_help()
  969.         self.set_usage(_usage)
  970.                         
  971.     def verify(self):
  972.         return baseConfig.verify(self)
  973.     def replace_escape_seqs(self):
  974.       replace_escapes(self)