|
| 1 | + |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import argparse |
| 5 | +import subprocess |
| 6 | + |
| 7 | +from rez.config import config as rezconfig |
| 8 | + |
| 9 | +from deliver.solve import RequestSolver |
| 10 | +from deliver.lib import clear_repo_cache |
| 11 | + |
| 12 | + |
| 13 | +class PackageInstaller(RequestSolver): |
| 14 | + """Extended from `RequestSolver` to execute installation""" |
| 15 | + |
| 16 | + def run(self): |
| 17 | + for _ in self.run_iter(): |
| 18 | + pass |
| 19 | + |
| 20 | + def run_iter(self): |
| 21 | + deliverconfig = rezconfig.plugins.command.deliver |
| 22 | + |
| 23 | + for requested in self._requirements: |
| 24 | + if requested.status != self.Ready: |
| 25 | + # TODO: prompt warning if the status is `ResolveFailed` |
| 26 | + continue |
| 27 | + |
| 28 | + if requested.source == self.loader.maker_source: |
| 29 | + self._make(requested.name, |
| 30 | + variant=requested.index) |
| 31 | + else: |
| 32 | + self._build(requested.name, |
| 33 | + os.path.dirname(requested.source), |
| 34 | + variant=requested.index) |
| 35 | + |
| 36 | + deliverconfig.on_package_deployed_callback( |
| 37 | + name=requested.name, |
| 38 | + path=self.deploy_path, |
| 39 | + ) |
| 40 | + |
| 41 | + yield requested |
| 42 | + |
| 43 | + def _make(self, name, variant=None): |
| 44 | + deploy_path = self.deploy_path |
| 45 | + if not os.path.isdir(deploy_path): |
| 46 | + os.makedirs(deploy_path) |
| 47 | + |
| 48 | + made_pkg = self.loader.get_maker_made_package(name) |
| 49 | + made_pkg.__install__(deploy_path, variant) |
| 50 | + |
| 51 | + clear_repo_cache(deploy_path) |
| 52 | + |
| 53 | + def _build(self, name, src_dir, variant=None): |
| 54 | + variant_cmd = [] if variant is None else ["--variants", str(variant)] |
| 55 | + deploy_path = self.deploy_path |
| 56 | + |
| 57 | + if not os.path.isdir(deploy_path): |
| 58 | + os.makedirs(deploy_path) |
| 59 | + |
| 60 | + if variant is not None: |
| 61 | + name += "[%d]" % variant |
| 62 | + |
| 63 | + env = os.environ.copy() |
| 64 | + cmd = [sys.executable, "-m", "deliver.install", name] |
| 65 | + |
| 66 | + if self._release: |
| 67 | + env["REZ_RELEASE_PACKAGES_PATH"] = deploy_path |
| 68 | + cmd += ["--release"] |
| 69 | + else: |
| 70 | + env["REZ_LOCAL_PACKAGES_PATH"] = deploy_path |
| 71 | + cmd += ["--install"] |
| 72 | + |
| 73 | + cmd += variant_cmd |
| 74 | + self._run_command(cmd, cwd=src_dir, env=env) |
| 75 | + |
| 76 | + clear_repo_cache(deploy_path) |
| 77 | + |
| 78 | + def _run_command(self, cmd_args, **kwargs): |
| 79 | + print("Running command:\n %s\n" % cmd_args) |
| 80 | + subprocess.check_call(cmd_args, **kwargs) |
| 81 | + |
| 82 | + |
| 83 | +def main(): |
| 84 | + from rez.cli._main import run |
| 85 | + from deliver.solve import RequestSolver |
| 86 | + from deliver.lib import override_config |
| 87 | + |
| 88 | + parser = argparse.ArgumentParser("deliver.install") |
| 89 | + parser.add_argument("PKG") |
| 90 | + parser.add_argument("--release", action="store_true") |
| 91 | + opts, remains = parser.parse_known_args() |
| 92 | + |
| 93 | + # for case like: |
| 94 | + # |
| 95 | + # `tests.test_manifest.TestManifest.test_buildtime_variants` |
| 96 | + # |
| 97 | + # which requires to scan packages to list out current available variants, |
| 98 | + # we resolve the request here again and append loader paths for including |
| 99 | + # developer packages in that scan. |
| 100 | + # |
| 101 | + solver = RequestSolver() |
| 102 | + solver.resolve(opts.PKG) |
| 103 | + |
| 104 | + # build/release |
| 105 | + # |
| 106 | + settings = { |
| 107 | + # developer packages loader paths appended, see comment above. |
| 108 | + "packages_path": solver.installed_packages_path + solver.loader.paths, |
| 109 | + } |
| 110 | + with override_config(settings): |
| 111 | + command = "release" if opts.release else "build" |
| 112 | + sys.argv = ["rez-" + command] + remains |
| 113 | + run(command) |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + main() |
0 commit comments