configurationengine/source/scripts/cone_tool.py
changeset 3 e7e0ae78773e
parent 0 2e8eeb919028
child 5 d2c80f5cab53
equal deleted inserted replaced
2:87cfa131b535 3:e7e0ae78773e
    40 
    40 
    41 ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
    41 ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
    42 
    42 
    43 import cone
    43 import cone
    44 import cone_subaction
    44 import cone_subaction
    45 from cone.public import settings
    45 import cone_common
       
    46 from cone.public import settings, utils
    46 
    47 
    47 CONE_SCRIPT_PATTERN = 'conesub_*.py'
       
    48 ROOT_PATH           = os.path.dirname(os.path.abspath(__file__))
       
    49 SUBS                = cone_subaction.get_subactions(ROOT_PATH, CONE_SCRIPT_PATTERN)
       
    50 ACTIONS             = [sub for sub in SUBS]
       
    51 logger              = logging.getLogger('cone')
    48 logger              = logging.getLogger('cone')
    52 VERSION             = cone.__version__
    49 VERSION             = cone.__version__
    53 if cone._svnrevision not in ("", "exported"):
    50 if cone._svnrevision not in ("", "exported"):
    54     VERSION += " (SVN %s)" % cone._svnrevision
    51     VERSION += " (SVN %s)" % cone._svnrevision
    55 CONE_USAGE          = "%prog [action] [options]."
    52 
    56 CONE_ACTIONS        = '\n'
    53 
    57 for act in ACTIONS:
    54 def format_actions(actions, filter=None):
    58      CONE_ACTIONS += '    %s\n' % act
    55     action_names = []
    59 CONE_ACTION_HELP    = "Available actions %s\nUse %%prog [action] -h to get action specific help." % CONE_ACTIONS
    56     for act in actions:
       
    57         if not filter or filter(act):
       
    58             action_names.append(act.name)
       
    59     
       
    60     action_names.sort()
       
    61     ret = ''
       
    62     for act in action_names:
       
    63         help =  actions[act].short_help()
       
    64         ret += '    %s : %s\n' % (act, help)
       
    65     return ret
       
    66 
       
    67 def get_cone_configs(paths):
       
    68     static_paths =  [os.path.expanduser('~'),
       
    69                      os.getcwd()]
       
    70     all_paths = [ROOT_PATH]
       
    71     all_paths += static_paths
       
    72     all_paths += paths
       
    73     
       
    74     configs = cone_subaction.get_cfg_files(all_paths, 'cone.ini')
       
    75     return configs
       
    76 
       
    77 def get_actions(configs):
       
    78     return cone_subaction.get_actions(configs)
       
    79 
       
    80 
       
    81 def get_help(actions):
       
    82     helpstr = \
       
    83 """
       
    84 Use %%prog [action] -h to get action specific help.
       
    85 
       
    86 Available actions 
       
    87 Main actions for one or more configurations. 
       
    88 %s
       
    89 
       
    90 Actions related to the configuration project maintenance. 
       
    91 %s
       
    92 
       
    93 extensions:
       
    94 %s
       
    95 """ % (format_actions(actions, lambda x: x.type=='configuration'),
       
    96        format_actions(actions, lambda x: x.type=='project'),
       
    97        format_actions(actions, lambda x: x.type=='extension'))
       
    98     return helpstr
    60 
    99 
    61 def main():
   100 def main():
    62     parser = OptionParser(usage="%s\n\n%s" % (CONE_USAGE,CONE_ACTION_HELP),
   101     # Get the operating system name to pass it on the the cmdsplit..
    63                           version="%%prog %s" % VERSION,
   102     os_name = os.name
    64                           prog="ConE")
   103     if os.getenv('CONE_CMDARG'):
       
   104         sys.argv = [sys.argv[0]]
       
   105         sys.argv += utils.cmdsplit(os.getenv('CONE_CMDARG'), os_name)
       
   106     if os.getenv('CONE_CMD_APPEND'):
       
   107         sys.argv.append( utils.cmdsplit(os.getenv('CONE_CMD_APPEND'), os_name) )
       
   108 
       
   109     CONE_USAGE = "%prog [action] [options]."
       
   110     configs = get_cone_configs([])
       
   111     actions = get_actions(configs)
    65     
   112     
    66     # Set the path for cone .cfg files to the same directory as this script
   113     # Set the path for cone .cfg files to the same directory as this script
    67     settings.SettingsFactory.configpath = ROOT_PATH
   114     settings.SettingsFactory.configpath = ROOT_PATH
    68     
   115     
    69     try:
   116     try:
    70         action = sys.argv[1]
   117         action = sys.argv[1]
    71         subaction = SUBS[action]
   118         subaction = actions[action]
    72         print "Running action %s" % subaction.name
   119         print "Running action %s" % subaction.name
    73     except (IndexError, KeyError):
   120     except (IndexError, KeyError):
       
   121         CONE_ACTION_HELP = get_help(actions)
       
   122         parser = OptionParser(usage="%s\n\n%s" % (CONE_USAGE,CONE_ACTION_HELP),
       
   123                               version="%%prog %s" % VERSION,
       
   124                               prog="ConE")
    74         (options, args) = parser.parse_args()
   125         (options, args) = parser.parse_args()
    75         parser.error("Action must be given! See --help.")
   126         parser.error("Action must be given! See --help.")
    76     
   127     
    77     subaction.run()
   128     subaction.run()
    78 
   129