Environment
- class sane.Environment[source]
Bases:
NameMatch,OptionLoaderControl the setup of an environment on a particular
HostUser Interface
User Methods
- __init__(name, aliases=[], lmod_path=None)[source]
Create a host with
nameand optionalaliasesandlmod_pathIf a
Hosthas aHost.base_env, by default thelmod_pathof that base env will be copied over duringsetup()
- setup_env_vars(cmd, var, val=None, category='unassigned')[source]
Store environment variable commands to execute later during
setup()These commands will eventually be executed within the isolated subprocess of
Action.run(). Thecmdshould be one of {"set","unset","prepend","append"}. Thevalis used for all eventual calls except"unset". Thecmdcorresponds to a respective call used insetup()Example usage for recreating
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/new/path/:self.setup_env_vars( "append", "LD_LIBRARY_PATH", "/new/path/" )
Warning
os.environ uses values verbatim and does not expand values.
setup_env_vars( "append", "LD_LIBRARY_PATH", "$NEW_PATH" )withNEW_PATH=/new/pathwould not produce the intended effect. Currently, theenv_var_*methods do not expand values automatically.If a
categoryis passed in, commands are grouped based on thecategoryand during evaluation are executed in order ofcategorycreation then command insertion order (firstcategoryand first command input go first).
- setup_lmod_cmds(cmd, *args, category='unassigned', **kwargs)[source]
Store lmod commands to execute later during
setup()These commands will eventually be executed within the isolated subprocess of
Action.run(). Thecmdshould be the first argument one would use for module command. All arguments that follow are appended to execution.**kwargsis reseved for the pythonmodule()function implementation.Example usage for recreating
module purge && module load gcc/12.4.0:self.setup_lmod_cmds( "purge" ) self.setup_lmod_cmds( "load", "gcc/12.4.0" )
If a
categoryis passed in, commands are grouped based on thecategoryand during evaluation are executed in order ofcategorycreation then command insertion order (firstcategoryand first command input go first).
- setup_scripts(*scripts)[source]
Store scripts execute later during
setup()These scripts should modify the execution environment silently. Internally, to capture the effects from a subprocess, the difference in env output after running the script and output python commands to emulate this effect.
Danger
The script MUST be silent in output.
User Attributes & Properties
- property name
The name of this object set at instantiation
- property aliases
Aliases that this object may be referenced as
- lmod_path
Path to lmod python module containing the
module()function call
Customizable Functions
The
Environmentis also designed to be modified by users.While any function could generally be overwritten with some care, certain functions within each class are designed specifically for user modification without the need to worry about internal logic.
Defining these functions does not require calling
superor other intrinsic Python knowledge beyond the interface of the function and any user logic.- load_extra_options(options, origin=None)[source]
Load any extra options after
load_core_options().Any processed field should be removed from the options dict, with everything else ignored.
See
load_options()for parameters.
Internal API
The following documentation is provided for advanced use in the creation of a custom
Environment.- setup()[source]
Setup the environment
If a base env is present, call the
setup()for thatEnvironmentfirst. Then call_copy_from_base()to ensure this instance has the relevant information.Setup of this
Environmentfollows this order:Scripts stored from
setup_scripts()are executed viaenv_script()lmod commands stored from
setup_lmod_cmds()are executed viamodule()env commands stored from
setup_env_vars()are executed using the respective command call:
"set"=>env_var_set()"unset"=>env_var_unset()"prepend"=>env_var_prepend()"append"=>env_var_append()
Setup commands with
categoryare executed in order ofcategorycreation then command insertion order (firstcategoryand first command input go first).
- load_options(options, origin=None)[source]
Base class implementation for loading of dict-based attributes into instance
Take a options dict of relevant attributes and load them via
load_core_options()thenload_extra_options(). The options dict should be modified in each call to remove processed fields so that at the very end of this method, any unused keys in the options dict may be logged.The
load_extra_options()is meant as a user-overwritable method so thatload_core_options()may retain core underlying base class implementation details without the risk of base class loading not being called.To keep track of every time this function is called and potentially modifying this instance an origin may be provided, noting where the change is coming from.
- load_core_options(options, origin)[source]
From
OptionLoader.load_core_options:Any processed field should be removed from the options dict, with everything else ignored. All listed options are cummulative and optional unless specified otherwise.
See
load_options()for parameters.From
Environment.load_core_options():Load the options into this
EnvironmentThe following keys are loaded to their respective attribute. If not present, the attributes are unmodified.
The following key is iterated over, where each dict is then expanded to keyword arguments directly calling
setup_env_vars()"env_vars"
The following key is iterated over, where for each dict
"cmd"and"args"are extracted as positional arguments and the remainder is expanded to keyword arguments directly callingsetup_lmod_cmds()"lmod_cmds"
The following key is loaded verbatim using
setup_scripts():"env_scripts"
An example options
dict:{ "aliases" : [ "generic", "maybe something else" ], "lmod_path" : "<path to lmod>", "env_vars" : [ { "cmd" : "prepend", "var" : "foo", "val" : 0 }, { "cmd" : "append", "var" : "bar", "val" : "/path/" } ], "lmod_cmds" : [ { "cmd" : "load", "args" : [ "gcc", "netcdf" ] } ] "env_scripts" : [ "/etc/profile.d/z00_modules.sh", "/some/other/profile/script.sh" ] }
- module(cmd, *args, **kwargs)[source]
Execute lmod commands in a
subprocess.Popenand return the python commands to emulate the environment changes.
- env_script(script)[source]
Execute a script in a
subprocess.Popenand return the python commands to emulate the environment changes.