Skip to content

Commit

Permalink
Add update --recreate option
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoshanuikabundi committed Aug 26, 2022
1 parent c906164 commit 0f7e2e7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
10 changes: 9 additions & 1 deletion soap/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def update(
None,
help="Environment to update. If not specified, all environments will be updated.",
),
recreate: bool = Option(
False,
help="Delete and recreate the environment(s), rather than attempting to update in place",
),
):
"""
Update Conda environments.
Expand All @@ -96,7 +100,11 @@ def update(
+ f"from '{this_env.yml_path}' "
+ f"in '{this_env.env_path}':\u001b[0m"
)
soap.prepare_env(this_env, ignore_cache=True)
soap.prepare_env(
this_env,
ignore_cache=True,
allow_update=not recreate,
)


@app.command()
Expand Down
16 changes: 13 additions & 3 deletions soap/_soap.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import filecmp


def prepare_env(env: Env, ignore_cache=False):
def prepare_env(
env: Env,
ignore_cache: bool = False,
allow_update: bool = True,
):
"""
Prepare the provided environment
Expand All @@ -18,6 +22,9 @@ def prepare_env(env: Env, ignore_cache=False):
If True, rebuild the environment even if the cache suggests it is
up-to-date. If False, only rebuild the environment when the YAML file
itself has changed since the last build.
allow_update
If ``True``, attempt to update an existing environment. If ``False``,
delete and recreate an existing environment.
"""
path_yml = env.yml_path
path_env = env.env_path
Expand All @@ -29,9 +36,12 @@ def prepare_env(env: Env, ignore_cache=False):
or (not filecmp.cmp(path_cached_yml, path_yml))
):
soap.conda.env_from_file(
path_yml, path_env, install_current=env.install_current
path_yml,
path_env,
install_current=env.install_current,
allow_update=allow_update,
)
shutil.copy2(path_yml, path_env)
shutil.copy2(path_yml, path_cached_yml)


def run_in_env(args: Sequence[str], env: Env):
Expand Down
10 changes: 9 additions & 1 deletion soap/conda.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def env_from_file(
file: Union[str, Path],
env_path: Union[str, Path],
install_current=False,
allow_update: bool = True,
):
"""
Create or update an enviroment from a Conda environment YAML file
Expand All @@ -70,8 +71,15 @@ def env_from_file(
install_current
If ``True``, the root directory of the current Git repository will be
installed in dev mode with `pip install -e`
allow_update
If ``True``, attempt to update an existing environment. If ``False``,
delete and recreate an existing environment.
"""
if Path(env_path).exists():
env_path = Path(env_path)
if not allow_update:
env_path.unlink(missing_ok=True)

if env_path.exists() and allow_update:
conda(
[
"env",
Expand Down

0 comments on commit 0f7e2e7

Please sign in to comment.