configurationengine/source/dev-tools/deprfea.py
changeset 3 e7e0ae78773e
equal deleted inserted replaced
2:87cfa131b535 3:e7e0ae78773e
       
     1 #
       
     2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description:
       
    15 #
       
    16 
       
    17 import os
       
    18 import sys
       
    19 import re
       
    20 import shutil
       
    21 from optparse import OptionParser
       
    22 
       
    23 ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
       
    24 sys.path.append(os.path.join(ROOT_PATH, '..'))
       
    25 if sys.version_info[0] == 2 and (sys.version_info[1] == 5 or sys.version_info[1] == 6):
       
    26     cone_basedir = os.path.join(ROOT_PATH, 'configurationengine', 
       
    27                                  'win', '%s.%s' % (sys.version_info[0], sys.version_info[1]))
       
    28     cone_scriptdir = os.path.join(cone_basedir, 'scripts')
       
    29     cone_libdir = os.path.join(cone_basedir, 'lib')
       
    30     sys.path.append(cone_basedir)
       
    31     sys.path.append(cone_scriptdir)
       
    32     sys.path.append(cone_libdir)
       
    33 else:
       
    34     print 'You are using an unsupported Python version: %s.%s' % (sys.version_info[0], sys.version_info[1])
       
    35     sys.exit(1)
       
    36 
       
    37 print sys.path
       
    38 print os.getenv('PATH')
       
    39     
       
    40 try:
       
    41     import scripts.cone_common
       
    42 except:
       
    43     import cone_common
       
    44 from cone.public import api, plugin, utils, exceptions
       
    45 from cone.storage.filestorage import FileStorage
       
    46 
       
    47 CARBON_PROJECT_URL = 'http://carbon.nokia.com/extapi'
       
    48 
       
    49 CONFIGS_FILE = os.path.join(ROOT_PATH, 'configs.txt')
       
    50 ALL_FEATURES_FILE = os.path.join(ROOT_PATH, 'all_features_and_values.txt')
       
    51 REPORT_FILE = os.path.join(ROOT_PATH, 'deprecated_features.txt')
       
    52 EXPORT_STORAGE = os.path.join(ROOT_PATH, 'exported')
       
    53 
       
    54 
       
    55 def get_list_of_configurations_from_carbon(carbon_prj):
       
    56     config_list = carbon_prj.list_configurations()
       
    57     config_list.sort()
       
    58     return config_list
       
    59 
       
    60 def filter_configurations_from_file(cfilter=""):
       
    61     config_list = []
       
    62     fh = open(CONFIGS_FILE, 'r')
       
    63     config_list = fh.readlines()
       
    64     fh.close()
       
    65     if cfilter:
       
    66         return [elem.strip() for elem in config_list if match_filter(cfilter, elem)]
       
    67     else: 
       
    68         return config_list
       
    69 
       
    70 def check_deprecated_features(config, depr_features):
       
    71     default_view = config.get_default_view()
       
    72     f = open(ALL_FEATURES_FILE, 'a')
       
    73     f.write('\n\n### %s ###\n\n' % config.get_name())
       
    74     for fea_ref in default_view.list_all_features():
       
    75         feature = default_view.get_feature(fea_ref)
       
    76         # If feature has subfeatures, skip it
       
    77         if len(feature.list_features()) > 0:
       
    78             f.write('%-15s # %s\n' % ('Has subs', fea_ref))
       
    79             continue
       
    80         fea_value = default_view.get_feature(fea_ref).get_value()
       
    81         f.write('%-15s # %s\n' % (fea_value, fea_ref))  
       
    82         # If the value is None and it is not on the list yet, append it to deprecated features
       
    83         if fea_value == None and depr_features.count(fea_ref) == 0:
       
    84             depr_features.append(fea_ref)
       
    85         # If the value is something else and the feature is on the list, remove it
       
    86         elif fea_value != None and depr_features.count(fea_ref) != 0:
       
    87             depr_features.remove(fea_ref)
       
    88     f.close()
       
    89     return depr_features
       
    90 
       
    91 def save_report(depr_features):
       
    92     fh = open(REPORT_FILE, 'w')
       
    93     try: [fh.write(df + '\n') for df in depr_features]
       
    94     finally: fh.close()
       
    95     
       
    96 
       
    97 def match_filter(cfilter, element):
       
    98     filters = cfilter.split(';')
       
    99     for f in filters:
       
   100         if f.strip().lower() == element.strip().lower():
       
   101             return True
       
   102         if re.match('.*' + f.strip().lower() + '.*', element.strip().lower()):
       
   103             return True
       
   104     return False
       
   105 
       
   106 def create_options():
       
   107     #parser = OptionParser(usage="Sumthin")
       
   108     parser = OptionParser()
       
   109     parser.add_option("-f", "--filter",
       
   110                       action="store",
       
   111                       dest="filter",
       
   112                       help="Filter configurations. Multiple filters can be given, separated by \';\'. E.g. -f \"\(Vasco 01\);\(Vasco 06\)\"",
       
   113                       metavar="REGEX",
       
   114                       default="")
       
   115     parser.add_option("-l", "--list-configurations",
       
   116                       action="store_true",
       
   117                       dest="list_configs",
       
   118                       help="Only list available configurations in Carbon. When used with the -f option, preview the configurations which would be fetched from Carbon.",
       
   119                       default=False)
       
   120     parser.add_option("--force-carbon",
       
   121                       action="store_true",
       
   122                       dest="force_carbon",
       
   123                       help="Get configurations from Carbon even if they have already been fetched.",
       
   124                       default=False)
       
   125     return parser
       
   126 
       
   127 def main():
       
   128     parser = create_options()
       
   129     (options, args) = parser.parse_args()
       
   130     configs = []
       
   131     carbon_prj = None
       
   132     local_prj = None
       
   133     
       
   134     if options.filter == "":
       
   135         selection = raw_input('No filter given! ALL the configs in Carbon will be fetched and it will take a loooong time. Are you ABSOLUTELY sure you want to continue (y/n)? ')
       
   136         if selection.lower() != 'y':
       
   137             print '\nGood choice :)'
       
   138             return 0
       
   139         else:
       
   140             print '\nOk...\n'
       
   141     
       
   142     try:
       
   143         os.remove(ALL_FEATURES_FILE)
       
   144     except Exception, e:
       
   145         pass
       
   146     
       
   147     print '\nOpening project in Carbon (%s)...' % CARBON_PROJECT_URL
       
   148     try:
       
   149         carbon_prj = api.Project(api.Storage.open(CARBON_PROJECT_URL,"r"))
       
   150     except Exception, e:
       
   151         print 'Unable to open Carbon project. %s' % e
       
   152         return 1
       
   153     
       
   154     if os.path.exists(EXPORT_STORAGE):
       
   155         print '\nOpening project on local disk (%s)...' % EXPORT_STORAGE
       
   156         try:
       
   157             local_prj = api.Project(api.Storage.open(EXPORT_STORAGE, 'r'))
       
   158         except Exception, e:
       
   159             print 'Unable to open local project. %s' % e
       
   160             return 1
       
   161     
       
   162     # Force script to get everything from Carbon again
       
   163     if options.force_carbon:
       
   164         try:
       
   165             os.remove(CONFIGS_FILE)
       
   166             shutil.rmtree(EXPORT_STORAGE, ignore_errors=True)
       
   167         except:
       
   168             pass
       
   169     
       
   170     # Only get available configs and exit
       
   171     if options.list_configs:
       
   172         print 'Getting available configurations from Carbon (%s)' % CARBON_PROJECT_URL
       
   173         configs = get_list_of_configurations_from_carbon(carbon_prj)
       
   174         print 'Saving configs to %s...' % CONFIGS_FILE
       
   175         fh = open(CONFIGS_FILE, 'w')
       
   176         try: [fh.write('%s\n' % c) for c in configs]
       
   177         finally: fh.close()
       
   178         print 'Filtered configs: '
       
   179         configs = filter_configurations_from_file(options.filter)
       
   180         for elem in configs: print elem.strip()
       
   181         return 0
       
   182         
       
   183         
       
   184     if not os.path.exists(CONFIGS_FILE):
       
   185         print 'Configurations file not found. Getting list of configurations in project...'
       
   186         configs = get_list_of_configurations_from_carbon(carbon_prj)
       
   187         print 'Saving configs to %s...' % CONFIGS_FILE
       
   188         fh = open(CONFIGS_FILE, 'w')
       
   189         try: [fh.write('%s\n' % c) for c in configs]
       
   190         finally: fh.close()
       
   191     
       
   192     print '\nFilter wanted configurations from file: '   
       
   193     configs = filter_configurations_from_file(options.filter)
       
   194     for elem in configs: print elem.strip()
       
   195     
       
   196     depr_features = []
       
   197     
       
   198     for c in configs:
       
   199         config_name = c.strip()
       
   200         # Configuration has not been exported yet
       
   201         if not os.path.exists(os.path.join(EXPORT_STORAGE, config_name)):
       
   202             print '\nExport configuration %s from Carbon' % config_name
       
   203             if not carbon_prj: 
       
   204                 carbon_prj = api.Project(api.Storage.open(CARBON_PROJECT_URL,"r"))
       
   205             try:
       
   206                 config = carbon_prj.get_configuration(config_name)
       
   207                 carbon_prj.export_configuration(config, FileStorage(EXPORT_STORAGE, 'w'))
       
   208             except:
       
   209                 print 'Unable to export %s' % config_name
       
   210                 continue
       
   211         print '\nOpen configuration %s in project %s' % (config_name, EXPORT_STORAGE)
       
   212         if not local_prj:
       
   213             try:
       
   214                 local_prj = api.Project(api.Storage.open(EXPORT_STORAGE, 'r'))
       
   215             except Exception, e:
       
   216                 print 'Unable to open local project. %s' % e
       
   217                 if carbon_prj: carbon_prj.close()
       
   218                 return 1
       
   219         config = local_prj.get_configuration(c.strip())
       
   220         depr_features = check_deprecated_features(config, depr_features)
       
   221         
       
   222     if local_prj: local_prj.close()
       
   223     if carbon_prj: carbon_prj.close()
       
   224     
       
   225     save_report(depr_features)
       
   226     
       
   227     return 0
       
   228 
       
   229 if __name__ == '__main__':
       
   230     sys.exit(main())