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