configurationengine/source/cone/action/fix.py
changeset 3 e7e0ae78773e
equal deleted inserted replaced
2:87cfa131b535 3:e7e0ae78773e
       
     1 
       
     2 #
       
     3 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 # All rights reserved.
       
     5 # This component and the accompanying materials are made available
       
     6 # under the terms of "Eclipse Public License v1.0"
       
     7 # which accompanies this distribution, and is available
       
     8 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9 #
       
    10 # Initial Contributors:
       
    11 # Nokia Corporation - initial contribution.
       
    12 #
       
    13 # Contributors:
       
    14 #
       
    15 # Description: 
       
    16 #
       
    17 
       
    18 """
       
    19 Test the configuration
       
    20 """
       
    21 import logging
       
    22 
       
    23 from cone.public import api, exceptions
       
    24 from cone.validation import confmlvalidation
       
    25 from cone.validation.problem_type_filter import ProblemTypeFilter
       
    26 
       
    27 logger    = logging.getLogger('cone')
       
    28 
       
    29 class ConeFixAction(object):
       
    30     def     __init__(self, **kwargs):
       
    31         # all action attributes are given as keyword arguments
       
    32         self.include_filter = kwargs.get('include_filter',[])
       
    33         self.exclude_filter = kwargs.get('exclude_filter',[])
       
    34         self.username = kwargs.get('username',None)
       
    35         self.password = kwargs.get('password',None)
       
    36         self.project_name = kwargs.get('project_name', '.')
       
    37         self.configuration_name = kwargs.get('configuration_name', None)
       
    38         self.project = kwargs.get('project', None)
       
    39         self.config = kwargs.get('configuration', None)
       
    40         
       
    41         
       
    42     def run(self):
       
    43         filter = ProblemTypeFilter(includes = self.include_filter,
       
    44                                    excludes = self.exclude_filter)
       
    45         
       
    46         # Open the project if it is not already opened
       
    47         if self.project == None:
       
    48             storage = api.Storage.open(self.project_name, 
       
    49                                        "a", 
       
    50                                        username=self.username, 
       
    51                                        password=self.password)
       
    52             self.project = api.Project(storage)
       
    53         if self.config == None:
       
    54             if not self.configuration_name:
       
    55                 logging.getLogger('cone').error("No configuration given! Use -c / --configuration")
       
    56                 return False
       
    57             try:
       
    58                 self.config  = self.project.get_configuration(self.configuration_name)
       
    59             except exceptions.NotFound:
       
    60                 logging.getLogger('cone').error("No such configuration: %s" % self.configuration_name)
       
    61                 return False
       
    62         
       
    63         fixers = confmlvalidation.get_fixer_classes(filter)
       
    64         
       
    65         vc = confmlvalidation.fix_configuration(self.config, None, fixers)
       
    66         
       
    67         if vc.fixes:
       
    68             print "Fixed %d problem(s). See log for details" % len(vc.fixes)
       
    69         else:
       
    70             print "No problems to fix found. See log for details"
       
    71         return True
       
    72 
       
    73     def save(self):
       
    74         self.project.save()
       
    75         
       
    76     def close(self):
       
    77         self.project.close()
       
    78 
       
    79 
       
    80 def get_class():
       
    81     return ConeFixAction