configurationengine/source/scripts/cone_subaction.py
changeset 3 e7e0ae78773e
parent 0 2e8eeb919028
equal deleted inserted replaced
2:87cfa131b535 3:e7e0ae78773e
    17 import os
    17 import os
    18 import sys
    18 import sys
    19 import fnmatch 
    19 import fnmatch 
    20 import re
    20 import re
    21 import logging
    21 import logging
       
    22 import ConfigParser
    22 
    23 
       
    24 
       
    25 def get_cfg_files(paths, filename):
       
    26     """ Find out the cone subscripts that are present in the"""
       
    27     files = []
       
    28     for path in paths:
       
    29         cfgfile = os.path.join(path, filename)
       
    30         if os.path.exists(cfgfile):
       
    31             files.append(cfgfile)
       
    32     return files
       
    33 
       
    34 def get_config(cfgfiles):
       
    35     config = ConfigParser.ConfigParser()
       
    36     config.read(cfgfiles)
       
    37     return config
       
    38 
       
    39 def get_actions(cfgfiles):
       
    40     """ Find out the cone subscripts that are present in the"""
       
    41     subacts = ActionContainer()
       
    42     config = get_config(cfgfiles)
       
    43     actions_section = 'actions'
       
    44     if not config.has_section(actions_section):
       
    45         raise Exception('The cone.ini does not have any %s section.' % actions_section)
       
    46     for (name, section) in config.items(actions_section):
       
    47         subacts += get_subactions_from_configs(cfgfiles, section)     
       
    48     return subacts
       
    49 
       
    50 def get_subactions_from_configs(cfgfiles, section):
       
    51     """ Find out the cone subscripts that are present in the"""
       
    52     subacts = ActionContainer()
       
    53     config = get_config(cfgfiles)
       
    54     paths = [os.path.dirname(cfgfile) for cfgfile in cfgfiles]
       
    55     if not config.has_section(section):
       
    56         raise Exception('The cone.ini does not have any %s section.' % section)
       
    57     for (commandname, path) in config.items(section):
       
    58         subacts.append(ConeAction(commandname, path, type=section, paths=paths))
       
    59     
       
    60     return subacts
    23 
    61 
    24 def get_subactions(path,pattern):
    62 def get_subactions(path,pattern):
    25     """ Find out the cone subscripts that are present in the ROOT_PATH """
    63     """ Find out the cone subscripts that are present in the ROOT_PATH """
    26     subacts = ActionContainer()
    64     subacts = ActionContainer()
    27     for file in os.listdir(path):
    65     for file in os.listdir(path):
    40     elif level == 3 : return logging.WARNING
    78     elif level == 3 : return logging.WARNING
    41     elif level == 4 : return logging.INFO
    79     elif level == 4 : return logging.INFO
    42     elif level == 5 : return logging.DEBUG
    80     elif level == 5 : return logging.DEBUG
    43     else : return logging.NOTSET
    81     else : return logging.NOTSET
    44 
    82 
    45 class ActionContainer(object):
    83 class ActionContainer(list):
    46     def __init__(self):
    84     pass
    47         self._actions = {}
       
    48 
    85 
    49     def __len__(self):
    86 #    def __init__(self):
    50         return len(self._actions)
    87 #        self._actions = {}
       
    88 #
       
    89 #    def __len__(self):
       
    90 #        return len(self._actions)
       
    91 #
       
    92     def __getitem__(self, key):
       
    93         for item in self:
       
    94             if item.name == key:
       
    95                 return item
       
    96         raise KeyError('Key %s not found in %s'  % (key, self))
       
    97 #
       
    98 #    def __setitem__( self, key, value):
       
    99 #        self._actions[key] = value
       
   100 #
       
   101 #    def __delitem__( self, key):
       
   102 #        del self._actions[key]
       
   103 #
       
   104 #    def __iter__( self):
       
   105 #        return self._actions.__iter__()
    51 
   106 
    52     def __getitem__(self, key):
   107 class ConeAction(object):
    53         return self._actions[key]
   108     def __init__(self, name, path, **kwargs):
       
   109         self.name = name
       
   110         self.path = path
       
   111         self.type = kwargs.get('type', '')
       
   112         self.paths = kwargs.get('paths', [])
       
   113         self._module = None
    54 
   114 
    55     def __setitem__( self, key, value):
   115     @property
    56         self._actions[key] = value
   116     def module_name(self):
       
   117         return os.path.basename(self.path)
    57 
   118 
    58     def __delitem__( self, key):
   119     @property
    59         del self._actions[key]
   120     def module_path(self):
       
   121         return os.path.dirname(self.path)
    60 
   122 
    61     def __iter__( self):
   123     @property
    62         return self._actions.__iter__()
   124     def module(self):
       
   125         if not self._module:
       
   126             paths = [os.path.join(pth, self.module_path) for pth in self.paths]
       
   127             paths.append(self.module_path)
       
   128             sys.path += paths
       
   129             try:
       
   130                 self._module  = __import__(self.module_name)
       
   131             finally:
       
   132                 del sys.path[-len(paths):]
       
   133         return self._module
       
   134     
       
   135     def short_help(self):
       
   136         if hasattr(self.module, 'short_help'):
       
   137             return self.module.short_help
       
   138         elif hasattr(self.module, 'main'):
       
   139             helpstr = self.module.main.__doc__ or '<None>'
       
   140             return helpstr.replace('\n', '').strip(' ')[:50]
       
   141         else:
       
   142             return 'Not a valid module!!'
       
   143         
       
   144     def run(self):
       
   145         self.module.main()
       
   146 
    63 
   147 
    64 class SubAction(object):
   148 class SubAction(object):
    65     def __init__(self, scriptname, pattern):
   149     def __init__(self, scriptname, pattern):
    66         self._scriptname = scriptname
   150         self._scriptname = scriptname
    67         self._pattern = pattern
   151         self._pattern = pattern