def _expand_environment(snapcraft_yaml: Dict[str, Any], *, parallel_build_count: int) -> None: """Expand global variables in the provided dictionary values. :param snapcraft_yaml: A dictionary containing the contents of the snapcraft.yaml project file. """ if utils.is_managed_mode(): work_dir = utils.get_managed_environment_home_path() else: work_dir = Path.cwd() project_vars = { "version": snapcraft_yaml.get("version", ""), "grade": snapcraft_yaml.get("grade", ""), } dirs = craft_parts.ProjectDirs(work_dir=work_dir) info = craft_parts.ProjectInfo( application_name="snapcraft", # not used in environment expansion cache_dir=Path(), # not used in environment expansion parallel_build_count=parallel_build_count, project_name=snapcraft_yaml.get("name", ""), project_dirs=dirs, project_vars=project_vars, ) _set_global_environment(info) craft_parts.expand_environment(snapcraft_yaml, info=info, skip=["name", "version"])
def get_dispatcher() -> craft_cli.Dispatcher: """Return an instance of Dispatcher. Run all the checks and setup required to ensure the Dispatcher can run. """ # Run the legacy implementation if inside a legacy managed environment. if os.getenv("SNAPCRAFT_BUILD_ENVIRONMENT") == "managed-host": snapcraft.BasePlugin = snapcraft_legacy.BasePlugin # type: ignore snapcraft.ProjectOptions = snapcraft_legacy.ProjectOptions # type: ignore legacy.legacy_run() # set lib loggers to debug level so that all messages are sent to Emitter for lib_name in _LIB_NAMES: logger = logging.getLogger(lib_name) _ORIGINAL_LIB_NAME_LOG_LEVEL[lib_name] = logger.level logger.setLevel(logging.DEBUG) if utils.is_managed_mode(): log_filepath = utils.get_managed_environment_log_path() else: log_filepath = None emit.init( mode=EmitterMode.BRIEF, appname="snapcraft", greeting=f"Starting Snapcraft {__version__}", log_filepath=log_filepath, ) return craft_cli.Dispatcher( "snapcraft", COMMAND_GROUPS, summary="Package, distribute, and update snaps for Linux and IoT", extra_global_args=GLOBAL_ARGS, default_command=commands.PackCommand, )
def _run_command( command_name: str, *, project: Project, parse_info: Dict[str, List[str]], assets_dir: Path, start_time: datetime, parallel_build_count: int, parsed_args: "argparse.Namespace", ) -> None: managed_mode = utils.is_managed_mode() part_names = getattr(parsed_args, "parts", None) if not managed_mode: run_project_checks(project, assets_dir=assets_dir) if command_name == "snap": emit.progress( "The 'snap' command is deprecated, use 'pack' instead.", permanent=True, ) if parsed_args.use_lxd and providers.get_platform_default_provider( ) == "lxd": emit.progress("LXD is used by default on this platform.", permanent=True) if (not managed_mode and not parsed_args.destructive_mode and not os.getenv("SNAPCRAFT_BUILD_ENVIRONMENT") == "host"): if command_name == "clean" and not part_names: _clean_provider(project, parsed_args) else: _run_in_provider(project, command_name, parsed_args) return if managed_mode: work_dir = utils.get_managed_environment_home_path() project_dir = utils.get_managed_environment_project_path() else: work_dir = Path.cwd() project_dir = Path.cwd() step_name = "prime" if command_name in ("pack", "snap") else command_name lifecycle = PartsLifecycle( project.parts, work_dir=work_dir, assets_dir=assets_dir, base=project.get_effective_base(), package_repositories=project.package_repositories, parallel_build_count=parallel_build_count, part_names=part_names, adopt_info=project.adopt_info, project_name=project.name, parse_info=parse_info, project_vars={ "version": project.version or "", "grade": project.grade or "", }, extra_build_snaps=project.get_extra_build_snaps(), ) if command_name == "clean": lifecycle.clean(part_names=part_names) return lifecycle.run( step_name, debug=parsed_args.debug, shell=getattr(parsed_args, "shell", False), shell_after=getattr(parsed_args, "shell_after", False), ) # Extract metadata and generate snap.yaml project_vars = lifecycle.project_vars if step_name == "prime" and not part_names: emit.progress("Extracting and updating metadata...") metadata_list = lifecycle.extract_metadata() update_project_metadata( project, project_vars=project_vars, metadata_list=metadata_list, assets_dir=assets_dir, prime_dir=lifecycle.prime_dir, ) emit.progress("Copying snap assets...") setup_assets( project, assets_dir=assets_dir, project_dir=project_dir, prime_dir=lifecycle.prime_dir, ) emit.progress("Generating snap metadata...") snap_yaml.write( project, lifecycle.prime_dir, arch=lifecycle.target_arch, arch_triplet=lifecycle.target_arch_triplet, ) emit.progress("Generated snap metadata", permanent=True) if parsed_args.enable_manifest: _generate_manifest( project, lifecycle=lifecycle, start_time=start_time, parsed_args=parsed_args, ) if command_name in ("pack", "snap"): snap_filename = pack.pack_snap( lifecycle.prime_dir, output=parsed_args.output, compression=project.compression, name=project.name, version=process_version(project.version), target_arch=lifecycle.target_arch, ) emit.message(f"Created snap package {snap_filename}")
def _run_command( command_name: str, *, project: Project, parse_info: Dict[str, List[str]], assets_dir: Path, start_time: datetime, parallel_build_count: int, parsed_args: "argparse.Namespace", ) -> None: managed_mode = utils.is_managed_mode() part_names = getattr(parsed_args, "parts", None) if not managed_mode: run_project_checks(project, assets_dir=assets_dir) if command_name == "snap": emit.progress( "The 'snap' command is deprecated, use 'pack' instead.", permanent=True, ) if parsed_args.use_lxd and providers.get_platform_default_provider( ) == "lxd": emit.progress("LXD is used by default on this platform.", permanent=True) if (not managed_mode and not parsed_args.destructive_mode and not os.getenv("SNAPCRAFT_BUILD_ENVIRONMENT") == "host"): if command_name == "clean" and not part_names: _clean_provider(project, parsed_args) else: _run_in_provider(project, command_name, parsed_args) return if managed_mode: work_dir = utils.get_managed_environment_home_path() project_dir = utils.get_managed_environment_project_path() else: work_dir = project_dir = Path.cwd() step_name = "prime" if command_name in ("pack", "snap") else command_name lifecycle = PartsLifecycle( project.parts, work_dir=work_dir, assets_dir=assets_dir, base=project.get_effective_base(), package_repositories=project.package_repositories, parallel_build_count=parallel_build_count, part_names=part_names, adopt_info=project.adopt_info, project_name=project.name, parse_info=parse_info, project_vars={ "version": project.version or "", "grade": project.grade or "", }, extra_build_snaps=project.get_extra_build_snaps(), target_arch=project.get_build_for(), ) if command_name == "clean": lifecycle.clean(part_names=part_names) return lifecycle.run( step_name, debug=parsed_args.debug, shell=getattr(parsed_args, "shell", False), shell_after=getattr(parsed_args, "shell_after", False), ) # Extract metadata and generate snap.yaml if step_name == "prime" and not part_names: _generate_metadata( project=project, lifecycle=lifecycle, project_dir=project_dir, assets_dir=assets_dir, start_time=start_time, parsed_args=parsed_args, ) if command_name in ("pack", "snap"): issues = linters.run_linters(lifecycle.prime_dir, lint=project.lint) status = linters.report(issues, intermediate=True) # In case of linter errors, stop execution and return the error code. if status in (LinterStatus.ERRORS, LinterStatus.FATAL): raise errors.LinterError("Linter errors found", exit_code=status) snap_filename = pack.pack_snap( lifecycle.prime_dir, output=parsed_args.output, compression=project.compression, name=project.name, version=process_version(project.version), target_arch=project.get_build_for(), ) emit.message(f"Created snap package {snap_filename}")