From 308183bae7ee0e6001fa25f2457e3361968e3880 Mon Sep 17 00:00:00 2001 From: Marius van Niekerk Date: Sat, 20 Jul 2024 12:28:20 -0400 Subject: [PATCH 1/2] Make condax install idempotent --- .pre-commit-config.yaml | 4 ++-- condax/conda.py | 17 ++++++++++++++++- condax/core.py | 17 ++++++++++++----- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index df2c368..ca17ee6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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] \ No newline at end of file diff --git a/condax/conda.py b/condax/conda.py index 7ac0144..46c48a3 100644 --- a/condax/conda.py +++ b/condax/conda.py @@ -1,3 +1,4 @@ +import enum import json import logging import os @@ -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) @@ -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( diff --git a/condax/core.py b/condax/core.py index aeba321..b741afe 100644 --- a/condax/core.py +++ b/condax/core.py @@ -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( @@ -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( From 868dd352a4b54e38e62c63efde732ced7b86dac0 Mon Sep 17 00:00:00 2001 From: Marius van Niekerk Date: Sat, 20 Jul 2024 12:30:05 -0400 Subject: [PATCH 2/2] fmt --- condax/activation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/condax/activation.py b/condax/activation.py index da8b7df..c30ec6a 100644 --- a/condax/activation.py +++ b/condax/activation.py @@ -2,7 +2,7 @@ def write_activating_entrypoint_unix(executable, prefix): - template = dedent( + dedent( f""" #!/bin/sh export PATH="$PATH:{prefix}/bin" @@ -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"