Skip to content

Commit

Permalink
Merge pull request #77 from mariusvniekerk/issue-73
Browse files Browse the repository at this point in the history
Make condax install idempotent
  • Loading branch information
mariusvniekerk authored Aug 8, 2024
2 parents 553d51e + 868dd35 commit 6e31d0b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
minimum_pre_commit_version: '2.9.0'
repos:
- repo: https://github.com/Zac-HD/shed
rev: 0.10.5
rev: 2024.3.1
hooks:
- id: shed
# args: [--refactor, --py39-plus]
types_or: [python, markdown, rst]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.982
rev: v1.11.0
hooks:
- id: mypy
additional_dependencies: [types-requests, types-PyYAML]
4 changes: 2 additions & 2 deletions condax/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


def write_activating_entrypoint_unix(executable, prefix):
template = dedent(
dedent(
f"""
#!/bin/sh
export PATH="$PATH:{prefix}/bin"
Expand All @@ -14,7 +14,7 @@ def write_activating_entrypoint_unix(executable, prefix):

def write_activating_entrypoint_windows(executable, prefix):
# TODO: Verify if this is right?
template = dedent(
dedent(
f"""
SET PATH="%PATH%;{prefix}\\Library\\bin;{prefix}\\Scripts"
Expand Down
17 changes: 16 additions & 1 deletion condax/conda.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import enum
import json
import logging
import os
Expand Down Expand Up @@ -28,9 +29,17 @@ def write_condarc_to_prefix(
fo.write("\n")


class CreateResult(enum.Enum):
CREATED = enum.auto()
ALREADY_EXISTS = enum.auto()


def create_conda_environment(
package: str, channels: Optional[List[str]] = None
) -> None:
) -> CreateResult:
if conda_environment_exists(package):
return CreateResult.ALREADY_EXISTS

conda_exe = CONFIG.conda_executable
assert conda_exe is not None
prefix = conda_env_prefix(package)
Expand All @@ -56,6 +65,12 @@ def create_conda_environment(
)

write_condarc_to_prefix(prefix, channels)
return CreateResult.CREATED


def conda_environment_exists(package: str):
prefix = conda_env_prefix(package)
return prefix.exists()


def install_conda_packages(
Expand Down
17 changes: 12 additions & 5 deletions condax/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,14 @@ def install_package(
) -> None:
if channels is None:
channels = CONFIG.channels
conda.create_conda_environment(package, channels=channels)
res = conda.create_conda_environment(package, channels=channels)
if res == conda.CreateResult.ALREADY_EXISTS:
typer.secho(
f"`{package}` already installed, skipping installation",
err=True,
fg=typer.colors.YELLOW,
)
return
executables_to_link = conda.determine_executables_from_env(package)
CONFIG.link_destination.mkdir(parents=True, exist_ok=True)
create_links(
Expand Down Expand Up @@ -205,12 +212,12 @@ def inject_packages(
CONFIG.link_destination.mkdir(parents=True, exist_ok=True)
create_links(executables_to_link, link_conflict_action, env_prefix=prefix)
with prefix_metadata(prefix) as metadata:
metadata.injected_packages = list(
sorted(set(metadata.injected_packages) | set(extra_packages))
metadata.injected_packages = sorted(
set(metadata.injected_packages) | set(extra_packages)
)
if include_apps:
metadata.injected_packages_with_apps = list(
sorted(set(metadata.injected_packages_with_apps) | set(extra_packages))
metadata.injected_packages_with_apps = sorted(
set(metadata.injected_packages_with_apps) | set(extra_packages)
)

typer.secho(
Expand Down

0 comments on commit 6e31d0b

Please sign in to comment.