File size: 4,119 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | # Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
"""CLI implementation for `conda compare`.
Compare the packages in an environment with the packages listed in an environment file.
"""
from __future__ import annotations
import logging
import os
from os.path import abspath, expanduser, expandvars
from typing import TYPE_CHECKING
from ..deprecations import deprecated
if TYPE_CHECKING:
from argparse import ArgumentParser, Namespace, _SubParsersAction
log = logging.getLogger(__name__)
def configure_parser(sub_parsers: _SubParsersAction, **kwargs) -> ArgumentParser:
from ..auxlib.ish import dals
from .helpers import add_parser_json, add_parser_prefix
summary = "Compare packages between conda environments."
description = summary
epilog = dals(
"""
Examples:
Compare packages in the current environment with respect
to 'environment.yml' located in the current working directory::
conda compare environment.yml
Compare packages installed into the environment 'myenv' with respect
to 'environment.yml' in a different directory::
conda compare -n myenv path/to/file/environment.yml
"""
)
p = sub_parsers.add_parser(
"compare",
help=summary,
description=description,
epilog=epilog,
**kwargs,
)
add_parser_json(p)
add_parser_prefix(p)
p.add_argument(
"file",
action="store",
help="Path to the environment file that is to be compared against.",
)
p.set_defaults(func="conda.cli.main_compare.execute")
return p
@deprecated(
"26.3",
"26.9",
addendum="Use `conda.core.prefix_data.PrefixData.map_records` instead.",
)
def get_packages(prefix):
from ..core.prefix_data import PrefixData
from ..exceptions import EnvironmentLocationNotFound
if not os.path.isdir(prefix):
raise EnvironmentLocationNotFound(prefix)
return sorted(
PrefixData(prefix, interoperability=True).iter_records(),
key=lambda x: x.name,
)
def compare_packages(active_pkgs, specification_pkgs) -> tuple[int, list[str]]:
from ..models.match_spec import MatchSpec
errors = []
for pkg in specification_pkgs:
pkg_spec = MatchSpec(pkg)
if (name := pkg_spec.name) in active_pkgs:
if not pkg_spec.match(active_pkg := active_pkgs[name]):
errors.append(
f"{name} found but mismatch. Specification pkg: {pkg}, "
f"Running pkg: {active_pkg.spec}"
)
else:
errors.append(f"{name} not found")
if not errors:
return 0, [
"Success. All the packages in the "
"specification file are present in the environment "
"with matching version and build string."
]
return 1, errors
def execute(args: Namespace, parser: ArgumentParser) -> int:
from ..base.context import context
from ..core.prefix_data import PrefixData
from ..gateways.connection.session import CONDA_SESSION_SCHEMES
from .common import stdout_json
prefix_data = PrefixData.from_context(interoperability=True)
prefix_data.assert_environment()
url_scheme = args.file.split("://", 1)[0]
if url_scheme in CONDA_SESSION_SCHEMES:
filename = args.file
else:
filename = abspath(expanduser(expandvars(args.file)))
spec_hook = context.plugin_manager.get_environment_specifier(
source=filename,
name=context.environment_specifier,
)
spec = spec_hook.environment_spec(filename)
env = spec.env
active_pkgs = prefix_data.map_records()
specification_pkgs = (
*env.requested_packages,
*(
package
for packages in env.external_packages.values()
for package in packages
),
)
exitcode, output = compare_packages(active_pkgs, specification_pkgs)
if context.json:
stdout_json(output)
else:
print("\n".join(map(str, output)))
return exitcode
|