|
| 1 | +import shutil |
| 2 | +from collections.abc import Callable, Iterable, Sequence |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from cognite_toolkit._cdf_tk.builders._base import Builder |
| 6 | +from cognite_toolkit._cdf_tk.cruds import AppCRUD |
| 7 | +from cognite_toolkit._cdf_tk.data_classes import ( |
| 8 | + BuildDestinationFile, |
| 9 | + BuildSourceFile, |
| 10 | + BuiltResourceList, |
| 11 | + ModuleLocation, |
| 12 | +) |
| 13 | +from cognite_toolkit._cdf_tk.exceptions import ToolkitFileExistsError, ToolkitNotADirectoryError, ToolkitValueError |
| 14 | +from cognite_toolkit._cdf_tk.tk_warnings import ( |
| 15 | + FileReadWarning, |
| 16 | + HighSeverityWarning, |
| 17 | + LowSeverityWarning, |
| 18 | + ToolkitWarning, |
| 19 | + WarningList, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +class AppBuilder(Builder): |
| 24 | + _resource_folder = AppCRUD.folder_name |
| 25 | + |
| 26 | + def __init__(self, build_dir: Path, warn: Callable[[ToolkitWarning], None]) -> None: |
| 27 | + super().__init__(build_dir, warn=warn) |
| 28 | + |
| 29 | + def build( |
| 30 | + self, |
| 31 | + source_files: list[BuildSourceFile], |
| 32 | + module: ModuleLocation, |
| 33 | + console: Callable[[str], None] | None = None, |
| 34 | + ) -> Iterable[BuildDestinationFile | Sequence[ToolkitWarning]]: |
| 35 | + for source_file in source_files: |
| 36 | + if source_file.loaded is None: |
| 37 | + continue |
| 38 | + if source_file.source.path.parent.parent != module.dir: |
| 39 | + continue |
| 40 | + |
| 41 | + loader, warning = self._get_loader(source_file.source.path) |
| 42 | + if loader is None: |
| 43 | + if warning is not None: |
| 44 | + yield [warning] |
| 45 | + continue |
| 46 | + |
| 47 | + warnings = WarningList[FileReadWarning]() |
| 48 | + if loader is AppCRUD: |
| 49 | + warnings = self.copy_app_directory_to_build(source_file) |
| 50 | + |
| 51 | + destination_path = self._create_destination_path(source_file.source.path, loader.kind) |
| 52 | + |
| 53 | + yield BuildDestinationFile( |
| 54 | + path=destination_path, |
| 55 | + loaded=source_file.loaded, |
| 56 | + loader=loader, |
| 57 | + source=source_file.source, |
| 58 | + extra_sources=None, |
| 59 | + warnings=warnings, |
| 60 | + ) |
| 61 | + |
| 62 | + def validate_directory( |
| 63 | + self, |
| 64 | + built_resources: BuiltResourceList, |
| 65 | + module: ModuleLocation, |
| 66 | + ) -> WarningList[ToolkitWarning]: |
| 67 | + warnings = WarningList[ToolkitWarning]() |
| 68 | + has_config_files = any(resource.kind == AppCRUD.kind for resource in built_resources) |
| 69 | + if has_config_files: |
| 70 | + return warnings |
| 71 | + config_files_misplaced = [ |
| 72 | + file |
| 73 | + for file in module.source_paths_by_resource_folder[AppCRUD.folder_name] |
| 74 | + if AppCRUD.is_supported_file(file) |
| 75 | + ] |
| 76 | + if config_files_misplaced: |
| 77 | + for yaml_source_path in config_files_misplaced: |
| 78 | + required_location = module.dir / AppCRUD.folder_name / yaml_source_path.name |
| 79 | + warning = LowSeverityWarning( |
| 80 | + f"The required App resource configuration file " |
| 81 | + f"was not found in {required_location.as_posix()!r}. " |
| 82 | + f"The file {yaml_source_path.as_posix()!r} is currently " |
| 83 | + f"considered part of the App's artifacts and " |
| 84 | + f"will not be processed by the Toolkit.", |
| 85 | + ) |
| 86 | + warnings.append(warning) |
| 87 | + return warnings |
| 88 | + |
| 89 | + def copy_app_directory_to_build(self, source_file: BuildSourceFile) -> WarningList[FileReadWarning]: |
| 90 | + raw_content = source_file.loaded |
| 91 | + if raw_content is None: |
| 92 | + raise ToolkitValueError("App source file should be a YAML file.") |
| 93 | + raw_apps = raw_content if isinstance(raw_content, list) else [raw_content] |
| 94 | + warnings = WarningList[FileReadWarning]() |
| 95 | + for raw_app in raw_apps: |
| 96 | + app_external_id = raw_app.get("appExternalId") |
| 97 | + if not app_external_id: |
| 98 | + warnings.append( |
| 99 | + HighSeverityWarning( |
| 100 | + f"App in {source_file.source.path.as_posix()!r} has no appExternalId defined. " |
| 101 | + f"This is used to match the app to the app directory.", |
| 102 | + ), |
| 103 | + ) |
| 104 | + continue |
| 105 | + if not raw_app.get("version"): |
| 106 | + warnings.append( |
| 107 | + HighSeverityWarning( |
| 108 | + f"App {app_external_id} in {source_file.source.path.as_posix()!r} has no version defined.", |
| 109 | + ), |
| 110 | + ) |
| 111 | + |
| 112 | + app_directory = source_file.source.path.with_name(app_external_id) |
| 113 | + |
| 114 | + if not app_directory.is_dir(): |
| 115 | + raise ToolkitNotADirectoryError( |
| 116 | + f"App directory not found for appExternalId {app_external_id} defined in {source_file.source.path.as_posix()!r}.", |
| 117 | + ) |
| 118 | + |
| 119 | + destination = self.build_dir / self.resource_folder / app_external_id |
| 120 | + if destination.exists(): |
| 121 | + raise ToolkitFileExistsError( |
| 122 | + f"App {app_external_id!r} is duplicated. If this is unexpected, ensure you have a clean build directory.", |
| 123 | + ) |
| 124 | + shutil.copytree(app_directory, destination, ignore=shutil.ignore_patterns("__pycache__")) |
| 125 | + |
| 126 | + return warnings |
0 commit comments