def __execute_steps(self, pi: PackageIdentifier, ipmap: dict, se_func: callable, env: Environment = None): # Find the package ip = find_manifest(pi, ipmap) # The environment if env is None: env = Environment.build(self.build_builtin_environment(), self.build_user_environment()) # build the dependencies deps = DependencyUtils.installed([pi], ipmap, env=env, ignore_unknown=True) # Update env env.append(self.build_packages_environment(deps)) # Fix PREREQ_ROOT env.set_variable("LEAF_PREREQ_ROOT", self.install_folder) # The Variable resolver vr = VariableResolver(ip, ipmap.values()) # Execute steps se = StepExecutor(self.logger, ip, vr, env=env) se_func(se)
def test_multiple(self): env = Environment("my env 1", content=[("A1", "a1"), ("B1", "b1")], in_files=["/tmp/a1.in", "/tmp/b1.in"], out_files=["/tmp/a1.out", "/tmp/b1.out"]) env.append( Environment("my env 2", content=[("A2", "a2"), ("B2", "b2")], in_files=["/tmp/a2.in", "/tmp/b2.in"], out_files=["/tmp/a2.out", "/tmp/b2.out"])) env.append( Environment("my env 3", in_files=["/tmp/a3.in", "/tmp/b3.in"], out_files=["/tmp/a3.out", "/tmp/b3.out"])) env.append( Environment("my env 4", content=[("A4", "a4"), ("B4", "b4")], in_files=["/tmp/a4.in", "/tmp/b4.in"])) env.append( Environment("my env 5", content=[("A5", "a5"), ("B5", "b5")], out_files=["/tmp/a5.out", "/tmp/b5.out"])) env.append(Environment("my env 6")) with self.assertStdout(template_out="activate.out"): env.activate( comment_consumer=lambda l: print( Environment.tostring_comment(l)), kv_consumer=lambda k, v: print( Environment.tostring_export(k, v)), file_consumer=lambda f: print(Environment.tostring_file(f)), ) with self.assertStdout(template_out="deactivate.out"): env.deactivate( comment_consumer=lambda l: print( Environment.tostring_comment(l)), kv_consumer=lambda k, v: print( Environment.tostring_export(k, v)), file_consumer=lambda f: print(Environment.tostring_file(f)), ) env.generate_scripts( activate_file=self.volatile_folder / "activate.sh", deactivate_file=self.volatile_folder / "deactivate.sh") self.assertFileContentEquals(self.volatile_folder / "activate.sh", "activate.out") self.assertFileContentEquals(self.volatile_folder / "deactivate.sh", "deactivate.out")
def install_prereq(self, pilist: list, tmp_install_folder: Path, apmap: dict = None, env: Environment = None, raise_on_error: bool = True): """ Install given prereg available package in alternative root folder @return: error count """ if apmap is None: apmap = self.list_available_packages() # Get packages to install aplist = [find_manifest(pi, apmap) for pi in pilist] errors = 0 if len(aplist) > 0: self.logger.print_verbose( "Installing {count} pre-required package(s) in {folder}". format(count=len(aplist), folder=tmp_install_folder)) if env is None: env = Environment.build(self.build_builtin_environment(), self.build_user_environment()) env.append( Environment("Prereq", {"LEAF_PREREQ_ROOT": tmp_install_folder})) for prereqap in aplist: try: prereqla = self.__download_ap(prereqap) prereqip = self.__extract_artifact( prereqla, env, tmp_install_folder, keep_folder_on_error=True) self.logger.print_verbose( "Prereq package {ip.identifier} is OK".format( ip=prereqip)) except Exception as e: if raise_on_error: raise e self.logger.print_verbose( "Prereq package {ap.identifier} has error: {error}". format(ap=prereqap, error=e)) errors += 1 return errors
def __execute(self, step, label): command = step[JsonConstants.STEP_EXEC_COMMAND] command = list(map(self.__vr.resolve, command)) command_text = " ".join(command) self.__logger.print_verbose("Execute: {command}".format(command=command_text)) env = Environment() env.append(self.__env) env.append(Environment(content=step.get(JsonConstants.STEP_EXEC_ENV))) verbose = step.get(JsonConstants.STEP_EXEC_VERBOSE, False) rc = execute_command(*command, cwd=self.__target_folder, env=env, print_stdout=verbose or self.__logger.isverbose()) if rc != 0: self.__logger.print_verbose("Command '{command}' exited with {rc}".format(command=command_text, rc=rc)) if step.get(JsonConstants.STEP_IGNORE_FAIL, False): self.__logger.print_verbose("Step ignores failure") else: raise LeafException("Error during {label} step for {ip.identifier} (command returned {rc})".format(label=label, ip=self.__package, rc=rc))
def test_envprovider(self): class MyEnvProvider(IEnvProvider): def _getenvmap(self): return {"AaaA": "a b c"} def _getenvinfiles(self): return ["/tmp/a.in", "/tmp/b.in"] def _getenvoutfiles(self): return ["/tmp/a.out", "/tmp/b.out"] env = Environment("my env 1", content=[("A1", "a1"), ("B1", "b1")], in_files=["/tmp/a1.in", "/tmp/b1.in"], out_files=["/tmp/a1.out", "/tmp/b1.out"]) env.append( MyEnvProvider("My custom env").build_environment( vr=lambda x: x.upper())) with self.assertStdout(template_out="activate.out"): env.activate( comment_consumer=lambda l: print( Environment.tostring_comment(l)), kv_consumer=lambda k, v: print( Environment.tostring_export(k, v)), file_consumer=lambda f: print(Environment.tostring_file(f)), ) with self.assertStdout(template_out="deactivate.out"): env.deactivate( comment_consumer=lambda l: print( Environment.tostring_comment(l)), kv_consumer=lambda k, v: print( Environment.tostring_export(k, v)), file_consumer=lambda f: print(Environment.tostring_file(f)), ) env.generate_scripts( activate_file=self.volatile_folder / "activate.sh", deactivate_file=self.volatile_folder / "deactivate.sh") self.assertFileContentEquals(self.volatile_folder / "activate.sh", "activate.out") self.assertFileContentEquals(self.volatile_folder / "deactivate.sh", "deactivate.out")
def build_packages_environment(self, items: list, ipmap=None): """ Get the env vars declared by given packages @param items: a list of InstalledPackage or PackageIdentifier """ ipmap = ipmap or self.list_installed_packages() out = Environment() for item in items: ip = None if isinstance(item, InstalledPackage): ip = item elif isinstance(item, PackageIdentifier): ip = None if is_latest_package(item): ip = find_manifest(item, ipmap) else: ip = ipmap.get(item) if ip is None: raise InvalidPackageNameException(item) else: raise InvalidPackageNameException(item) vr = VariableResolver(ip, ipmap.values()) out.append(ip.build_environment(vr=vr.resolve)) return out
def get_setting_value(self, setting_id: str, user_env=None, ws_env=None, pf_env=None) -> str: setting = self.get_setting(setting_id) # build env env = Environment() if Scope.USER in setting.scopes: env.append(user_env or self.build_user_environment()) if self.is_initialized: if Scope.WORKSPACE in setting.scopes: env.append(ws_env or self.build_ws_environment()) if Scope.PROFILE in setting.scopes: try: env.append(pf_env or self.get_profile( self.current_profile_name).build_environment()) except NoProfileSelected: pass # Search the setting value return env.find_setting(setting)