rez.rex

class rez.rex.Action(*args)[source]

Bases: object

classmethod get_command_types()[source]
classmethod register()[source]
classmethod register_command_type(name, klass)[source]
class rez.rex.ActionInterpreter[source]

Bases: object

Abstract base class that provides callbacks for rex Actions. This class should not be used directly. Its methods are called by the ActionManager in response to actions issued by user code written using the rex python API.

Sub-classes should override the get_output method to return implementation-specific data structure. For example, an interpreter for a shell language like bash would return a string of shell code. An interpreter for an active python session might return a dictionary of the modified environment.

Sub-classes can override the expand_env_vars class variable to instruct the ActionManager whether or not to expand the value of environment variables which reference other variables (e.g. “this-${THAT}”).

ENV_VAR_REGEX = re.compile('\\${([^\\{\\}]+?)}|\\$([a-zA-Z_]+[a-zA-Z0-9_]*?)')
alias(key, value)[source]
appendenv(key, value)[source]

This is optional, but if it is not implemented, you must implement setenv.

command(value)[source]
comment(value)[source]
error(value)[source]
escape_string(value, is_path=False)[source]

Escape a string.

Escape the given string so that special characters (such as quotes and whitespace) are treated properly. If value is a string, assume that this is an expandable string in this interpreter.

Note that is_path provided because of the special case where a path-like envvar is set. In this case, path normalization, if it needs to occur, has to be part of the string escaping process.

Note

This default implementation returns the string with no escaping applied.

Parameters
  • value (str or EscapedString) – String to escape.

  • is_path (bool) – True if the value is path-like.

Returns

The escaped string.

Return type

str

expand_env_vars = False
get_output(style=<OutputStyle.file: ('Code as it would appear in a script file.', )>)[source]

Returns any implementation specific data.

Parameters

style (OutputStyle) – Style affecting output format.

Returns

Depends on implementation, but usually a code string.

info(value)[source]
normalize_path(path)[source]

Normalize a path.

Change path to a valid filepath representation for this interpreter.

IMPORTANT: Because var references like ${THIS} might be passed to funcs like appendvar, path might be in this form. You need to take that into account (ie, ensure normalization doesn’t break such a var reference).

Parameters

path (str) – A filepath which may be in posix format, or windows format, or some combination of the two. For eg, a string like {root}/bin on windows will evaluate to C:.../bin - in this case, the cmd shell would want to normalize this and convert to all forward slashes.

Returns

The normalized path.

Return type

str

normalize_paths(value)[source]

Normalize value if it’s a path(s).

Note that value may be more than one pathsep-delimited paths.

pathsep = ':'
prependenv(key, value)[source]

This is optional, but if it is not implemented, you must implement setenv.

resetenv(key, value, friends=None)[source]
setenv(key, value)[source]
shebang()[source]
source(value)[source]
unsetenv(key)[source]
class rez.rex.ActionManager(interpreter, parent_environ=None, parent_variables=None, formatter=None, verbose=False, env_sep_map=None)[source]

Bases: object

Handles the execution book-keeping. Tracks env variable values, and triggers the callbacks of the ActionInterpreter.

alias(key, value)[source]
appendenv(key, value)[source]
command(value)[source]
comment(value)[source]
defined(key)[source]
error(value)[source]
expandvars(value, format=True)[source]
get_action_methods()[source]

return a list of methods on this class for executing actions. methods are return as a list of (name, func) tuples

get_output(style=<OutputStyle.file: ('Code as it would appear in a script file.', )>)[source]
get_public_methods()[source]

return a list of methods on this class which should be exposed in the rex API.

getenv(key)[source]
info(value='')[source]
prependenv(key, value)[source]
resetenv(key, value, friends=None)[source]
setenv(key, value)[source]
shebang()[source]
source(value)[source]
stop(msg, *nargs)[source]
undefined(key)[source]
unsetenv(key)[source]
class rez.rex.Alias(*args)[source]

Bases: rez.rex.Action

name = 'alias'
class rez.rex.Appendenv(*args)[source]

