From a72a685c8419f7558bd52e1424696852ac67b9b8 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Wed, 5 Aug 2026 14:43:23 +0800 Subject: [PATCH] refactor: update packer artifact functions return type --- packer/src/sarasacw_omrf_packer/artifact.py | 78 +++++++++------------ 1 file changed, 33 insertions(+), 45 deletions(-) diff --git a/packer/src/sarasacw_omrf_packer/artifact.py b/packer/src/sarasacw_omrf_packer/artifact.py index 7eb38b9..15d9a47 100644 --- a/packer/src/sarasacw_omrf_packer/artifact.py +++ b/packer/src/sarasacw_omrf_packer/artifact.py @@ -1,5 +1,6 @@ import sys import logging +from typing import Iterator from pathlib import Path from dataclasses import dataclass from re import Pattern, compile @@ -66,13 +67,15 @@ class FileCopyInfo: """The path of destination file relative to the install directory""" -def resolve_include_copy(ctx: ArtifactContext) -> tuple[FileCopyInfo, ...]: +def resolve_include_copy(ctx: ArtifactContext) -> Iterator[FileCopyInfo]: extractor = ctx.extractor metadata = ctx.metadata project_root = Path(extractor.get_project_dir()) - rv: list[FileCopyInfo] = [] for header in metadata.headers: + logging.debug( + "Resolving header copy rule: %s -> %s", header.from_path, header.to_path + ) to_path = Path("include") / Path(header.to_path) if _is_glob_pattern(header.from_path): (common_prefix, glob_residue) = _extract_common_prefix(header.from_path) @@ -82,16 +85,12 @@ def resolve_include_copy(ctx: ArtifactContext) -> tuple[FileCopyInfo, ...]: # YYC MARK: # Built ``subpath`` is prefix with ``new_project_root`` relative_subpath = subpath.relative_to(new_project_root) - rv.append(FileCopyInfo(subpath, to_path / relative_subpath)) + yield FileCopyInfo(subpath, to_path / relative_subpath) else: - rv.append( - FileCopyInfo( - project_root / header.from_path, to_path / header.from_path - ) + yield FileCopyInfo( + project_root / header.from_path, to_path / header.from_path ) - return tuple(rv) - def _generate_dll_artifact_name(name: str) -> str: match sys.platform: @@ -110,32 +109,25 @@ def _generate_lib_artifact_name(name: str) -> str: return f"{name}.dll.lib" -def resolve_lib_copy(ctx: ArtifactContext) -> tuple[FileCopyInfo, ...]: +def resolve_lib_copy(ctx: ArtifactContext) -> Iterator[FileCopyInfo]: extractor = ctx.extractor target_directory = extractor.get_target_directory() / "release" target_name = extractor.get_target_name() - rv: list[FileCopyInfo] = [] # copy artifact for redist dll_artifact_filename = _generate_dll_artifact_name(target_name) - rv.append( - FileCopyInfo( - target_directory / dll_artifact_filename, - Path("bin" if sys.platform == "win32" else "lib") / dll_artifact_filename, - ) + yield FileCopyInfo( + target_directory / dll_artifact_filename, + Path("bin" if sys.platform == "win32" else "lib") / dll_artifact_filename, ) # copy artifact for linking only on windows if sys.platform == "win32": lib_artifact_filename = _generate_lib_artifact_name(target_name) - rv.append( - FileCopyInfo( - target_directory / lib_artifact_filename, - Path("lib") / lib_artifact_filename, - ) + yield FileCopyInfo( + target_directory / lib_artifact_filename, + Path("lib") / lib_artifact_filename, ) - return tuple(rv) - @dataclass(frozen=True) class TextCopyInfo: @@ -156,7 +148,7 @@ def _sanitize_name(name: str) -> str: return new_name -def resolve_cmake_copy(ctx: ArtifactContext) -> tuple[TextCopyInfo, ...]: +def resolve_cmake_copy(ctx: ArtifactContext) -> Iterator[TextCopyInfo]: extractor = ctx.extractor metadata = ctx.metadata target_name = extractor.get_target_name() @@ -182,29 +174,27 @@ def resolve_cmake_copy(ctx: ArtifactContext) -> tuple[TextCopyInfo, ...]: ) render = CMakeRender(properties) # return infos - return ( - TextCopyInfo( - render.render_config(), - Path( - "lib", - "cmake", - cmake_target_name, - f"{cmake_target_name}Config.cmake", - ), + yield TextCopyInfo( + render.render_config(), + Path( + "lib", + "cmake", + cmake_target_name, + f"{cmake_target_name}Config.cmake", ), - TextCopyInfo( - render.render_config_version(), - Path( - "lib", - "cmake", - cmake_target_name, - f"{cmake_target_name}ConfigVersion.cmake", - ), + ) + yield TextCopyInfo( + render.render_config_version(), + Path( + "lib", + "cmake", + cmake_target_name, + f"{cmake_target_name}ConfigVersion.cmake", ), ) -def resolve_pkgconfig_copy(ctx: ArtifactContext) -> tuple[TextCopyInfo, ...]: +def resolve_pkgconfig_copy(ctx: ArtifactContext) -> Iterator[TextCopyInfo]: extractor = ctx.extractor metadata = ctx.metadata target_name = extractor.get_target_name() @@ -227,6 +217,4 @@ def resolve_pkgconfig_copy(ctx: ArtifactContext) -> tuple[TextCopyInfo, ...]: pkgconfig_name, pkgconfig_description, target_name, extractor.get_version() ) render = PkgConfigRender(properties) - return ( - TextCopyInfo(render.render(), Path("lib", "pkgconfig", f"{pkgconfig_id}.pc")), - ) + yield TextCopyInfo(render.render(), Path("lib", "pkgconfig", f"{pkgconfig_id}.pc"))