def testFileDir(self): tmp = TempDir({ 'env.xml': '''<?xml version="1.0" ?> <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd "> <env:set variable="mydirs">${.}</env:set> <env:set variable="myparent">${.}/..</env:set> </env:config>''' }) control = Control.Environment() control.loadXML(tmp('env.xml')) self.assertEqual(str(control['mydirs']), tmp()) self.assertEqual(str(control['myparent']), os.path.dirname(tmp())) olddir = os.getcwd() os.chdir(tmp()) try: control = Control.Environment() control.loadXML('env.xml') self.assertTrue(os.path.samefile(str(control['mydirs']), tmp())) self.assertTrue( os.path.samefile(str(control['myparent']), os.path.dirname(tmp()))) finally: os.chdir(olddir)
def testValues(self): '''Test of value appending, prepending, setting, unsetting, removing''' control = Control.Environment() self.assertFalse('MY_PATH' in control.vars()) control.append('MY_PATH', 'newValue') self.assertTrue('MY_PATH' in control.vars()) var = control.var('MY_PATH') control.append('MY_PATH', 'newValue:secondVal:valval') self.assertTrue(var[len(var) - 1] == 'valval') self.assertTrue('newValue' in var) control.remove('MY_PATH', 'newValue') self.assertFalse('newValue' in var) control.prepend('MY_PATH', 'newValue') self.assertTrue('newValue' == var[0]) control.set('MY_PATH', 'hi:hello') self.assertTrue(len(var) == 2) self.assertTrue('hi' == var[0]) control.unset('MY_PATH') self.assertTrue('MY_PATH' not in control)
def testDelete(self): control = Control.Environment() control.append('MY_PATH', 'myVal:anotherVal:lastVal') control.remove('MY_PATH', 'anotherVal') self.assertFalse('anotherVal' in control['MY_PATH']) self.assertTrue('myVal' in control['MY_PATH']) self.assertTrue('lastVal' in control['MY_PATH']) control.set('MY_PATH', 'myVal:anotherVal:lastVal:else') control.remove('MY_PATH', '^anotherVal$', False) self.assertTrue('anotherVal' in control['MY_PATH']) control.remove('MY_PATH', '^anotherVal$', True) self.assertFalse('anotherVal' in control['MY_PATH']) self.assertTrue('myVal' in control['MY_PATH']) self.assertTrue('lastVal' in control['MY_PATH']) self.assertTrue('lastVal' in control['MY_PATH']) control.remove('MY_PATH', 'Val', True) self.assertTrue('else' in control['MY_PATH']) self.assertTrue(len(control['MY_PATH']) == 1) control.declare('myLoc', 'scalar', False) control.append('myLoc', 'myVal:anotherVal:lastVal') control.remove('myLoc', 'Val:', True) self.assertTrue(str(control['myLoc']) == 'myanotherlastVal')
def testSearch(self): '''Testing searching in variables''' control = Control.Environment() control.append('MY_PATH', 'newValue:mess:something new:aaaabbcc') def count(val, regExp=False): return len(control.search('MY_PATH', val, regExp)) self.assertEqual(count('new'), 0) self.assertEqual(count('newValue'), 1) self.assertEqual(count('me', False), 0) self.assertEqual(count('me', True), 2) self.assertEqual(count('cc', False), 0) self.assertEqual(count('cc', True), 1) self.assertEqual(count('a{2}b{2}c{2}', True), 1) self.assertEqual(count('a{2}b{2}', True), 1) self.assertEqual(count('a{1}b{2}c{2}', True), 1) self.assertEqual(count('a{1}b{1}c{2}', True), 0) self.assertEqual(count('a{1,2}b{1,2}c{2}', True), 1) self.assertEqual(count('a{2,3}', True), 1) self.assertEqual(count('a{2,3}?', True), 1)
def testHidingDotVar(self): control = Control.Environment() control.variables['.'].set('some/dir') self.assertTrue('.' in control.variables) self.assertTrue('.' not in control.vars()) self.assertTrue('.' not in control.vars(strings=False)) control.set('MY_DIR', '${.}') self.assertEqual(control.var('MY_DIR').value(True), 'some/dir') self.assertEqual(control.vars()['MY_DIR'], 'some/dir')
def testWriteWithList(self): """XML file write and load test""" control = Control.Environment(useAsWriter=True) control.unset('MY_PATH') control.set('MY_PATH', ['set', 'toDelete']) control.append('MY_PATH', ['appended', 'toDelete']) control.prepend('MY_PATH', ['prepended', 'toDelete']) control.remove('MY_PATH', ['toDelete']) control.finishXMLinput('testOutputFile.xml') control = Control.Environment() self.assertFalse('MY_PATH' in control.vars()) control.loadXML('testOutputFile.xml') self.assertTrue('MY_PATH' in control.vars()) var = control.var('MY_PATH') self.assertTrue(var[0] == 'prepended') self.assertTrue(var[1] == 'set') self.assertTrue(var[2] == 'appended') self.assertFalse('toDelete' in var) os.remove('testOutputFile.xml')
def testVariables(self): '''Tests variables creation and redeclaration.''' control = Control.Environment() control.append('MY_PATH', 'newValue') self.assertFalse(control.var('MY_PATH').local) self.assertTrue(isinstance(control.var('MY_PATH'), Variable.List)) control.declare('loc', 'list', True) self.assertTrue(control.var('loc').local) self.assertTrue(isinstance(control.var('loc'), Variable.List)) control.declare('myVar2', 'scalar', False) self.assertFalse(control.var('myVar2').local) self.assertTrue(isinstance(control.var('myVar2'), Variable.Scalar)) control.declare('loc2', 'scalar', True) self.assertTrue(control.var('loc2').local) self.assertTrue(isinstance(control.var('loc2'), Variable.Scalar)) control.declare('MY_PATH', 'list', False) self.failUnlessRaises(Variable.EnvError, control.declare, 'MY_PATH', 'list', True) self.failUnlessRaises(Variable.EnvError, control.declare, 'MY_PATH', 'scalar', True) self.failUnlessRaises(Variable.EnvError, control.declare, 'MY_PATH', 'scalar', True) control.declare('loc', 'list', True) self.failUnlessRaises(Variable.EnvError, control.declare, 'loc', 'list', False) self.failUnlessRaises(Variable.EnvError, control.declare, 'loc', 'scalar', True) self.failUnlessRaises(Variable.EnvError, control.declare, 'loc', 'scalar', True) control.declare('myVar2', 'scalar', False) self.failUnlessRaises(Variable.EnvError, control.declare, 'myVar2', 'list', False) self.failUnlessRaises(Variable.EnvError, control.declare, 'myVar2', 'list', True) self.failUnlessRaises(Variable.EnvError, control.declare, 'myVar2', 'scalar', True) control.declare('loc2', 'scalar', True) self.failUnlessRaises(Variable.EnvError, control.declare, 'loc2', 'list', False) self.failUnlessRaises(Variable.EnvError, control.declare, 'loc2', 'list', True) self.failUnlessRaises(Variable.EnvError, control.declare, 'loc2', 'scalar', False)
def testDefaults(self): tmp = TempDir({ 'env.xml': '''<?xml version="1.0" ?> <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd "> <env:default variable="var1">value1</env:default> <env:declare variable="var2" local="true" /> <env:default variable="var2">test2</env:default> </env:config>''' }) if 'var1' in os.environ: del os.environ['var1'] control = Control.Environment() control.loadXML(tmp('env.xml')) self.assertEqual(str(control['var1']), "value1") self.assertEqual(str(control['var2']), "test2") os.environ['var1'] = "some_value" control = Control.Environment() control.loadXML(tmp('env.xml')) self.assertEqual(str(control['var1']), "some_value") self.assertEqual(str(control['var2']), "test2")
def testSystemEnvironment(self): control = Control.Environment() os.environ['MY_PATH'] = '$myVal' os.environ['myScal'] = '$myVal' control.set('ABC', 'anyValue') control.declare('MY_PATH', 'list', False) control.append('MY_PATH', '$ABC') self.assertTrue(control['MY_PATH'].value(True) == '$myVal:anyValue') control.declare('myScal', 'scalar', False) control.append('myScal', '$ABC') self.assertTrue(control['myScal'].value(True) == '$myValanyValue')
def testSaveToXML(self): """XML file write and load test""" control = Control.Environment() control.unset('MY_PATH') control.set('MY_PATH', 'set:toDelete') control.append('MY_PATH', 'appended:toDelete') control.prepend('MY_PATH', 'prepended:toDelete') control.remove('MY_PATH', 'toDelete') control.writeToXMLFile('testOutputFile.xml') control = Control.Environment() self.assertFalse('MY_PATH' in control.vars()) control.loadXML('testOutputFile.xml') self.assertTrue('MY_PATH' in control.vars()) var = control.var('MY_PATH') self.assertTrue(var[0] == 'prepended') self.assertTrue(var[1] == 'set') self.assertTrue(var[2] == 'appended') self.assertFalse('toDelete' in var) os.remove('testOutputFile.xml')
def testSaveToFile(self): '''Test addition of variable to system''' control = Control.Environment() control.append('sysVar', 'newValue:lala') control.writeToFile('setupFile.txt') with open('setupFile.txt', "r") as f: f.readline() stri = f.readline() f.close() self.assertEqual(stri, 'export sysVar=newValue:lala' + os.linesep) os.remove('setupFile.txt')
def testDependencies(self): control = Control.Environment() control.declare('myVar', 'list', False) control.declare('loc', 'list', True) control.append('loc', 'locVal') control.append('loc', 'locVal2') control.declare('scal', 'scalar', False) control.append('scal', 'scalVal') control.append('scal', 'scalVal2') control.declare('scal2', 'scalar', True) control.append('scal2', 'locScal') control.append('scal2', 'locScal2') control.set('myVar', 'newValue:$loc:endValue') self.assertEqual(str(control['myVar']), 'newValue:locVal:locVal2:endValue') control.set('myVar', 'newValue:$scal:endValue') self.assertEqual(str(control['myVar']), 'newValue:scalValscalVal2:endValue') control.set('myVar', 'new${scal}Value:endValue') self.assertEqual(str(control['myVar']), 'newscalValscalVal2Value:endValue') control.set('myVar', 'bla:$myVar:Value') self.assertEqual(str(control['myVar']), 'bla:newscalValscalVal2Value:endValue:Value') control.set('scal', 'new${scal2}Value') self.assertEqual(str(control['scal']), 'newlocScallocScal2Value') control.set('scal', 'new${loc}Value') self.assertEqual(str(control['scal']), 'newlocVal:locVal2Value') control.set('scal2', 'new${scal2}Value') self.assertEqual(str(control['scal2']), 'newlocScallocScal2Value')
def _makeEnv(self): ''' Generate a dictionary of the environment variables after applying all the required actions. ''' # prepare the environment control instance control = Control.Environment() if not self.opts.ignore_environment: control.presetFromSystem() # apply all the actions for action, args in self.opts.actions: print(getattr(control, action), args) #apply(getattr(control, action), args) f = getattr(control, action) f(*args) #function(*args, **keywords) instead of apply(function, args, keywords) # extract the result env dictionary env = control.vars() # set the library search path correctly for the non-Linux platforms if "LD_LIBRARY_PATH" in env: # replace LD_LIBRARY_PATH with the corresponding one on other # systems if sys.platform.startswith("win"): other = "PATH" elif sys.platform.startswith("darwin"): other = "DYLD_LIBRARY_PATH" else: other = None if other: if other in env: env[other] = env[other] + \ os.pathsep + env["LD_LIBRARY_PATH"] else: env[other] = env["LD_LIBRARY_PATH"] del env["LD_LIBRARY_PATH"] self.env = env
def testsVariablesInSearchPath(self): with TemporaryDir(chdir=True) as tmp: with open('EntryPoint.xenv', 'w') as f: f.write('''<?xml version="1.0" ?> <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd ">' <env:default variable="INCLUDED_FILE_PATH">${.}/subdir</env:default> <env:search_path>${INCLUDED_FILE_PATH}</env:search_path> <env:include>Included.xenv</env:include> </env:config>''') os.makedirs('subdir') with open('subdir/Included.xenv', 'w') as f: f.write('''<?xml version="1.0" ?> <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd "> <env:set variable="INCLUDED_FILE">OK</env:set> </env:config>''') control = Control.Environment(searchPath=[]) control.loadXML('EntryPoint.xenv') self.assertEqual(str(control['INCLUDED_FILE_PATH']), os.path.join(tmp, 'subdir')) self.assertEqual(str(control['INCLUDED_FILE']), 'OK')
def testFileLoad(self): '''Test loading of previously written file.''' self.control = Control.Environment(useAsWriter = True) self.control.unset('varToUnset') self.control.declare('myVar', 'list', True) self.control.set('myVar', 'setVal:$local') self.control.append('myVar', 'appVal:appVal2') self.control.prepend('myVar', 'prepVal:prepVal2') self.control.declare('myScalar', 'scalar', False) self.control.set('myScalar', 'setValscal') self.control.append('myScalar', 'appValscal') self.control.prepend('myScalar', 'prepValscal') self.control.declare('myScalar2', 'scalar', True) self.control.finishXMLinput('testOutputFile.xml') loader = xmlModule.XMLFile() variables = loader.variable('testOutputFile.xml') expected = [('declare', ('varToUnset', 'list', 'false')), ('unset', ('varToUnset', '', None)), ('declare', ('myVar', 'list', 'true')), ('set', ('myVar', 'setVal:$local', None)), ('append', ('myVar', 'appVal:appVal2', None)), ('prepend', ('myVar', 'prepVal:prepVal2', None)), ('declare', ('myScalar', 'scalar', 'false')), ('set', ('myScalar', 'setValscal', None)), ('append', ('myScalar', 'appValscal', None)), ('prepend', ('myScalar', 'prepValscal', None)), ('declare', ('myScalar2', 'scalar', 'true'))] self.assertEqual(variables, expected) os.remove('testOutputFile.xml')
def testInclude(self): tmp = TempDir({ 'first.xml': '''<?xml version="1.0" ?> <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd "> <env:set variable="main">first</env:set> <env:append variable="test_path">data1</env:append> <env:include>first_inc.xml</env:include> </env:config>''', 'second.xml': '''<?xml version="1.0" ?> <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd "> <env:set variable="main">second</env:set> <env:include>second_inc.xml</env:include> <env:append variable="test_path">data1</env:append> </env:config>''', 'third.xml': '''<?xml version="1.0" ?> <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd "> <env:set variable="main">third</env:set> <env:append variable="test_path">data1</env:append> <env:include>subdir/first_inc.xml</env:include> </env:config>''', 'fourth.xml': '''<?xml version="1.0" ?> <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd "> <env:set variable="main">fourth</env:set> <env:include hints="subdir2">fourth_inc.xml</env:include> </env:config>''', 'recursion.xml': '''<?xml version="1.0" ?> <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd "> <env:set variable="main">recursion</env:set> <env:include>recursion.xml</env:include> </env:config>''', 'first_inc.xml': '''<?xml version="1.0" ?> <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd "> <env:append variable="test_path">data2</env:append> <env:append variable="derived">another_${main}</env:append> </env:config>''', 'subdir': { 'second_inc.xml': '''<?xml version="1.0" ?> <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd "> <env:append variable="test_path">data0</env:append> <env:set variable="map">this_is_second_inc</env:set> </env:config>''', 'first_inc.xml': '''<?xml version="1.0" ?> <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd "> <env:append variable="derived">second_${main}</env:append> </env:config>''', 'fourth_inc.xml': '''<?xml version="1.0" ?> <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd "> <env:append variable="included">from subdir</env:append> </env:config>''', }, 'subdir2': { 'fourth_inc.xml': '''<?xml version="1.0" ?> <env:config xmlns:env="EnvSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="EnvSchema ./EnvSchema.xsd "> <env:append variable="included">from subdir2</env:append> </env:config>''', } }) # set the basic search path to the minimal default if 'ENVXMLPATH' in os.environ: del os.environ['ENVXMLPATH'] import EnvConfig # @UnresolvedImport saved_path = list(EnvConfig.path) EnvConfig.path[:] = ['.'] control = Control.Environment(searchPath=[]) #self.assertRaises(OSError, control.loadXML, tmp('first.xml')) control.loadXML(tmp('first.xml')) self.assertEqual(str(control['main']), 'first') self.assertEqual(str(control['test_path']), 'data1:data2') self.assertEqual(str(control['derived']), 'another_first') control = Control.Environment(searchPath=[tmp()]) control.loadXML(tmp('first.xml')) self.assertEqual(str(control['main']), 'first') self.assertEqual(str(control['test_path']), 'data1:data2') self.assertEqual(str(control['derived']), 'another_first') control = Control.Environment(searchPath=[tmp()]) control.loadXML('first.xml') self.assertEqual(str(control['main']), 'first') self.assertEqual(str(control['test_path']), 'data1:data2') self.assertEqual(str(control['derived']), 'another_first') control = Control.Environment(searchPath=[tmp()]) self.assertRaises(OSError, control.loadXML, tmp('second.xml')) control = Control.Environment(searchPath=[tmp(), tmp('subdir')]) control.loadXML(tmp('second.xml')) self.assertEqual(str(control['main']), 'second') self.assertEqual(str(control['test_path']), 'data0:data1') self.assertEqual(str(control['map']), 'this_is_second_inc') control = Control.Environment(searchPath=[tmp(), tmp('subdir')]) control.loadXML(tmp('first.xml')) self.assertEqual(str(control['main']), 'first') self.assertEqual(str(control['test_path']), 'data1:data2') self.assertEqual(str(control['derived']), 'another_first') control = Control.Environment(searchPath=[tmp('subdir'), tmp()]) control.loadXML(tmp('first.xml')) self.assertEqual(str(control['main']), 'first') self.assertEqual(str(control['test_path']), 'data1:data2') self.assertEqual(str(control['derived']), 'another_first') control = Control.Environment(searchPath=[tmp('subdir'), tmp()]) control.loadXML('first.xml') self.assertEqual(str(control['main']), 'first') self.assertEqual(str(control['test_path']), 'data1:data2') self.assertEqual(str(control['derived']), 'another_first') #os.environ['ENVXMLPATH'] = os.pathsep.join([tmp(), tmp('subdir')]) EnvConfig.path[:] = ['.', tmp(), tmp('subdir')] control = Control.Environment(searchPath=[]) control.loadXML(tmp('second.xml')) self.assertEqual(str(control['main']), 'second') self.assertEqual(str(control['test_path']), 'data0:data1') self.assertEqual(str(control['map']), 'this_is_second_inc') #del os.environ['ENVXMLPATH'] EnvConfig.path[:] = ['.'] control = Control.Environment(searchPath=[]) control.loadXML(tmp('third.xml')) self.assertEqual(str(control['main']), 'third') self.assertEqual(str(control['test_path']), 'data1') self.assertEqual(str(control['derived']), 'second_third') control = Control.Environment(searchPath=[tmp('subdir')]) control.loadXML(tmp('fourth.xml')) self.assertEqual(str(control['main']), 'fourth') self.assertEqual(str(control['included']), 'from subdir') control = Control.Environment(searchPath=[]) control.loadXML(tmp('fourth.xml')) self.assertEqual(str(control['main']), 'fourth') self.assertEqual(str(control['included']), 'from subdir2') control = Control.Environment(searchPath=[]) #self.assertRaises(OSError, control.loadXML, tmp('first.xml')) control.loadXML(tmp('recursion.xml')) # restore search path EnvConfig.path = saved_path