environment : simplified hierarchy according to comments in review
This commit is contained in:
parent
f0f0663d1b
commit
3da4d6664b
@ -3,73 +3,54 @@
|
|||||||
import collections
|
import collections
|
||||||
|
|
||||||
|
|
||||||
class AttributeHolder(object):
|
class NameModifier(object):
|
||||||
"""
|
def __init__(self, name, **kwargs):
|
||||||
Policy that permits to store any kind of attribute on self. The attributes must be passed as key/value pairs
|
self.name = name
|
||||||
during the initialization of the instance.
|
self.args = {'name': name}
|
||||||
"""
|
self.args.update(kwargs)
|
||||||
def __init__(self, **kwargs):
|
|
||||||
for key, value in kwargs.items():
|
|
||||||
setattr(self, key, value)
|
|
||||||
|
|
||||||
|
|
||||||
class SetEnv(AttributeHolder):
|
class NameValueModifier(object):
|
||||||
def __init__(self, name, value, **kwargs):
|
def __init__(self, name, value, **kwargs):
|
||||||
super(SetEnv, self).__init__(**kwargs)
|
|
||||||
self.name = name
|
self.name = name
|
||||||
self.value = value
|
self.value = value
|
||||||
|
self.args = {'name': name, 'value': value}
|
||||||
|
self.args.update(kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class SetEnv(NameValueModifier):
|
||||||
def execute(self):
|
def execute(self):
|
||||||
os.environ[self.name] = str(self.value)
|
os.environ[self.name] = str(self.value)
|
||||||
|
|
||||||
|
|
||||||
class UnsetEnv(AttributeHolder):
|
class UnsetEnv(NameModifier):
|
||||||
def __init__(self, name, **kwargs):
|
|
||||||
super(UnsetEnv, self).__init__(**kwargs)
|
|
||||||
self.name = name
|
|
||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
os.environ.pop(self.name, None) # Avoid throwing if the variable was not set
|
os.environ.pop(self.name, None) # Avoid throwing if the variable was not set
|
||||||
|
|
||||||
|
|
||||||
class AppendPath(AttributeHolder):
|
class AppendPath(NameValueModifier):
|
||||||
def __init__(self, name, path, **kwargs):
|
|
||||||
super(AppendPath, self).__init__(**kwargs)
|
|
||||||
self.name = name
|
|
||||||
self.path = path
|
|
||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
environment_value = os.environ.get(self.name, '')
|
environment_value = os.environ.get(self.name, '')
|
||||||
directories = environment_value.split(':') if environment_value else []
|
directories = environment_value.split(':') if environment_value else []
|
||||||
# TODO : Check if this is a valid directory name
|
# TODO : Check if this is a valid directory name
|
||||||
directories.append(os.path.normpath(self.path))
|
directories.append(os.path.normpath(self.value))
|
||||||
os.environ[self.name] = ':'.join(directories)
|
os.environ[self.name] = ':'.join(directories)
|
||||||
|
|
||||||
|
|
||||||
class PrependPath(AttributeHolder):
|
class PrependPath(NameValueModifier):
|
||||||
def __init__(self, name, path, **kwargs):
|
|
||||||
super(PrependPath, self).__init__(**kwargs)
|
|
||||||
self.name = name
|
|
||||||
self.path = path
|
|
||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
environment_value = os.environ.get(self.name, '')
|
environment_value = os.environ.get(self.name, '')
|
||||||
directories = environment_value.split(':') if environment_value else []
|
directories = environment_value.split(':') if environment_value else []
|
||||||
# TODO : Check if this is a valid directory name
|
# TODO : Check if this is a valid directory name
|
||||||
directories = [os.path.normpath(self.path)] + directories
|
directories = [os.path.normpath(self.value)] + directories
|
||||||
os.environ[self.name] = ':'.join(directories)
|
os.environ[self.name] = ':'.join(directories)
|
||||||
|
|
||||||
|
|
||||||
class RemovePath(AttributeHolder):
|
class RemovePath(NameValueModifier):
|
||||||
def __init__(self, name, path, **kwargs):
|
|
||||||
super(RemovePath, self).__init__(**kwargs)
|
|
||||||
self.name = name
|
|
||||||
self.path = path
|
|
||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
environment_value = os.environ.get(self.name, '')
|
environment_value = os.environ.get(self.name, '')
|
||||||
directories = environment_value.split(':') if environment_value else []
|
directories = environment_value.split(':') if environment_value else []
|
||||||
directories = [os.path.normpath(x) for x in directories if x != os.path.normpath(self.path)]
|
directories = [os.path.normpath(x) for x in directories if x != os.path.normpath(self.value)]
|
||||||
os.environ[self.name] = ':'.join(directories)
|
os.environ[self.name] = ':'.join(directories)
|
||||||
|
|
||||||
|
|
||||||
|
@ -178,7 +178,7 @@ def write_header(self, stream):
|
|||||||
def process_environment_command(self, env):
|
def process_environment_command(self, env):
|
||||||
for command in env:
|
for command in env:
|
||||||
# FIXME : how should we handle errors here?
|
# FIXME : how should we handle errors here?
|
||||||
yield self.formats[type(command)].format(command)
|
yield self.formats[type(command)].format(**command.args)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def file_name(self):
|
def file_name(self):
|
||||||
@ -203,8 +203,8 @@ class Dotkit(EnvModule):
|
|||||||
path = join_path(spack.share_path, "dotkit")
|
path = join_path(spack.share_path, "dotkit")
|
||||||
|
|
||||||
formats = {
|
formats = {
|
||||||
PrependPath: 'dk_alter {0.name} {0.path}\n',
|
PrependPath: 'dk_alter {name} {value}\n',
|
||||||
SetEnv: 'dk_setenv {0.name} {0.value}\n'
|
SetEnv: 'dk_setenv {name} {value}\n'
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -238,8 +238,8 @@ class TclModule(EnvModule):
|
|||||||
path = join_path(spack.share_path, "modules")
|
path = join_path(spack.share_path, "modules")
|
||||||
|
|
||||||
formats = {
|
formats = {
|
||||||
PrependPath: 'prepend-path {0.name} \"{0.path}\"\n',
|
PrependPath: 'prepend-path {name} \"{value}\"\n',
|
||||||
SetEnv: 'setenv {0.name} \"{0.value}\"\n'
|
SetEnv: 'setenv {name} \"{value}\"\n'
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -979,6 +979,7 @@ def module(self):
|
|||||||
|
|
||||||
def setup_environment(self, env):
|
def setup_environment(self, env):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
Called before the install() method of dependents.
|
Called before the install() method of dependents.
|
||||||
|
|
||||||
Return the list of environment modifications needed by dependents (or extensions). Default implementation does
|
Return the list of environment modifications needed by dependents (or extensions). Default implementation does
|
||||||
|
@ -53,7 +53,7 @@ def test_extra_arguments(self):
|
|||||||
env = EnvironmentModifications()
|
env = EnvironmentModifications()
|
||||||
env.set_env('A', 'dummy value', who='Pkg1')
|
env.set_env('A', 'dummy value', who='Pkg1')
|
||||||
for x in env:
|
for x in env:
|
||||||
assert hasattr(x, 'who')
|
assert 'who' in x.args
|
||||||
env.apply_modifications()
|
env.apply_modifications()
|
||||||
self.assertEqual('dummy value', os.environ['A'])
|
self.assertEqual('dummy value', os.environ['A'])
|
||||||
|
|
||||||
|
@ -40,13 +40,11 @@ def env_flag(name):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
# FIXME : remove this function ?
|
|
||||||
def path_set(var_name, directories):
|
def path_set(var_name, directories):
|
||||||
path_str = ":".join(str(dir) for dir in directories)
|
path_str = ":".join(str(dir) for dir in directories)
|
||||||
os.environ[var_name] = path_str
|
os.environ[var_name] = path_str
|
||||||
|
|
||||||
|
|
||||||
# FIXME : remove this function ?
|
|
||||||
def path_put_first(var_name, directories):
|
def path_put_first(var_name, directories):
|
||||||
"""Puts the provided directories first in the path, adding them
|
"""Puts the provided directories first in the path, adding them
|
||||||
if they're not already there.
|
if they're not already there.
|
||||||
|
Loading…
Reference in New Issue
Block a user