configurationengine/source/scripts/conesub_update.py
changeset 3 e7e0ae78773e
parent 0 2e8eeb919028
equal deleted inserted replaced
2:87cfa131b535 3:e7e0ae78773e
    19 import fnmatch
    19 import fnmatch
    20 import logging
    20 import logging
    21 from optparse import OptionParser, OptionGroup
    21 from optparse import OptionParser, OptionGroup
    22 
    22 
    23 import cone_common
    23 import cone_common
    24 from cone.public import api, plugin, utils, exceptions
    24 from cone.public import api, plugin, utils, exceptions, container
    25 
    25 
    26 
    26 
    27 VERSION     = '1.0'
    27 VERSION     = '1.0'
    28 DEFAULT_EXT = '.cpf'
    28 DEFAULT_EXT = '.cpf'
    29 CPF_NAMESPACE  = "http://www.nokia.com/xml/cpf-id/1"
    29 CPF_NAMESPACE  = "http://www.nokia.com/xml/cpf-id/1"
    30 CPF_META_TAG = "configuration-property"
    30 CPF_META_TAG = "configuration-property"
    31 
    31 
    32 logger    = logging.getLogger('cone')
    32 logger    = logging.getLogger('cone')
    33 
    33 
    34 def main():
    34 def main():
       
    35     """ Update/set values to features in configuration(s). """
    35     parser = OptionParser(version="%%prog %s" % VERSION)
    36     parser = OptionParser(version="%%prog %s" % VERSION)
    36     
    37     
    37     parser.add_options(cone_common.COMMON_OPTIONS)
    38     parser.add_options(cone_common.COMMON_OPTIONS)
    38     
    39     
    39     parser.add_option("-c", "--configuration",
    40     parser.add_option("-c", "--configuration",
   118                    type="string",
   119                    type="string",
   119                    help="Add given ConfML data to defined configuration."\
   120                    help="Add given ConfML data to defined configuration."\
   120                         "Example --add-data \"KCRUidAvkon.KAknDefaultAppOrientation=1\"",
   121                         "Example --add-data \"KCRUidAvkon.KAknDefaultAppOrientation=1\"",
   121                    default=None) 
   122                    default=None) 
   122 
   123 
       
   124     group.add_option("--add-configuration",\
       
   125                    dest="configurations",\
       
   126                    action="append",
       
   127                    type="string",
       
   128                    help="Include given configuration inside the updated configuration."\
       
   129                         "Example --add-configuration \"test_configuration.confml\"",
       
   130                    default=None) 
       
   131 
       
   132     group.add_option("--add-policy",\
       
   133                    dest="policy",\
       
   134                    type="string",
       
   135                    help="Define the data update policy, which can be append|prepend|replace."\
       
   136                         "Example --add-configuration \"test_configuration.confml\" --add-policy=prepend"\
       
   137                         "This would add the configuration as the first include to the target configuration.",
       
   138                    default='append') 
       
   139 
   123     parser.add_option_group(group)
   140     parser.add_option_group(group)
   124     (options, args) = parser.parse_args()
   141     (options, args) = parser.parse_args()
   125     
   142     
   126     cone_common.handle_common_options(options)
   143     cone_common.handle_common_options(options)
   127     
   144     
   128     # Open the project and find out the active configuration
   145     # Open the project and find out the active configuration
   129     project = api.Project(api.Storage.open(options.project, "a"))
   146     project = api.Project(api.Storage.open(options.project, "a", username=options.username, password=options.password))
   130     try:
   147     try:
   131         active_root = project.get_storage().get_active_configuration()
   148         active_root = project.get_storage().get_active_configuration()
   132     except AttributeError:
   149     except AttributeError:
   133         active_root = None
   150         active_root = None
   134     
   151     
   165         
   182         
   166         # Handle metadata and data additions
   183         # Handle metadata and data additions
   167         if added_meta:      _add_meta(config, added_meta)
   184         if added_meta:      _add_meta(config, added_meta)
   168         if added_cpf_meta:  _add_cpf_meta(config, added_cpf_meta)
   185         if added_cpf_meta:  _add_cpf_meta(config, added_cpf_meta)
   169         if added_data:      _add_data(config, added_data)
   186         if added_data:      _add_data(config, added_data)
       
   187         
       
   188         # Handle configuration additions
       
   189         for configinc in options.configurations or []:
       
   190             config.include_configuration(configinc, parse_policy(options.policy))
   170           
   191           
   171         # Handle description  
   192         # Handle description  
   172         if options.desc:        
   193         if options.desc:        
   173             logger.info("Setting description to %s" % options.desc)
   194             logger.info("Setting description to %s" % options.desc)
   174             config.desc = options.desc
   195             config.desc = options.desc
   213         if mo:
   234         if mo:
   214             name = mo.group(1)
   235             name = mo.group(1)
   215             value = mo.group(2)
   236             value = mo.group(2)
   216             result[name] = value
   237             result[name] = value
   217         else:
   238         else:
   218             logger.error("Illegal %s definition: %s" % (entry_name, entry))
   239             logger.error("Illegal %s definition: %s" % (entry_type_name, entry))
   219     return result
   240     return result
   220 
   241 
   221 def _add_meta(config, added_meta):
   242 def _add_meta(config, added_meta):
   222     if not config.meta:
   243     if not config.meta:
   223         config.meta = []
   244         config.meta = []
   255                 logger.info("Set %s=%s" % (ref, repr(value)))
   276                 logger.info("Set %s=%s" % (ref, repr(value)))
   256         else:
   277         else:
   257             config.get_default_view().get_feature(ref).set_value(value)
   278             config.get_default_view().get_feature(ref).set_value(value)
   258             logger.info("Set %s=%s" % (ref, value))
   279             logger.info("Set %s=%s" % (ref, value))
   259 
   280 
       
   281 def parse_policy(policy_str):
       
   282     """
       
   283     >>> parse_policy('replace')
       
   284     0
       
   285     >>> parse_policy('append')
       
   286     1
       
   287     >>> parse_policy('prepend')
       
   288     2
       
   289     """
       
   290     if policy_str == 'append':
       
   291         return container.APPEND
       
   292     elif policy_str == 'replace':
       
   293         return container.REPLACE
       
   294     elif policy_str == 'prepend':
       
   295         return container.PREPEND
       
   296     else:
       
   297         raise Exception('Could not parse policy string! %s' % policy_str)
       
   298 
   260 if __name__ == "__main__":
   299 if __name__ == "__main__":
   261     main()
   300     main()
   262 
   301     
   263 
   302     
   264 
   303 
       
   304 
       
   305