|
| 1 | +import os |
| 2 | +import pydantic |
| 3 | +import yaml |
| 4 | +from pathlib import Path |
| 5 | +from typing import Any |
| 6 | + |
| 7 | + |
| 8 | +def _parse_str_as_abs_path(value: str | Path, manifest_root: Path) -> Path: |
| 9 | + """Parse a string as a Path.""" |
| 10 | + # Ensure the debug directory is a Path object |
| 11 | + if isinstance(value, str): |
| 12 | + value = Path(value) |
| 13 | + # Ensure it's an absolute path |
| 14 | + if not value.is_absolute(): |
| 15 | + # Convert to absolute path relative to manifest root |
| 16 | + return (manifest_root / value).resolve() |
| 17 | + return value |
| 18 | + |
| 19 | + |
| 20 | +PackageMapEntry = dict[str, list[str] | dict[str, list[str]]] |
| 21 | + |
| 22 | + |
| 23 | +class PackageMappingSource: |
| 24 | + """Describes where additional package mapping data comes from.""" |
| 25 | + |
| 26 | + def __init__(self, mapping: dict[str, PackageMapEntry]): |
| 27 | + if mapping is None: |
| 28 | + raise ValueError("PackageMappingSource mapping cannot be null.") |
| 29 | + if not isinstance(mapping, dict): |
| 30 | + raise TypeError("PackageMappingSource mapping must be a dictionary.") |
| 31 | + # Copy to keep the source immutable for callers. |
| 32 | + self.mapping: dict[str, PackageMapEntry] = dict(mapping) |
| 33 | + |
| 34 | + @classmethod |
| 35 | + def from_mapping(cls, mapping: dict[str, PackageMapEntry]) -> "PackageMappingSource": |
| 36 | + """Create a source directly from a mapping dictionary.""" |
| 37 | + return cls(mapping) |
| 38 | + |
| 39 | + @classmethod |
| 40 | + def from_file(cls, file_path: str | Path) -> "PackageMappingSource": |
| 41 | + """Create a source from a mapping file.""" |
| 42 | + path = Path(file_path) |
| 43 | + if not path.exists(): |
| 44 | + raise ValueError(f"Additional package map file '{path}' not found.") |
| 45 | + with open(path) as f: |
| 46 | + data = yaml.safe_load(f) or {} |
| 47 | + if not isinstance(data, dict): |
| 48 | + raise TypeError("Expected package map file to contain a dictionary.") |
| 49 | + return cls(data) |
| 50 | + |
| 51 | + def get_package_mapping(self) -> dict[str, PackageMapEntry]: |
| 52 | + return dict(self.mapping) |
| 53 | + |
| 54 | + |
| 55 | +class ROSBackendConfig(pydantic.BaseModel, extra="forbid", arbitrary_types_allowed=True): |
| 56 | + """ROS backend configuration.""" |
| 57 | + |
| 58 | + # ROS distribution to use, e.g., "foxy", "galactic", "humble" |
| 59 | + # TODO: This should be figured out in some other way, not from the config. |
| 60 | + distro: str |
| 61 | + |
| 62 | + noarch: bool | None = None |
| 63 | + # Environment variables to set during the build |
| 64 | + env: dict[str, str] | None = None |
| 65 | + # Directory for debug files of this script |
| 66 | + debug_dir: Path | None = pydantic.Field(default=None, alias="debug-dir") |
| 67 | + # Extra input globs to include in the build hash |
| 68 | + extra_input_globs: list[str] | None = pydantic.Field(default=None, alias="extra-input-globs") |
| 69 | + |
| 70 | + # Extra package mappings to use in the build |
| 71 | + extra_package_mappings: list[PackageMappingSource] = pydantic.Field( |
| 72 | + default_factory=list, alias="extra-package-mappings" |
| 73 | + ) |
| 74 | + |
| 75 | + def is_noarch(self) -> bool: |
| 76 | + """Whether to build a noarch package or a platform-specific package.""" |
| 77 | + return self.noarch is None or self.noarch |
| 78 | + |
| 79 | + @pydantic.field_validator("debug_dir", mode="before") |
| 80 | + @classmethod |
| 81 | + def _parse_debug_dir(cls, value: Any, info: pydantic.ValidationInfo) -> Path | None: |
| 82 | + """Parse debug directory if set.""" |
| 83 | + if value is None: |
| 84 | + return None |
| 85 | + base_path = Path(os.getcwd()) |
| 86 | + if info.context and "manifest_root" in info.context: |
| 87 | + base_path = Path(info.context["manifest_root"]) |
| 88 | + return _parse_str_as_abs_path(value, base_path) |
| 89 | + |
| 90 | + @pydantic.field_validator("extra_package_mappings", mode="before") |
| 91 | + @classmethod |
| 92 | + def _parse_package_mappings( |
| 93 | + cls, input_value: Any, info: pydantic.ValidationInfo |
| 94 | + ) -> list[PackageMappingSource] | None: |
| 95 | + """Parse additional package mappings if set.""" |
| 96 | + if input_value is None: |
| 97 | + return [] |
| 98 | + |
| 99 | + base_path = Path(os.getcwd()) |
| 100 | + if info.context and "manifest_root" in info.context: |
| 101 | + base_path = Path(info.context["manifest_root"]) |
| 102 | + |
| 103 | + result: list[PackageMappingSource] = [] |
| 104 | + for raw_entry in input_value: |
| 105 | + # match for cases |
| 106 | + # it's already a package mapping source (usually for testing) |
| 107 | + if isinstance(raw_entry, PackageMappingSource): |
| 108 | + entry = raw_entry |
| 109 | + elif isinstance(raw_entry, dict): |
| 110 | + if "file" in raw_entry: |
| 111 | + file_value = raw_entry["file"] |
| 112 | + entry = PackageMappingSource.from_file(_parse_str_as_abs_path(file_value, base_path)) |
| 113 | + elif "mapping" in raw_entry: |
| 114 | + mapping_value = raw_entry["mapping"] |
| 115 | + entry = PackageMappingSource.from_mapping(mapping_value) |
| 116 | + else: |
| 117 | + entry = PackageMappingSource.from_mapping(raw_entry) |
| 118 | + elif isinstance(raw_entry, str | Path): |
| 119 | + entry = PackageMappingSource.from_file(_parse_str_as_abs_path(raw_entry, base_path)) |
| 120 | + else: |
| 121 | + raise ValueError( |
| 122 | + f"Unrecognized entry for extra-package-mappings: {raw_entry} of type {type(raw_entry)}." |
| 123 | + ) |
| 124 | + result.append(entry) |
| 125 | + return result |
0 commit comments