def _execute_in_conda_env(conda_env_path, command, install_mlflow, command_env=None): if command_env is None: command_env = os.environ env_id = os.environ.get("MLFLOW_HOME", VERSION) if install_mlflow else None conda_env_name = _get_or_create_conda_env(conda_env_path, env_id=env_id) activate_path = _get_conda_bin_executable("activate") activate_conda_env = [ "source {0} {1}".format(activate_path, conda_env_name) ] if install_mlflow: if "MLFLOW_HOME" in os.environ: # dev version install_mlflow = "pip install -e {} 1>&2".format( os.environ["MLFLOW_HOME"]) else: install_mlflow = "pip install mlflow=={} 1>&2".format(VERSION) activate_conda_env += [install_mlflow] command = " && ".join(activate_conda_env + [command]) _logger.info("=== Running command '%s'", command) child = subprocess.Popen(["bash", "-c", command], close_fds=True, env=command_env) rc = child.wait() if rc != 0: raise Exception( "Command '{0}' returned non zero return code. Return code = {1}". format(command, rc))
def can_score_model(self): if self._no_conda: return True # already in python; dependencies are assumed to be installed (no_conda) conda_path = _get_conda_bin_executable("conda") try: p = subprocess.Popen([conda_path, "--version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) _, _ = p.communicate() return p.wait() == 0 except FileNotFoundError: # Can not find conda return False
def _rerun_in_conda(conda_env_path): """ Rerun CLI command inside a to-be-created conda environment.""" conda_env_name = _get_or_create_conda_env(conda_env_path) activate_path = _get_conda_bin_executable("activate") commands = [] commands.append("source {} {}".format(activate_path, conda_env_name)) safe_argv = [shlex_quote(arg) for arg in sys.argv] commands.append(" ".join(safe_argv) + " --no-conda") commandline = " && ".join(commands) eprint("=== Running command '{}'".format(commandline)) child = subprocess.Popen(["bash", "-c", commandline], close_fds=True) exit_code = child.wait() return exit_code
def _execute_in_conda_env(conda_env_path, command): conda_env_name = _get_or_create_conda_env(conda_env_path) activate_path = _get_conda_bin_executable("activate") command = " && ".join([ "source {} {}".format(activate_path, conda_env_name), "pip install mlflow 1>&2", command ]) _logger.info("=== Running command '%s'", command) child = subprocess.Popen(["bash", "-c", command], close_fds=True) rc = child.wait() if rc != 0: raise Exception( "Command '{0}' returned non zero return code. Return code = {1}". format(command, rc))