Bases: rez.rex.Setenv

name = 'appendenv'
class rez.rex.Command(*args)[source]

Bases: rez.rex.Action

name = 'command'
class rez.rex.Comment(*args)[source]

Bases: rez.rex.Action

name = 'comment'
class rez.rex.EnvAction(*args)[source]

Bases: rez.rex.Action

property key
property value
class rez.rex.EnvironmentDict(manager)[source]

Bases: collections.abc.MutableMapping

Provides a mapping interface to EnvironmentVariable instances, which provide an object-oriented interface for recording environment variable manipulations.

__getitem__ is always guaranteed to return an EnvironmentVariable instance: it will not raise a KeyError.

keys() a set-like object providing a view on D's keys[source]
class rez.rex.EnvironmentVariable(name, environ_map)[source]

Bases: object

class representing an environment variable

combined with EnvironmentDict class, records changes to the environment

append(value)[source]
get()[source]
property name
prepend(value)[source]
reset(value, friends=None)[source]
set(value)[source]
setdefault(value)[source]

set value if the variable does not yet exist

unset()[source]
value()[source]
class rez.rex.Error(*args)[source]

Bases: rez.rex.Action

name = 'error'
class rez.rex.EscapedString(value, is_literal=False)[source]

Bases: object

Class for constructing literal or expandable strings, or a combination of both.

This determines how a string is escaped in an interpreter. For example, the following rex commands may result in the bash code shown:

>>> env.FOO = literal('oh "noes"')
>>> env.BAH = expandable('oh "noes"')
export FOO='oh "noes"'
export BAH="oh "noes""

You do not need to use expandable - a string by default is interpreted as expandable. However you can mix literals and expandables together, like so:

>>> env.FOO = literal("hello").expandable(" ${DUDE}")
export FOO='hello'" ${DUDE}"

Shorthand methods e and l are also supplied, for better readability:

>>> env.FOO = literal("hello").e(" ${DUDE}").l(", and welcome!")
export FOO='hello'" ${DUDE}"', and welcome!'

Note

you can use the literal and expandable free functions, rather than constructing a class instance directly.

copy()[source]
classmethod demote(value)[source]
classmethod disallow(value)[source]
e(value)[source]
expandable(value)[source]
expanduser()[source]

Analogous to os.path.expanduser.

Returns

EscapedString object with expanded ‘~’ references.

formatted(func)[source]

Return the string with non-literal parts formatted.

Parameters

func (callable) – Callable that translates a string into a formatted string.

Returns

EscapedString object.

classmethod join(sep, values)[source]
l(value)[source]
literal(value)[source]
classmethod promote(value)[source]
split(delimiter=None)[source]

Same as string.split(), but retains literal/expandable structure.

Returns

List of EscapedString.

class rez.rex.Info(*args)[source]

Bases: rez.rex.Action

name = 'info'
class rez.rex.NamespaceFormatter(namespace)[source]

Bases: string.Formatter

String formatter that, as well as expanding ‘{variable}’ strings, also protects environment variable references such as ${THIS} so they do not get expanded as though {THIS} is a formatting target. Also, environment variable references such as $THIS are converted to ${THIS}, which gives consistency across shells, and avoids some problems with non-curly-braced variables in some situations.

format(format_string, *args, **kwargs)[source]
format_field(value, format_spec)[source]
get_value(key, args, kwds)[source]
class rez.rex.OutputStyle(value, names=None, module=None, type=None)[source]

Bases: rez.vendor.enum.Enum

Enum to represent the style of code output when using Rex.

class rez.rex.Prependenv(*args)[source]

Bases: rez.rex.Setenv

name = 'prependenv'
class rez.rex.Python(target_environ=None, passive=False)[source]

Bases: rez.rex.ActionInterpreter

Execute commands in the current python session

adjust_env_for_platform(env)[source]

Make required platform-specific adjustments to env.

alias(key, value)[source]
appendenv(key, value)[source]

This is optional, but if it is not implemented, you must implement setenv.

apply_environ()[source]

Apply changes to target environ.

