587
|
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 |
"""selectors for things like distribution policy"""
|
|
20 |
|
|
21 |
import os
|
|
22 |
import logging
|
|
23 |
import fileutils
|
|
24 |
import archive
|
|
25 |
|
|
26 |
# Getting logger for the module
|
|
27 |
_logger = logging.getLogger("archive.selectors")
|
|
28 |
|
|
29 |
|
|
30 |
class DistributionPolicySelector:
|
|
31 |
""" A selector that selects files based on other criteria.
|
|
32 |
|
|
33 |
It is similar to the Ant file selector objects in design. This one selects files
|
|
34 |
based on whether the root-most Distribution.Policy.S60 file matches the given value.
|
|
35 |
"""
|
|
36 |
|
|
37 |
def __init__(self, policy_files, value, ignoremissingpolicyfiles=False):
|
|
38 |
""" Initialization. """
|
|
39 |
self._negate = False
|
|
40 |
self.values = [v.strip() for v in value.split() if v.strip()!=""]
|
|
41 |
self._policy_files = policy_files
|
|
42 |
self._ignoremissingpolicyfiles = ignoremissingpolicyfiles
|
|
43 |
|
|
44 |
def get_value_and_negate(self, value):
|
|
45 |
"""get value and negate it"""
|
|
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 |
policy_file = None
|
|
55 |
# finding the distribution policy from the filelist.
|
|
56 |
for filename in self._policy_files:
|
|
57 |
#slow method on case sensitive system
|
|
58 |
if os.sep != '\\':
|
|
59 |
for f_dir in os.listdir(current_dir):
|
|
60 |
if f_dir.lower() == filename.lower():
|
|
61 |
policy_file = os.path.join(current_dir, f_dir)
|
|
62 |
break
|
|
63 |
elif os.path.exists(os.path.join(current_dir, filename)):
|
|
64 |
policy_file = os.path.join(current_dir, filename)
|
|
65 |
_logger.debug('Using Policy file: ' + policy_file)
|
|
66 |
break
|
|
67 |
|
|
68 |
policy_value = None
|
|
69 |
if policy_file is None:
|
|
70 |
if not self._ignoremissingpolicyfiles:
|
|
71 |
_logger.error("POLICY_ERROR: Policy file not found under '%s' using names [%s]" % (current_dir, ", ".join(self._policy_files)))
|
|
72 |
policy_value = archive.mappers.MISSING_POLICY
|
|
73 |
else:
|
|
74 |
try:
|
|
75 |
policy_value = fileutils.read_policy_content(policy_file)
|
|
76 |
except Exception:
|
|
77 |
_logger.warning('POLICY_ERROR: Exception thrown parsing policy file: ' + policy_file)
|
|
78 |
policy_value = archive.mappers.MISSING_POLICY
|
|
79 |
# loop through the possible values
|
|
80 |
for value in self.values:
|
|
81 |
(val, negate) = self.get_value_and_negate(value)
|
|
82 |
_logger.debug('Policy value: ' + str(policy_value) + ' ' + val)
|
|
83 |
if (not negate and policy_value == val) or (negate and policy_value != val):
|
|
84 |
return True
|
|
85 |
return False
|
|
86 |
|
|
87 |
|
|
88 |
class SymbianPolicySelector:
|
|
89 |
""" A selector that selects files based on other criteria.
|
|
90 |
|
|
91 |
It is similar to the Ant file selector objects in design. This one selects files
|
|
92 |
based on whether the root-most distribution.policy file matches the given value.
|
|
93 |
"""
|
|
94 |
|
|
95 |
def __init__(self, policy_files, value):
|
|
96 |
""" Initialization. """
|
|
97 |
self._negate = False
|
|
98 |
self.values = [v.strip() for v in value.split() if v.strip()!=""]
|
|
99 |
self._policy_files = policy_files
|
|
100 |
|
|
101 |
|
|
102 |
def get_value_and_negate(self, value):
|
|
103 |
"""get value and negate it"""
|
|
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 |
policy_file = None
|
|
113 |
# finding the distribution policy from the filelist.
|
|
114 |
for filename in self._policy_files:
|
|
115 |
if os.sep != '\\':
|
|
116 |
for f_dir in os.listdir(current_dir):
|
|
117 |
if f_dir.lower() == filename.lower():
|
|
118 |
policy_file = os.path.join(current_dir, f_dir)
|
|
119 |
_logger.debug('Using Policy file: ' + policy_file)
|
|
120 |
break
|
|
121 |
elif os.path.exists(os.path.join(current_dir, filename)):
|
|
122 |
policy_file = os.path.join(current_dir, filename)
|
|
123 |
_logger.debug('Using Policy file: ' + policy_file)
|
|
124 |
break
|
|
125 |
|
|
126 |
policy_value = None
|
|
127 |
if policy_file is None:
|
|
128 |
_logger.error("POLICY_ERROR: Policy file not found under '%s' using names [%s]" % (current_dir, ", ".join(self._policy_files)))
|
|
129 |
policy_value = archive.mappers.MISSING_POLICY
|
|
130 |
else:
|
|
131 |
try:
|
|
132 |
policy_value = fileutils.read_symbian_policy_content(policy_file)
|
|
133 |
except Exception:
|
|
134 |
_logger.warning('POLICY_ERROR: Exception thrown parsing policy file: ' + policy_file)
|
|
135 |
policy_value = archive.mappers.MISSING_POLICY
|
|
136 |
# loop through the possible values
|
|
137 |
for value in self.values:
|
|
138 |
(val, negate) = self.get_value_and_negate(value)
|
|
139 |
_logger.debug('Policy value: ' + str(policy_value) + ' ' + val)
|
|
140 |
if (not negate and policy_value == val) or (negate and policy_value != val):
|
|
141 |
return True
|
|
142 |
return False
|
|
143 |
|
|
144 |
SELECTORS = {'policy': lambda config: DistributionPolicySelector(config.get_list('policy.filenames', ['Distribution.Policy.S60']), config['policy.value']),
|
|
145 |
'symbian.policy': lambda config: SymbianPolicySelector(config.get_list('policy.filenames', ['distribution.policy']), config['policy.value']),
|
|
146 |
'distribution.policy.s60': lambda config: DistributionPolicySelector(['Distribution.Policy.S60'], config['distribution.policy.s60'], config['ignore.missing.policyfiles'] == 'true'),
|
|
147 |
}
|
|
148 |
|
|
149 |
def get_selector(name, config):
|
|
150 |
"""get selector"""
|
|
151 |
if not 'ignore.missing.policyfiles' in config:
|
|
152 |
config['ignore.missing.policyfiles'] = 'false'
|
|
153 |
return SELECTORS[name](config)
|