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

Handle relative paths and writing nonexistent directories for CONDA_PROJECT_ENVS_PATH #161

Merged
merged 13 commits into from
Apr 22, 2024
2 changes: 1 addition & 1 deletion .github/disclaimer.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Copyright (C) 2022 Anaconda, Inc
Copyright (C) 2022-2024 Anaconda, Inc
SPDX-License-Identifier: BSD-3-Clause
3 changes: 3 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

Expand Down
3 changes: 3 additions & 0 deletions examples/multi-env-files/print_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

Expand Down
3 changes: 3 additions & 0 deletions examples/multi-env-files/test_get_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

Expand Down
3 changes: 3 additions & 0 deletions scripts/ap-to-cp.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

Expand Down
3 changes: 3 additions & 0 deletions src/conda_project/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

Expand Down
3 changes: 3 additions & 0 deletions src/conda_project/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
from .cli.main import main
Expand Down
3 changes: 3 additions & 0 deletions src/conda_project/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
3 changes: 3 additions & 0 deletions src/conda_project/cli/commands.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
import logging
Expand Down
3 changes: 3 additions & 0 deletions src/conda_project/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
from __future__ import annotations
Expand Down
3 changes: 3 additions & 0 deletions src/conda_project/conda.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
from __future__ import annotations
Expand Down
3 changes: 3 additions & 0 deletions src/conda_project/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

Expand Down
31 changes: 19 additions & 12 deletions src/conda_project/project.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2022 Anaconda, Inc
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
from __future__ import annotations

Expand Down Expand Up @@ -31,12 +31,12 @@
)
from fsspec.core import split_protocol

try:
try: # pragma: no-cover
# Version 2 provides a v1 API
from pydantic.v1 import BaseModel, create_model
except ImportError:
from pydantic import BaseModel # type: ignore
from pydantic import create_model # type: ignore
from pydantic.v1 import BaseModel, create_model # pragma: no cover
except ImportError: # pragma: no cover
from pydantic import BaseModel # type: ignore; #pragma: no cover
from pydantic import create_model # type: ignore; #pragma no cover

from .conda import (
CONDA_EXE,
Expand Down Expand Up @@ -308,11 +308,19 @@ def init(
@property
def environments(self) -> BaseEnvironments:
env_path = self.directory / "envs"
env_paths = os.environ.get("CONDA_PROJECT_ENVS_PATH", "").split(os.pathsep)
specified_path = os.environ.get("CONDA_PROJECT_ENVS_PATH", "")
env_paths = specified_path.split(os.pathsep) if specified_path else []

for path in env_paths:
if os.access(path, os.W_OK):
env_path = Path(path)
for _path in env_paths:
path = Path(_path)

if not path.is_absolute():
path = self.directory / path

path_writable = path.exists() and os.access(path, os.W_OK)
parent_writable = (not path.exists()) and os.access(path.parent, os.W_OK)
if path_writable or parent_writable:
env_path = path
break

envs = OrderedDict()
Expand Down Expand Up @@ -804,9 +812,8 @@ def clean(
verbose: bool = False,
) -> None:
"""Remove the conda environment."""

_ = call_conda(
["env", "remove", "-p", str(self.prefix)],
["env", "remove", "-y", "-p", str(self.prefix)],
jkong-anaconda marked this conversation as resolved.
Show resolved Hide resolved
condarc_path=self.project.condarc,
verbose=verbose,
logger=logger,
Expand Down
17 changes: 10 additions & 7 deletions src/conda_project/project_file.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Copyright (C) 2022 Anaconda, Inc
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
from __future__ import annotations

Expand All @@ -11,13 +14,13 @@
from pkg_resources import Requirement
from ruamel.yaml import YAML

try:
try: # pragma: no cover
# Version 2 provides a v1 API
from pydantic.v1 import BaseModel, ValidationError, validator
except ImportError:
from pydantic import BaseModel # type: ignore
from pydantic import ValidationError # type: ignore
from pydantic import validator # type: ignore
from pydantic.v1 import BaseModel, ValidationError, validator # pragma: no cover
except ImportError: # pragma: no cover
from pydantic import BaseModel # type: ignore; #pragma: no cover
from pydantic import ValidationError # type: ignore; #pragma: no cover
from pydantic import validator # type: ignore; #pragma: no cover

from .exceptions import CondaProjectError

Expand Down
3 changes: 3 additions & 0 deletions src/conda_project/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

Expand Down
3 changes: 3 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
21 changes: 20 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

from __future__ import annotations

from pathlib import Path
from typing import Optional
from tempfile import TemporaryDirectory
from typing import Generator, Optional

import pytest

from conda_project.conda import call_conda
from conda_project.project import CondaProject, current_platform


@pytest.fixture
def tmp_dir() -> Generator[Path, None, None]:
"""create a temporary directory

This is useful if you want to two independent directories created for a test
since re-using `tmp_path` is not possible.
"""

with TemporaryDirectory() as d:
yield Path(d)


@pytest.fixture()
def project_directory_factory(tmp_path, request):
"""A fixture returning a factory function used to create a temporary project directory.
Expand Down
3 changes: 3 additions & 0 deletions tests/test_archive.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

Expand Down
3 changes: 3 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
from argparse import Namespace
Expand Down
3 changes: 3 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
import os
Expand Down
3 changes: 3 additions & 0 deletions tests/test_conda.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

Expand Down
3 changes: 3 additions & 0 deletions tests/test_dependencies.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

Expand Down
3 changes: 3 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
import logging
Expand Down
3 changes: 3 additions & 0 deletions tests/test_install.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (C) 2022-2024 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

# Copyright (C) 2022 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

Expand Down
Loading
Loading