command(value)[source]
comment(value)[source]
error(value)[source]
expand_env_vars = True
get_key_token(key)[source]
get_output(style=<OutputStyle.file: ('Code as it would appear in a script file.', )>)[source]

Returns any implementation specific data.

Parameters

style (OutputStyle) – Style affecting output format.

Returns

Depends on implementation, but usually a code string.

info(value)[source]
prependenv(key, value)[source]

This is optional, but if it is not implemented, you must implement setenv.

resetenv(key, value, friends=None)[source]
set_manager(manager)[source]
setenv(key, value)[source]
shebang()[source]
source(value)[source]
subprocess(args, **subproc_kwargs)[source]
unsetenv(key)[source]
class rez.rex.Resetenv(*args)[source]

Bases: rez.rex.EnvAction

property friends
name = 'resetenv'
post_exec(interpreter, result)[source]
pre_exec(interpreter)[source]
class rez.rex.RexExecutor(interpreter=None, globals_map=None, parent_environ=None, parent_variables=None, shebang=True, add_default_namespaces=True)[source]

Bases: object

Runs an interpreter over code within the given namespace. You can also access namespaces and rex functions directly in the executor, like so:

RexExecutor ex() ex.setenv(‘FOO’, ‘BAH’) ex.env.FOO_SET = 1 ex.alias(‘foo’,’foo -l’)

property actions

List of Action objects that will be executed.

append_rez_path()[source]

Append rez path to $PATH.

append_system_paths()[source]

Append system paths to $PATH.

bind(name, obj)[source]

Binds an object to the execution context.

Parameters
  • name (str) –

  • obj (object) – Object to bind.

classmethod compile_code(code, filename=None, exec_namespace=None)[source]

Compile and possibly execute rex code.

Parameters
  • code (str or SourceCode) – The python code to compile.

  • filename (str) – File to associate with the code, will default to ‘<string>’.

  • exec_namespace (dict) – Namespace to execute the code in. If None, the code is not executed.

Returns

Compiled code object.

execute_code(code, filename=None, isolate=False)[source]

Execute code within the execution context.

Parameters
  • code (str or SourceCode) – Rex code to execute.

  • filename (str) – Filename to report if there are syntax errors.

  • isolate (bool) – If True, do not affect self.globals by executing this code. DEPRECATED - use self.reset_globals instead.

execute_function(func, *nargs, **kwargs)[source]

Execute a function object within the execution context. @returns The result of the function call.

expand(value)[source]
get_output(style=<OutputStyle.file: ('Code as it would appear in a script file.', )>)[source]

Returns the result of all previous calls to execute_code.

property interpreter
normalize_path(path)[source]

Normalize a path.

Note that in many interpreters this will be unchanged.

Returns

The normalized path.

Return type

str

prepend_rez_path()[source]

Prepend rez path to $PATH.

reset_globals()[source]

Remove changes to globals dict post-context.

Any bindings (self.bind) will only be visible during this context.

unbind(name)[source]

Unbind an object from the execution context.

Has no effect if the binding does not exist.

Parameters

name (str) –

class rez.rex.Setenv(*args)[source]

Bases: rez.rex.EnvAction

name = 'setenv'
post_exec(interpreter, result)[source]
pre_exec(interpreter)[source]
class rez.rex.Shebang(*args)[source]

Bases: rez.rex.Action

name = 'shebang'
class rez.rex.Source(*args)[source]

Bases: rez.rex.Action

name = 'source'
class rez.rex.Stop(*args)[source]

Bases: rez.rex.Action

name = 'stop'
class rez.rex.Unsetenv(*args)[source]

Bases: rez.rex.EnvAction

name = 'unsetenv'
rez.rex.expandable(value)[source]

Creates an expandable string.

rez.rex.literal(value)[source]

Creates a literal string.

rez.rex.optionvars(name, default=None)[source]

Access arbitrary data from rez config setting ‘optionvars’.

Parameters
  • name (str) – Name of the optionvar. Use dot notation for values in nested dicts.

  • default (object) – Default value if setting is missing.