Skip to content

Commit

Permalink
-added support for pre_build_commands() package.py function
Browse files Browse the repository at this point in the history
  • Loading branch information
ajohns committed Dec 13, 2019
1 parent 7bf3454 commit c369436
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 66 deletions.
7 changes: 7 additions & 0 deletions src/rez/build_process_.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ def create_build_context(self, variant, build_type, build_path):
else:
packages_path = self.package.config.nonlocal_packages_path

# It is uncommon, but possible, to define the package filters in the
# developer package. Example scenario: you may want to enable visiblity
# of *.dev packages if the current package is *.dev also, for example
# (assuming you have a production-time package filter which filters out
# *.dev packages by default).
#
if self.package.config.is_overridden("package_filter"):
from rez.package_filter import PackageFilterList

Expand All @@ -239,6 +245,7 @@ def create_build_context(self, variant, build_type, build_path):
else:
package_filter = None

# create the build context
context = ResolvedContext(request,
package_paths=packages_path,
package_filter=package_filter,
Expand Down
38 changes: 26 additions & 12 deletions src/rez/build_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,21 +248,35 @@ def get_standard_vars(cls, context, variant, build_type, install,
return vars_

@classmethod
def set_standard_vars(cls, executor, context, variant, build_type,
install, build_path, install_path=None):
"""Sets a standard set of environment variables for the build system to
use
def add_standard_build_actions(cls, executor, context, variant, build_type,
install, build_path, install_path=None):
"""Perform build actions common to every build system.
This includes:
- Setting a standard list on env-vars;
- Executing pre_build_commands(), if the package has one.
"""
vars = cls.get_standard_vars(context=context,
variant=variant,
build_type=build_type,
install=install,
build_path=build_path,
install_path=install_path)

for var, value in vars.items():

# set env vars
env_vars = cls.get_standard_vars(
context=context,
variant=variant,
build_type=build_type,
install=install,
build_path=build_path,
install_path=install_path
)

for var, value in env_vars.items():
executor.env[var] = value

# execute pre_build_commands()
pre_build_commands = getattr(variant, "pre_build_commands")

if pre_build_commands:
executor.bind("this", variant)
executor.execute_code(pre_build_commands, isolate=True)


# Copyright 2013-2016 Allan Johns.
#
Expand Down
1 change: 1 addition & 0 deletions src/rez/package_maker__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
Optional('pre_commands'): _commands_schema,
Optional('commands'): _commands_schema,
Optional('post_commands'): _commands_schema,
Optional('pre_build_commands'): _commands_schema,

# attributes specific to pre-built packages
Optional("build_system"): basestring,
Expand Down
6 changes: 5 additions & 1 deletion src/rez/package_resources_.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@
"build_system",
"build_command",
"preprocess",
"pre_build_commands"
)

# package attributes that are rex-based functions
package_rex_keys = (
"pre_commands",
"commands",
"post_commands"
"post_commands",
"pre_build_commands"
)


Expand Down Expand Up @@ -131,6 +133,7 @@ def late_bound(schema):
Optional('pre_commands'): SourceCode,
Optional('commands'): SourceCode,
Optional('post_commands'): SourceCode,
Optional('pre_build_commands'): SourceCode,

# release info
Optional("timestamp"): int,
Expand Down Expand Up @@ -219,6 +222,7 @@ def late_bound(schema):
Optional('pre_commands'): _commands_schema,
Optional('commands'): _commands_schema,
Optional('post_commands'): _commands_schema,
Optional('pre_build_commands'): _commands_schema,

Optional("timestamp"): int,
Optional('revision'): object,
Expand Down
1 change: 1 addition & 0 deletions src/rez/package_serialise.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
Optional('pre_commands'): source_code_schema,
Optional('commands'): source_code_schema,
Optional('post_commands'): source_code_schema,
Optional('pre_build_commands'): source_code_schema,

Optional("help"): late_bound(help_schema),
Optional("uuid"): basestring,
Expand Down
36 changes: 13 additions & 23 deletions src/rez/resolved_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from rez.util import shlex_join, dedup, is_non_string_iterable
from rez.utils.sourcecode import SourceCodeError
from rez.utils.colorize import critical, heading, local, implicit, Printer
from rez.utils.formatting import columnise, PackageRequest, ENV_VAR_REGEX
from rez.utils.formatting import columnise, PackageRequest, ENV_VAR_REGEX, \
header_comment, minor_header_comment
from rez.utils.data_utils import deep_del
from rez.utils.filesystem import TempDirs
from rez.utils.memcached import pool_memcached_connections
Expand Down Expand Up @@ -1240,7 +1241,7 @@ def execute_shell(self, shell=None, parent_environ=None, rcfile=None,
context_file = context_filepath or \
os.path.join(tmpdir, "context.%s" % sh.file_extension())

# interpret this context and write out the native context file
# interpret this context and write out the native context (shell script) file
executor = self._create_executor(sh, parent_environ)
executor.env.REZ_RXT_FILE = rxt_file
executor.env.REZ_CONTEXT_FILE = context_file
Expand Down Expand Up @@ -1572,29 +1573,15 @@ def _get_pre_resolve_bindings(self):

@pool_memcached_connections
def _execute(self, executor):
br = '#' * 80
br_minor = '-' * 80

def _heading(txt):
executor.comment("")
executor.comment("")
executor.comment(br)
executor.comment(txt)
executor.comment(br)

def _minor_heading(txt):
executor.comment("")
executor.comment(txt)
executor.comment(br_minor)

# bind various info to the execution context
resolved_pkgs = self.resolved_packages or []
request_str = ' '.join(str(x) for x in self._package_requests)
implicit_str = ' '.join(str(x) for x in self.implicit_packages)
resolve_str = ' '.join(x.qualified_package_name for x in resolved_pkgs)
package_paths_str = os.pathsep.join(self.package_paths)

_heading("system setup")
header_comment(executor, "system setup")

executor.setenv("REZ_USED", self.rez_path)
executor.setenv("REZ_USED_VERSION", self.rez_version)
executor.setenv("REZ_USED_TIMESTAMP", str(self.timestamp))
Expand Down Expand Up @@ -1630,13 +1617,16 @@ def _minor_heading(txt):
# -- apply each resolved package to the execution context
#

_heading("package variables")
header_comment(executor, "package variables")

# TODO this is not having any effect. Below, a RexError is getting
# raised on bad commands code, not a SourceCodeError
error_class = SourceCodeError if config.catch_rex_errors else None

# set basic package variables and create per-package bindings
bindings = {}
for pkg in resolved_pkgs:
_minor_heading("variables for package %s" % pkg.qualified_name)
minor_header_comment(executor, "variables for package %s" % pkg.qualified_name)
prefix = "REZ_" + pkg.name.upper().replace('.', '_')

executor.setenv(prefix + "_VERSION", str(pkg.version))
Expand All @@ -1661,9 +1651,9 @@ def _minor_heading(txt):
continue
if not found:
found = True
_heading(attr)
header_comment(executor, attr)

_minor_heading("%s from package %s" % (attr, pkg.qualified_name))
minor_header_comment(executor, "%s from package %s" % (attr, pkg.qualified_name))
bindings_ = bindings[pkg.name]
executor.bind('this', bindings_["variant"])
executor.bind("version", bindings_["version"])
Expand All @@ -1688,7 +1678,7 @@ def _minor_heading(txt):

raise PackageCommandError(msg)

_heading("post system setup")
header_comment(executor, "post system setup")

# append suite paths based on suite visibility setting
self._append_suite_paths(executor)
Expand Down
2 changes: 1 addition & 1 deletion src/rez/utils/_version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@


# Update this value to version up Rez. Do not place anything else in this file.
_rez_version = "2.50.0"
_rez_version = "2.51.0"


# Copyright 2013-2016 Allan Johns.
Expand Down
24 changes: 24 additions & 0 deletions src/rez/utils/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,30 @@ def as_block_string(txt):
return '"""\n%s\n"""' % '\n'.join(lines)


_header_br = '#' * 80
_header_br_minor = '-' * 80


def header_comment(executor, txt):
"""Convenience for creating header-like comment in a rex executor.
Args:
executor (`RexExecutor`): Executor.
txt (str): Comment text.
"""
executor.comment("")
executor.comment("")
executor.comment(_header_br)
executor.comment(txt)
executor.comment(_header_br)


def minor_header_comment(executor, txt):
executor.comment("")
executor.comment(txt)
executor.comment(_header_br_minor)


# Copyright 2013-2016 Allan Johns.
#
# This library is free software: you can redistribute it and/or
Expand Down
20 changes: 11 additions & 9 deletions src/rezplugins/build_system/bez.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,22 @@ def build(self, context, variant, build_path, install_path, install=False,
retcode, _, _ = context.execute_shell(command=cmd,
block=True,
cwd=build_path,
actions_callback=callback)
post_actions_callback=callback)
ret["success"] = (not retcode)
return ret

@classmethod
def _add_build_actions(cls, executor, context, package, variant,
build_type, install, build_path, install_path=None):
cls.set_standard_vars(executor=executor,
context=context,
variant=variant,
build_type=build_type,
install=install,
build_path=build_path,
install_path=install_path)
cls.add_standard_build_actions(
executor=executor,
context=context,
variant=variant,
build_type=build_type,
install=install,
build_path=build_path,
install_path=install_path
)


def _FWD__spawn_build_shell(working_dir, build_path, variant_index, install,
Expand All @@ -131,7 +133,7 @@ def _FWD__spawn_build_shell(working_dir, build_path, variant_index, install,
install_path=install_path)

retcode, _, _ = context.execute_shell(block=True, cwd=build_path,
actions_callback=callback)
post_actions_callback=callback)
sys.exit(retcode)


Expand Down
24 changes: 13 additions & 11 deletions src/rezplugins/build_system/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def _pr(s):
retcode, _, _ = context.execute_shell(command=cmd,
block=True,
cwd=build_path,
actions_callback=callback)
post_actions_callback=callback)
ret = {}
if retcode:
ret["success"] = False
Expand Down Expand Up @@ -208,7 +208,7 @@ def _pr(s):
retcode, _, _ = context.execute_shell(command=cmd,
block=True,
cwd=build_path,
actions_callback=callback)
post_actions_callback=callback)

if not retcode and install and "install" not in cmd:
cmd.append("install")
Expand All @@ -218,7 +218,7 @@ def _pr(s):
retcode, _, _ = context.execute_shell(command=cmd,
block=True,
cwd=build_path,
actions_callback=callback)
post_actions_callback=callback)

ret["success"] = (not retcode)
return ret
Expand All @@ -230,13 +230,15 @@ def _add_build_actions(cls, executor, context, package, variant,
cmake_path = os.path.join(os.path.dirname(__file__), "cmake_files")
template_path = os.path.join(os.path.dirname(__file__), "template_files")

cls.set_standard_vars(executor=executor,
context=context,
variant=variant,
build_type=build_type,
install=install,
build_path=build_path,
install_path=install_path)
cls.add_standard_build_actions(
executor=executor,
context=context,
variant=variant,
build_type=build_type,
install=install,
build_path=build_path,
install_path=install_path
)

executor.env.CMAKE_MODULE_PATH.append(cmake_path.replace('\\', '/'))
executor.env.REZ_BUILD_DOXYFILE = os.path.join(template_path, 'Doxyfile')
Expand All @@ -262,7 +264,7 @@ def _FWD__spawn_build_shell(working_dir, build_path, variant_index, install,

retcode, _, _ = context.execute_shell(block=True,
cwd=build_path,
actions_callback=callback)
post_actions_callback=callback)
sys.exit(retcode)


Expand Down
20 changes: 11 additions & 9 deletions src/rezplugins/build_system/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,22 @@ def _callback(executor):
retcode, _, _ = context.execute_shell(command=command,
block=True,
cwd=build_path,
actions_callback=_callback)
post_actions_callback=_callback)
ret["success"] = (not retcode)
return ret

@classmethod
def _add_build_actions(cls, executor, context, package, variant,
build_type, install, build_path, install_path=None):
cls.set_standard_vars(executor=executor,
context=context,
variant=variant,
build_type=build_type,
install=install,
build_path=build_path,
install_path=install_path)
cls.add_standard_build_actions(
executor=executor,
context=context,
variant=variant,
build_type=build_type,
install=install,
build_path=build_path,
install_path=install_path
)


def _FWD__spawn_build_shell(working_dir, build_path, variant_index, install,
Expand All @@ -214,7 +216,7 @@ def _FWD__spawn_build_shell(working_dir, build_path, variant_index, install,
install_path=install_path)

retcode, _, _ = context.execute_shell(block=True, cwd=build_path,
actions_callback=callback)
post_actions_callback=callback)
sys.exit(retcode)


Expand Down

0 comments on commit c369436

Please sign in to comment.