Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make condax install idempotent #77

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading