Package archive :: Module selectors |
[hide private]
[frames] | no frames]

# Source Code for Module archive.selectors

      1  #============================================================================
      2  #Name        : selectors.py
      3  #Part of     : Helium
      4
      5  #Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
      6  #All rights reserved.
      7  #This component and the accompanying materials are made available
      8  #under the terms of the License "Eclipse Public License v1.0"
      9  #which accompanies this distribution, and is available
     10  #at the URL "http://www.eclipse.org/legal/epl-v10.html".
     11  #
     12  #Initial Contributors:
     13  #Nokia Corporation - initial contribution.
     14  #
     15  #Contributors:
     16  #
     17  #Description:
     18  #===============================================================================
     19
     20  import re
     21  import os
     22  import sys
     23  import logging
     24  import fileutils
     25  import archive
     26
     27  # Getting logger for the module
     28  logger = logging.getLogger("archive.selectors")
     29
     30
     31 -class DistributionPolicySelector:
     32      """ A selector that selects files based on other criteria.
     33
     34      It is similar to the Ant file selector objects in design. This one selects files
     35      based on whether the root-most Distribution.Policy.S60 file matches the given value.
     36      """
     37
     38 -    def __init__(self, policy_files, value, ignoremissingpolicyfiles=False):
     39          """ Initialization. """
     40          self._negate = False
     41          self.values = [v.strip() for v in value.split() if v.strip()!=""]
     42          self._policy_files = policy_files
     43          self._ignoremissingpolicyfiles = ignoremissingpolicyfiles
     44
     45 -    def get_value_and_negate(self, value):
     46          if value.startswith('!'):
     47              return (value[1:], True)
     48          return (value, False)
     49
     50 -    def is_selected(self, path):
     51          """ Determines if the path is selected by this selector. """
     52          current_dir = os.path.abspath(os.path.dirname(path))
     53          logger.debug('is_selected: current dir = ' + current_dir + '  ' + str(os.path.exists(current_dir)))
     54          result = False
     55          policy_file = None
     56          # finding the distribution policy from the filelist.
     57          for filename in self._policy_files:
     58              #slow method on case sensitive system
     59              if sys.platform != 'win32':
     60                  for f in os.listdir(current_dir):
     61                      if f.lower() == filename.lower():
     62                          policy_file = os.path.join(current_dir, f)
     63                          break
     64              elif os.path.exists(os.path.join(current_dir, filename)):
     65                  policy_file = os.path.join(current_dir, filename)
     66                  logger.debug('Using Policy file: ' + policy_file)
     67                  break
     68
     69          policy_value = None
     70          if policy_file is None:
     71              if not self._ignoremissingpolicyfiles:
     72                  logger.error("POLICY_ERROR: Policy file not found under '%s' using names [%s]" % (current_dir, ", ".join(self._policy_files)))
     73              policy_value = archive.mappers.MISSING_POLICY
     74          else:
     75              try:
     76                  policy_value = fileutils.read_policy_content(policy_file)
     77              except Exception:
     78                  logger.warning('POLICY_ERROR: Exception thrown parsing policy file: ' + policy_file)
     79                  policy_value = archive.mappers.MISSING_POLICY
     80          # loop through the possible values
     81          for value in self.values:
     82              (val, negate) = self.get_value_and_negate(value)
     83              logger.debug('Policy value: ' + str(policy_value) + '  ' + val)
     84              if (not negate and policy_value == val) or (negate and policy_value != val):
     85                  return True
     86          return False
     87
     88
     89 -class SymbianPolicySelector:
     90      """ A selector that selects files based on other criteria.
     91
     92      It is similar to the Ant file selector objects in design. This one selects files
     93      based on whether the root-most distribution.policy file matches the given value.
     94      """
     95
     96 -    def __init__(self, policy_files, value):
     97          """ Initialization. """
     98          self._negate = False
     99          self.values = [v.strip() for v in value.split() if v.strip()!=""]
    100          self._policy_files = policy_files
    101
    102
    103 -    def get_value_and_negate(self, value):
    104          if value.startswith('!'):
    105              return (value[1:], True)
    106          return (value, False)
    107
    108 -    def is_selected(self, path):
    109          """ Determines if the path is selected by this selector. """
    110          current_dir = os.path.abspath(os.path.dirname(path))
    111          logger.debug('is_selected: current dir = ' + current_dir + '  ' + str(os.path.exists(current_dir)))
    112          result = False
    113          policy_file = None
    114          # finding the distribution policy from the filelist.
    115          for filename in self._policy_files:
    116              if sys.platform != 'win32':
    117                  for f in os.listdir(current_dir):
    118                      if f.lower() == filename.lower():
    119                          policy_file = os.path.join(current_dir, f)
    120                          logger.debug('Using Policy file: ' + policy_file)
    121                          break
    122              elif os.path.exists(os.path.join(current_dir, filename)):
    123                  policy_file = os.path.join(current_dir, filename)
    124                  logger.debug('Using Policy file: ' + policy_file)
    125                  break
    126
    127          policy_value = None
    128          if policy_file is  None:
    129              logger.error("POLICY_ERROR: Policy file not found under '%s' using names [%s]" % (current_dir, ", ".join(self._policy_files)))
    130              policy_value = archive.mappers.MISSING_POLICY
    131          else:
    132              try:
    133                  policy_value = fileutils.read_symbian_policy_content(policy_file)
    134              except Exception:
    135                  logger.warning('POLICY_ERROR: Exception thrown parsing policy file: ' + policy_file)
    136                  policy_value = archive.mappers.MISSING_POLICY
    137          # loop through the possible values
    138          for value in self.values:
    139              (val, negate) = self.get_value_and_negate(value)
    140              logger.debug('Policy value: ' + str(policy_value) + '  ' + val)
    141              if (not negate and policy_value == val) or (negate and policy_value != val):
    142                  return True
    143          return False
    144
    145  SELECTORS = {'policy': lambda config: DistributionPolicySelector(config.get_list('policy.filenames', ['Distribution.Policy.S60']), config['policy.value']),
    146                 'symbian.policy': lambda config: SymbianPolicySelector(config.get_list('policy.filenames', ['distribution.policy']), config['policy.value']),
    147               'distribution.policy.s60': lambda config: DistributionPolicySelector(['Distribution.Policy.S60'], config['distribution.policy.s60'], config['ignore.missing.policyfiles'] == 'true'),
    148               }
    149
    150 -def get_selector(name, config):
    151      if not 'ignore.missing.policyfiles' in config:
    152          config['ignore.missing.policyfiles'] = 'false'
    153      return SELECTORS[name](config)
    154