| |
| |
| """CLI implementation for `conda export`. |
| |
| Dumps specified environment package specifications to the screen. |
| """ |
|
|
| from argparse import ( |
| ArgumentParser, |
| Namespace, |
| _SubParsersAction, |
| ) |
|
|
| from conda.base.constants import KNOWN_SUBDIRS |
|
|
| from ..auxlib.ish import dals |
| from ..base.context import context |
| from ..common.constants import NULL |
| from ..models.environment import Environment |
| from ..plugins.environment_exporters.environment_yml import ( |
| ENVIRONMENT_JSON_FORMAT, |
| ENVIRONMENT_YAML_FORMAT, |
| ) |
|
|
|
|
| def configure_parser(sub_parsers: _SubParsersAction, **kwargs) -> ArgumentParser: |
| from .helpers import LazyChoicesAction, add_parser_json, add_parser_prefix |
|
|
| summary = "Export a given environment" |
| description = summary |
| epilog = dals( |
| """ |
| Examples:: |
| |
| conda export |
| conda export --file FILE_NAME |
| conda export --format yaml |
| conda export --file environment.yaml |
| """ |
| ) |
|
|
| p = sub_parsers.add_parser( |
| "export", |
| help=summary, |
| description=description, |
| epilog=epilog, |
| **kwargs, |
| ) |
|
|
| p.add_argument( |
| "-c", |
| "--channel", |
| action="append", |
| help="Additional channel to include in the export", |
| ) |
|
|
| p.add_argument( |
| "-O", |
| "--override-channels", |
| action="store_true", |
| help="Do not include .condarc channels", |
| ) |
| |
| |
| |
| |
| |
| |
| |
| p.add_argument( |
| "--platform", |
| "--subdir", |
| action="append", |
| dest="export_platforms", |
| help="Target platform(s)/subdir(s) for export (e.g., linux-64, osx-64, win-64)", |
| ) |
| p.add_argument( |
| "--override-platforms", |
| action="store_true", |
| help="Override the platforms specified in the condarc", |
| ) |
| add_parser_prefix(p) |
|
|
| p.add_argument( |
| "-f", |
| "--file", |
| default=None, |
| required=False, |
| help=( |
| "File name or path for the exported environment. " |
| "Note: This will silently overwrite any existing file " |
| "of the same name in the current directory." |
| ), |
| ) |
|
|
| p.add_argument( |
| "--format", |
| default=NULL, |
| required=False, |
| action=LazyChoicesAction, |
| choices_func=lambda: sorted( |
| context.plugin_manager.get_exporter_format_mapping() |
| ), |
| help=( |
| "Format for the exported environment. " |
| "If not specified, format will be determined by file extension or default to YAML." |
| ), |
| ) |
|
|
| p.add_argument( |
| "--no-builds", |
| default=False, |
| action="store_true", |
| required=False, |
| help="Remove build specification from dependencies", |
| ) |
|
|
| p.add_argument( |
| "--ignore-channels", |
| default=False, |
| action="store_true", |
| required=False, |
| help="Do not include channel names with package names.", |
| ) |
| add_parser_json(p) |
|
|
| p.add_argument( |
| "--from-history", |
| default=False, |
| action="store_true", |
| required=False, |
| help="Build environment spec from explicit specs in history", |
| ) |
| p.set_defaults(func="conda.cli.main_export.execute") |
|
|
| return p |
|
|
|
|
| |
| def execute(args: Namespace, parser: ArgumentParser) -> int: |
| from ..base.context import env_name |
| from ..exceptions import CondaValueError |
| from .common import stdout_json |
|
|
| unknown = set(context.export_platforms) - set(KNOWN_SUBDIRS) |
| if unknown: |
| raise CondaValueError( |
| f"Could not find platform(s): {', '.join(sorted(unknown))}. " |
| f"Valid platforms include: {', '.join(sorted(KNOWN_SUBDIRS))}" |
| ) |
|
|
| |
| target_format = args.format |
| environment_exporter = None |
|
|
| |
| |
| |
| |
| if target_format is not NULL: |
| |
| pass |
| elif args.file: |
| |
| environment_exporter = context.plugin_manager.detect_environment_exporter( |
| args.file |
| ) |
| target_format = environment_exporter.name |
| elif args.json: |
| |
| target_format = ENVIRONMENT_JSON_FORMAT |
| else: |
| |
| target_format = ENVIRONMENT_YAML_FORMAT |
|
|
| |
| if not environment_exporter: |
| environment_exporter = ( |
| context.plugin_manager.get_environment_exporter_by_format(target_format) |
| ) |
|
|
| |
| if ( |
| len(context.export_platforms) > 1 |
| and not environment_exporter.multiplatform_export |
| ): |
| raise CondaValueError( |
| f"Multiple platforms are not supported for the `{environment_exporter.name}` exporter" |
| ) |
|
|
| prefix = context.target_prefix |
|
|
| |
| env = Environment.from_prefix( |
| prefix=prefix, |
| name=env_name(prefix), |
| platform=context.subdir, |
| from_history=args.from_history, |
| no_builds=args.no_builds, |
| ignore_channels=args.ignore_channels, |
| channels=context.channels, |
| ) |
|
|
| |
| envs = [env.extrapolate(platform) for platform in context.export_platforms] |
| if environment_exporter.multiplatform_export: |
| exported_content = environment_exporter.multiplatform_export(envs) |
| elif environment_exporter.export: |
| exported_content = environment_exporter.export(envs[0]) |
| else: |
| raise CondaValueError( |
| f"No export method found for {environment_exporter.name} exporter" |
| ) |
|
|
| |
| exported_content = exported_content.rstrip() + "\n" |
|
|
| |
| if args.file: |
| with open(args.file, "w") as fp: |
| fp.write(exported_content) |
| if args.json: |
| stdout_json({"success": True, "file": args.file, "format": target_format}) |
| else: |
| print(exported_content, end="") |
|
|
| return 0 |
|
|