File size: 2,263 Bytes
71687cf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | # Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
"""Define explicit spec."""
from __future__ import annotations
from ...base.constants import EXPLICIT_MARKER
from ...base.context import context
from ...exceptions import CondaValueError
from ...gateways.disk.read import yield_lines
from ...misc import get_package_records_from_explicit
from ...models.environment import Environment
from ...plugins.types import EnvironmentSpecBase
class ExplicitSpec(EnvironmentSpecBase):
"""
The ExplicitSpec class handles explicit environment files. These are ones
which are marked with the @EXPLICIT marker.
"""
def __init__(self, filename: str | None = None, **kwargs) -> None:
"""Initialize the explicit specification.
:param filename: Path to the requirements file
:param kwargs: Additional arguments
"""
self.filename = filename
def can_handle(self) -> bool:
"""
Validates that this spec can process the environment definition.
This checks if:
* a filename was provided
* the file has the "@EXPLICIT" marker
:return: True if the file can be handled, False otherwise
"""
# Return early if no filename was provided
if self.filename is None:
return False
# Ensure the file has the "@EXPLICIT" marker
dependencies_list = list(yield_lines(self.filename))
if EXPLICIT_MARKER in dependencies_list:
return True
else:
return False
@property
def env(self) -> Environment:
"""
Build an environment from the explicit file.
:return: An Environment object containing the package specifications
:raises ValueError: If the file cannot be read
"""
if not self.filename:
raise CondaValueError("No filename provided")
# Convert generator to list since Dependencies needs to access it multiple times
dependencies_list = list(yield_lines(self.filename))
explicit_packages = get_package_records_from_explicit(dependencies_list)
return Environment(
platform=context.subdir,
explicit_packages=explicit_packages,
)
|