2 Created on Jun 27, 2011
8 from os.path
import normpath
9 from zipfile
import is_zipfile
13 Base class for the objects used to process the variables.
17 @param env: dictionary with the reference environment to use
25 Return True if this processor can operate on the given variable.
33 @param value: the content of the variable to be processed
34 @return: the processed value
40 return self.
process(variable, value)
44 Base class for processors operating only on lists.
48 Return True if this variable is a list.
50 return isinstance(variable, List)
54 Base class for processors operating only on scalars.
58 Return True if this variable is a scalar.
60 return isinstance(variable, Scalar)
64 Variable processor to expand the reference to environment variables.
67 super(EnvExpander, self).
__init__(env)
68 self.
_exp = re.compile(
r"\$([A-Za-z_][A-Za-z0-9_]*)|\$\(([A-Za-z_][A-Za-z0-9_]*)\)|\$\{([A-Za-z_][A-Za-z0-9_]*)\}|\$\{(\.)\}")
71 return (super(EnvExpander, self).
isTarget(variable)
72 and variable.expandVars)
75 m = self._exp.search(value)
77 value = (value[:m.start()]
78 + str(self.
_env[filter(
None, m.groups())[0]])
80 return self.
_repl(value)
85 if isinstance(value, str):
86 value = self.
_repl(value)
89 old_values = set(variable.val)
90 value = map(
lambda v: v
if v
in old_values
else self.
_repl(v), value)
95 Call os.path.normpath for all the entries of the variable.
98 if isinstance(value, str):
99 if '://' not in value:
100 value = normpath(value)
102 value = [normpath(v)
for v
in value
if v]
107 Remove duplicates entries from lists.
118 Remove empty or not existing directories from lists.
121 from os.path
import isdir
122 from os
import listdir
123 return [s
for s
in value
if s.endswith(
'.zip')
or (isdir(s)
and listdir(s))]
127 Use .zip files instead of regular directories in PYTHONPATH when possible.
130 return (super(UsePythonZip, self).
isTarget(variable)
131 and variable.varName ==
'PYTHONPATH')
144 processors = [ EnvExpander, PathNormalizer, DuplicatesRemover,
146 EmptyDirsRemover, UsePythonZip
150 if (
'no-strip-path' in os.environ.get(
'CMTEXTRATAGS',
'')
151 or 'GAUDI_NO_STRIP_PATH' in os.environ
152 or 'LB_NO_STRIP_PATH' in os.environ):
153 processors.remove(EmptyDirsRemover)
155 if 'no-pyzip' in os.environ.get(
'CMTEXTRATAGS',
''):
156 processors.remove(UsePythonZip)
160 Base class for the classes used to manipulate the environment.
163 def __init__(self, name, local=False, report=None):
171 Call all the processors defined in the processors list on 'value'.
173 @return: the processed value
175 for p
in [
c(env)
for c
in processors]:
177 value = p(self, value)
182 Class for manipulating with environment lists.
184 It holds its name and values represented by a list.
185 Some operations are done with separator, which is usually colon. For windows use semicolon.
188 def __init__(self, name, local=False, report=None):
189 super(List, self).
__init__(name, local, report)
193 '''Returns the name of the List.'''
196 def set(self, value, separator=':
', environment=None):
197 '''Sets the value of the List. Any previous value is overwritten.'''
198 if isinstance(value, str):
199 value = value.split(separator)
202 def unset(self, value, separator=':
', environment=None):# pylint: disable=W0613
203 '''Sets the value of the List to empty. Any previous value is overwritten.'''
206 def value(self, asString=False, separator=':
'):
207 '''Returns values of the List. Either as a list or string with desired separator.'''
209 return separator.join(self.
val)
212 return list(self.
val)
215 self.remove(value, separator, True)
217 def remove(self, value, separator=':
', regexp=False):
218 '''Removes value(s) from List. If value is not found, removal is canceled.'''
220 value = self.
search(value,
True)
222 elif isinstance(value,str):
223 value = value.split(separator)
225 for i
in range(len(value)):
228 self.report.addWarn(
'Value "'+val+
'" not found in List: "'+self.
varName+
'". Removal canceled.')
229 while val
in self.
val:
233 def append(self, value, separator=':
', environment=None):
234 '''Adds value(s) at the end of the list.'''
235 if isinstance(value, str):
236 value = value.split(separator)
239 def prepend(self, value, separator=':
', environment=None):
240 '''Adds value(s) at the beginning of the list.
241 resolve references and duplications'''
242 if isinstance(value, str):
243 value = value.split(separator)
247 '''Searches in List's values for a match
249 Use string value or set regExp to True.
250 In the first case search is done only for an exact match for one of List`s value ('^' and '$' added).
253 expr =
'^' + expr +
'$'
266 if value
in self.
val:
267 self.report.addWarn(
'Var: "' + self.
varName +
'" value: "' + value +
'". Addition canceled because of duplicate entry.')
269 self.val.insert(key, value)
279 return item
in self.
val
285 return ':'.join(self.
val)
289 '''Class for manipulating with environment scalars.'''
291 def __init__(self, name, local=False, report=None):
292 super(Scalar, self).
__init__(name, local, report)
296 '''Returns the name of the scalar.'''
299 def set(self, value, separator=':
', environment=None):# pylint: disable=W0613
300 '''Sets the value of the scalar. Any previous value is overwritten.'''
303 def unset(self, value, separator=':
', environment=None):# pylint: disable=W0613
304 '''Sets the value of the variable to empty. Any previous value is overwritten.'''
307 def value(self, asString=False, separator=':
'):# pylint: disable=W0613
308 '''Returns values of the scalar.'''
312 self.remove(value, separator, True)
314 def remove(self, value, separator=':
', regexp=True):# pylint: disable=W0613
315 '''Removes value(s) from the scalar. If value is not found, removal is canceled.'''
316 value = self.
search(value)
318 self.
val = self.val.replace(val,
'')
320 def append(self, value, separator=':
', environment=None):# pylint: disable=W0613
321 '''Adds value(s) at the end of the scalar.'''
324 def prepend(self, value, separator=':
', environment=None):# pylint: disable=W0613
325 '''Adds value(s) at the beginning of the scalar.'''
329 '''Searches in scalar`s values for a match'''
330 return re.findall(expr, self.
val)
336 '''Class which defines errors for locals operations.'''
342 if self.
code ==
'undefined':
343 return 'Reference to undefined environment element: "'+self.
val +
'".'
344 elif self.
code ==
'ref2var':
345 return 'Reference to list from the middle of string.'
346 elif self.
code ==
'redeclaration':
347 return 'Wrong redeclaration of environment element "'+self.
val+
'".'