2 Created on Jun 27, 2011
9 from os.path
import normpath
10 from zipfile
import is_zipfile
14 Base class for the objects used to process the variables.
18 @param env: dictionary with the reference environment to use
26 Return True if this processor can operate on the given variable.
34 @param value: the content of the variable to be processed
35 @return: the processed value
41 return self.
process(variable, value)
45 Base class for processors operating only on lists.
49 Return True if this variable is a list.
51 return isinstance(variable, List)
55 Base class for processors operating only on scalars.
59 Return True if this variable is a scalar.
61 return isinstance(variable, Scalar)
65 Variable processor to expand the reference to environment variables.
68 super(EnvExpander, self).
__init__(env)
69 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_]*)\}|\$\{(\.)\}")
72 return (super(EnvExpander, self).
isTarget(variable)
73 and variable.expandVars)
76 m = self._exp.search(value)
79 value = (value[:m.start()]
80 + str(self.
_env[filter(
None, m.groups())[0]])
83 logging.debug(
'KeyError: %s unknown while expanding %s', k, value)
85 return self.
_repl(value)
90 if isinstance(value, str):
91 value = self.
_repl(value)
94 old_values = set(variable.val)
95 value =
map(
lambda v: v
if v
in old_values
else self.
_repl(v), value)
100 Call os.path.normpath for all the entries of the variable.
103 if isinstance(value, str):
104 if '://' not in value:
105 value = normpath(value)
107 value = [normpath(v)
for v
in value
if v]
112 Remove duplicates entries from lists.
123 Remove empty or not existing directories from lists.
126 from os.path
import isdir
127 from os
import listdir
128 return [s
for s
in value
if s.endswith(
'.zip')
or (isdir(s)
and listdir(s))]
132 Use .zip files instead of regular directories in PYTHONPATH when possible.
135 return (super(UsePythonZip, self).
isTarget(variable)
136 and variable.varName ==
'PYTHONPATH')
149 processors = [ EnvExpander, PathNormalizer, DuplicatesRemover,
151 EmptyDirsRemover, UsePythonZip
155 if (
'no-strip-path' in os.environ.get(
'CMTEXTRATAGS',
'')
156 or 'GAUDI_NO_STRIP_PATH' in os.environ
157 or 'LB_NO_STRIP_PATH' in os.environ):
158 processors.remove(EmptyDirsRemover)
160 if 'no-pyzip' in os.environ.get(
'CMTEXTRATAGS',
''):
161 processors.remove(UsePythonZip)
165 Base class for the classes used to manipulate the environment.
172 self.
log = logging.getLogger(
'Variable')
176 Call all the processors defined in the processors list on 'value'.
178 @return: the processed value
180 for p
in [
c(env)
for c
in processors]:
182 value = p(self, value)
187 Class for manipulating with environment lists.
189 It holds its name and values represented by a list.
190 Some operations are done with separator, which is usually colon. For windows use semicolon.
194 super(List, self).
__init__(name, local)
198 '''Returns the name of the List.'''
201 def set(self, value, separator=':
', environment=None):
202 '''Sets the value of the List. Any previous value is overwritten.'''
203 if isinstance(value, str):
204 value = value.split(separator)
207 def unset(self, value, separator=':
', environment=None):# pylint: disable=W0613
208 '''Sets the value of the List to empty. Any previous value is overwritten.'''
211 def value(self, asString=False, separator=':
'):
212 '''Returns values of the List. Either as a list or string with desired separator.'''
214 return separator.join(self.
val)
217 return list(self.
val)
220 self.remove(value, separator, True)
222 def remove(self, value, separator=':
', regexp=False):
223 '''Removes value(s) from List. If value is not found, removal is canceled.'''
225 value = self.
search(value,
True)
227 elif isinstance(value,str):
228 value = value.split(separator)
230 for i
in range(len(value)):
233 self.log.info(
'Value "%s" not found in List: "%s". Removal canceled.', val, self.
varName)
234 while val
in self.
val:
238 def append(self, value, separator=':
', environment=None):
239 '''Adds value(s) at the end of the list.'''
240 if isinstance(value, str):
241 value = value.split(separator)
244 def prepend(self, value, separator=':
', environment=None):
245 '''Adds value(s) at the beginning of the list.
246 resolve references and duplications'''
247 if isinstance(value, str):
248 value = value.split(separator)
252 '''Searches in List's values for a match
254 Use string value or set regExp to True.
255 In the first case search is done only for an exact match for one of List`s value ('^' and '$' added).
258 expr =
'^' + expr +
'$'
271 if value
in self.
val:
272 self.log.info(
'Var: "%s" value: "%s". Addition canceled because of duplicate entry.', self.
varName, value)
274 self.val.insert(key, value)
284 return item
in self.
val
290 return ':'.join(self.
val)
294 '''Class for manipulating with environment scalars.'''
297 super(Scalar, self).
__init__(name, local)
301 '''Returns the name of the scalar.'''
304 def set(self, value, separator=':
', environment=None):# pylint: disable=W0613
305 '''Sets the value of the scalar. Any previous value is overwritten.'''
308 def unset(self, value, separator=':
', environment=None):# pylint: disable=W0613
309 '''Sets the value of the variable to empty. Any previous value is overwritten.'''
312 def value(self, asString=False, separator=':
'):# pylint: disable=W0613
313 '''Returns values of the scalar.'''
317 self.remove(value, separator, True)
319 def remove(self, value, separator=':
', regexp=True):# pylint: disable=W0613
320 '''Removes value(s) from the scalar. If value is not found, removal is canceled.'''
321 value = self.
search(value)
323 self.
val = self.val.replace(val,
'')
325 def append(self, value, separator=':
', environment=None):# pylint: disable=W0613
326 '''Adds value(s) at the end of the scalar.'''
329 def prepend(self, value, separator=':
', environment=None):# pylint: disable=W0613
330 '''Adds value(s) at the beginning of the scalar.'''
334 '''Searches in scalar`s values for a match'''
335 return re.findall(expr, self.
val)
341 '''Class which defines errors for locals operations.'''
347 if self.
code ==
'undefined':
348 return 'Reference to undefined environment element: "'+self.
val +
'".'
349 elif self.
code ==
'ref2var':
350 return 'Reference to list from the middle of string.'
351 elif self.
code ==
'redeclaration':
352 return 'Wrong redeclaration of environment element "'+self.
val+
'".'