|
|
|
@@ -22,13 +22,13 @@ def remove_enviroment(conda_env): |
|
|
|
system_execute(args=["conda", "env", "remove", "-n", f"{conda_env}"]) |
|
|
|
|
|
|
|
|
|
|
|
def install_environment(zip_path, conda_env): |
|
|
|
def install_environment(learnware_dirpath, conda_env): |
|
|
|
"""Install environment of a learnware |
|
|
|
|
|
|
|
Parameters |
|
|
|
---------- |
|
|
|
zip_path : str |
|
|
|
Path of the learnware zip file |
|
|
|
learnware_dirpath : str |
|
|
|
Path of the learnware folder |
|
|
|
conda_env : str |
|
|
|
a new conda environment will be created with the given name; |
|
|
|
|
|
|
|
@@ -38,48 +38,45 @@ def install_environment(zip_path, conda_env): |
|
|
|
Lack of the environment configuration file. |
|
|
|
""" |
|
|
|
with tempfile.TemporaryDirectory(prefix="learnware_") as tempdir: |
|
|
|
with zipfile.ZipFile(file=zip_path, mode="r") as z_file: |
|
|
|
logger.info(f"zip_file namelist: {z_file.namelist()}") |
|
|
|
if "environment.yaml" in z_file.namelist(): |
|
|
|
z_file.extract(member="environment.yaml", path=tempdir) |
|
|
|
yaml_path: str = os.path.join(tempdir, "environment.yaml") |
|
|
|
yaml_path_filter: str = os.path.join(tempdir, "environment_filter.yaml") |
|
|
|
logger.info(f"checking the avaliabe conda packages for {conda_env}") |
|
|
|
filter_nonexist_conda_packages_file(yaml_file=yaml_path, output_yaml_file=yaml_path_filter) |
|
|
|
# create environment |
|
|
|
logger.info(f"create conda env [{conda_env}] according to .yaml file") |
|
|
|
system_execute( |
|
|
|
args=["conda", "env", "create", "--name", f"{conda_env}", "--file", f"{yaml_path_filter}"] |
|
|
|
) |
|
|
|
logger.info(f"zip_file namelist: {os.listdir(learnware_dirpath)}") |
|
|
|
if "environment.yaml" in os.listdir(learnware_dirpath): |
|
|
|
yaml_path: str = os.path.join(learnware_dirpath, "environment.yaml") |
|
|
|
yaml_path_filter: str = os.path.join(tempdir, "environment_filter.yaml") |
|
|
|
logger.info(f"checking the avaliabe conda packages for {conda_env}") |
|
|
|
filter_nonexist_conda_packages_file(yaml_file=yaml_path, output_yaml_file=yaml_path_filter) |
|
|
|
# create environment |
|
|
|
logger.info(f"create conda env [{conda_env}] according to .yaml file") |
|
|
|
system_execute( |
|
|
|
args=["conda", "env", "create", "--name", f"{conda_env}", "--file", f"{yaml_path_filter}"] |
|
|
|
) |
|
|
|
|
|
|
|
elif "requirements.txt" in z_file.namelist(): |
|
|
|
z_file.extract(member="requirements.txt", path=tempdir) |
|
|
|
requirements_path: str = os.path.join(tempdir, "requirements.txt") |
|
|
|
requirements_path_filter: str = os.path.join(tempdir, "requirements_filter.txt") |
|
|
|
logger.info(f"checking the avaliabe pip packages for {conda_env}") |
|
|
|
filter_nonexist_pip_packages_file( |
|
|
|
requirements_file=requirements_path, output_file=requirements_path_filter |
|
|
|
) |
|
|
|
logger.info(f"create empty conda env [{conda_env}]") |
|
|
|
system_execute(args=["conda", "create", "-y", "--name", f"{conda_env}", "python=3.8"]) |
|
|
|
logger.info(f"install pip requirements for conda env [{conda_env}]") |
|
|
|
system_execute( |
|
|
|
args=[ |
|
|
|
"conda", |
|
|
|
"run", |
|
|
|
"-n", |
|
|
|
f"{conda_env}", |
|
|
|
"--no-capture-output", |
|
|
|
"python3", |
|
|
|
"-m", |
|
|
|
"pip", |
|
|
|
"install", |
|
|
|
"-r", |
|
|
|
f"{requirements_path_filter}", |
|
|
|
] |
|
|
|
) |
|
|
|
else: |
|
|
|
raise Exception("Environment.yaml or requirements.txt not found in the learnware zip file.") |
|
|
|
elif "requirements.txt" in os.listdir(learnware_dirpath): |
|
|
|
requirements_path: str = os.path.join(learnware_dirpath, "requirements.txt") |
|
|
|
requirements_path_filter: str = os.path.join(tempdir, "requirements_filter.txt") |
|
|
|
logger.info(f"checking the avaliabe pip packages for {conda_env}") |
|
|
|
filter_nonexist_pip_packages_file( |
|
|
|
requirements_file=requirements_path, output_file=requirements_path_filter |
|
|
|
) |
|
|
|
logger.info(f"create empty conda env [{conda_env}]") |
|
|
|
system_execute(args=["conda", "create", "-y", "--name", f"{conda_env}", "python=3.8"]) |
|
|
|
logger.info(f"install pip requirements for conda env [{conda_env}]") |
|
|
|
system_execute( |
|
|
|
args=[ |
|
|
|
"conda", |
|
|
|
"run", |
|
|
|
"-n", |
|
|
|
f"{conda_env}", |
|
|
|
"--no-capture-output", |
|
|
|
"python3", |
|
|
|
"-m", |
|
|
|
"pip", |
|
|
|
"install", |
|
|
|
"-r", |
|
|
|
f"{requirements_path_filter}", |
|
|
|
] |
|
|
|
) |
|
|
|
else: |
|
|
|
raise Exception("Environment.yaml or requirements.txt not found in the learnware folder.") |
|
|
|
|
|
|
|
logger.info(f"install learnware package for conda env [{conda_env}]") |
|
|
|
system_execute( |
|
|
|
|