configurationengine/source/plugins/symbian/ConeCRMLPlugin/CRMLPlugin/crml_impl.py
changeset 0 2e8eeb919028
child 3 e7e0ae78773e
equal deleted inserted replaced
-1:000000000000 0:2e8eeb919028
       
     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 logging
       
    20 
       
    21 from cone.public import exceptions, plugin, utils, api
       
    22 import crml_writer, crml_comparator
       
    23 from crml_model import *
       
    24 
       
    25 class CrmlImpl(plugin.ImplBase):
       
    26     IMPL_TYPE_ID = 'crml'
       
    27     
       
    28     RFS_RECORDS_LIST_VARNAME = 'crml_cenrep_rfs_records_list'
       
    29     RFS_TXT_GENERATED_VARNAME = 'crml_cenrep_rfs_txt_generated'
       
    30 
       
    31     def __init__(self, resource_ref, configuration, repository):
       
    32         plugin.ImplBase.__init__(self, resource_ref, configuration)
       
    33         self.resource_ref = resource_ref
       
    34         self.configuration = configuration
       
    35         self.logger = logging.getLogger('cone.crml(%s)' % self.resource_ref)
       
    36         self.repository = repository
       
    37         
       
    38     def generate(self, context=None):
       
    39         # Quick fix 
       
    40         if context:
       
    41             self.generation_context = context
       
    42         file_path = self._get_cenrep_txt_file_path()
       
    43         self.logger.debug("Generating file '%s'..." % file_path)
       
    44         
       
    45         # Generate CenRep text data and write it to the output file
       
    46         writer = crml_writer.CrmlTxtWriter(self.configuration, self.logger)
       
    47         data = writer.get_cenrep_txt_data(self.repository).encode('UTF-16')
       
    48         self._write_to_file(file_path, data)
       
    49         
       
    50         
       
    51         # Collect the record for cenrep_rfs.txt generation in post_generate()
       
    52         if self.generation_context is not None:
       
    53             rfs_record = writer.get_cenrep_rfs_record(self.repository)
       
    54             if rfs_record:
       
    55                 # Add the record to the dictionary
       
    56                 data_dict = self.generation_context.impl_data_dict
       
    57                 VARNAME = self.RFS_RECORDS_LIST_VARNAME
       
    58                 if VARNAME not in data_dict:
       
    59                     data_dict[VARNAME] = []
       
    60                 data_dict[VARNAME].append(rfs_record)
       
    61     
       
    62     def post_generate(self, context=None):
       
    63         # Quick fix 
       
    64         if context:
       
    65             self.generation_context = context
       
    66         if self._is_cenrep_rfs_txt_to_be_generated():
       
    67             # Generate CenRep RFS text file if not already generated
       
    68             data_dict = self.generation_context.impl_data_dict
       
    69             if self.RFS_TXT_GENERATED_VARNAME not in data_dict:
       
    70                 rfs_records = data_dict.get(self.RFS_RECORDS_LIST_VARNAME, [])
       
    71                 
       
    72                 file_path = self._get_cenrep_rfs_txt_file_path()
       
    73                 writer = crml_writer.CrmlTxtWriter(self.configuration, self.logger)
       
    74                 data = writer.get_cenrep_rfs_txt_data(rfs_records).encode('UTF-16')
       
    75                 self._write_to_file(file_path, data)
       
    76             
       
    77                 data_dict[self.RFS_TXT_GENERATED_VARNAME] = True
       
    78 
       
    79     def list_output_files(self):
       
    80         """
       
    81         Return a list of output files as an array. 
       
    82         """
       
    83         files = [self._get_cenrep_txt_file_path()]
       
    84         if self._is_cenrep_rfs_txt_to_be_generated():
       
    85             files.append(self._get_cenrep_rfs_txt_file_path())
       
    86         return files
       
    87 
       
    88     def get_refs(self):
       
    89         if self.repository is None:
       
    90             return []
       
    91         else:
       
    92             return self.repository.get_refs()
       
    93     
       
    94     def get_flat_comparison_id(self):
       
    95         return crml_comparator.CrmlComparator.get_flat_comparison_id(self.repository)
       
    96     
       
    97     def get_flat_comparison_extra_data(self):
       
    98         return crml_comparator.CrmlComparator.get_flat_comparison_extra_data(self.repository)
       
    99     
       
   100     @classmethod
       
   101     def get_flat_comparison_impl_type_id(cls):
       
   102         return 'crml'
       
   103     
       
   104     def flat_compare(self, other):
       
   105         comparator = crml_comparator.CrmlComparator(self.resource_ref, self.repository)
       
   106         return comparator.flat_compare(other.resource_ref, other.repository)
       
   107     
       
   108     def _get_cenrep_txt_file_path(self):
       
   109         """
       
   110         Return the full path to the CenRep text file generated by this implementation
       
   111         """
       
   112         uid = self.repository.uid_value
       
   113         if uid.startswith('0x'):    uid = uid[2:]
       
   114         return os.path.normpath(os.path.join(self.output, uid + '.txt'))
       
   115     
       
   116     def _get_cenrep_rfs_txt_file_path(self):
       
   117         """
       
   118         Return the full path to the CenRep RFS text file
       
   119         """
       
   120         # cenrep_rfs.txt goes to a different place than the rest of
       
   121         # the CenRep files, so temporarily override plugin_output
       
   122         # for that purpose
       
   123         orig_pluginoutput = self.plugin_output
       
   124         self.plugin_output = 'private/100059C9'
       
   125         rfs_txt_path = os.path.normpath(os.path.join(self.output, 'cenrep_rfs.txt'))
       
   126         self.plugin_output = orig_pluginoutput
       
   127         return rfs_txt_path
       
   128     
       
   129     def _is_cenrep_rfs_txt_to_be_generated(self):
       
   130         """
       
   131         Return whether the CenRep RFS text file is to be generated.
       
   132         """
       
   133         if self.generation_context is None:
       
   134             return False
       
   135         
       
   136         targets = self.generation_context.tags.get('target', [])
       
   137         return 'core' in targets or 'rofs2' in targets
       
   138     
       
   139     def _write_to_file(self, file_path, data):
       
   140         # Create directories for the file if necessary
       
   141         file_dir = os.path.dirname(file_path)
       
   142         if file_dir != '' and not os.path.exists(file_dir):
       
   143             os.makedirs(file_dir)
       
   144         
       
   145         # Write data
       
   146         f = open(file_path, "wb")
       
   147         try:        f.write(data)
       
   148         finally:    f.close()