-
Notifications
You must be signed in to change notification settings - Fork 6
[CDF-26465] Rebuild build #2273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ronpal
wants to merge
16
commits into
main
Choose a base branch
from
re-build
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
496ed2b
Doodling
ronpal 7221f8d
Doodling
ronpal 66b99b2
For discussion
ronpal c29b84f
Focus on parity
ronpal 68ec51b
Focus on parity
ronpal 133ae27
Merge branch 'main' of https://github.com/cognitedata/toolkit into re…
ronpal a515054
Focus on parity
ronpal 56bd9e2
Testing for warning parity
ronpal a12daec
new build behind v08 flag
ronpal 270a731
validate vs verify
ronpal 102a935
validate vs verify
ronpal 7d313d6
Renaming for readability
ronpal e2ffd53
Housekeeping
ronpal d84ea72
Fixing toml
ronpal c42ca3e
Update cognite_toolkit/_cdf_tk/commands/build_v2/build_cmd.py
ronpal ad34daa
Restoring from gemini blunder
ronpal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ migrate = true | |
| streams = true | ||
| create = true | ||
| extend-download = true | ||
| v08 = false | ||
|
|
||
| [plugins] | ||
| run = true | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good refactoring that can be moved out to a separate PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| from pathlib import Path | ||
| from typing import Any, Literal, TypedDict | ||
|
|
||
| from rich import print | ||
| from rich.panel import Panel | ||
|
|
||
| from cognite_toolkit._cdf_tk.client import ToolkitClient | ||
| from cognite_toolkit._cdf_tk.commands._base import ToolkitCommand | ||
| from cognite_toolkit._cdf_tk.commands.build_cmd import BuildCommand as OldBuildCommand | ||
| from cognite_toolkit._cdf_tk.commands.build_v2.build_input import BuildInput | ||
| from cognite_toolkit._cdf_tk.commands.build_v2.build_issues import BuildIssue, BuildIssueList | ||
| from cognite_toolkit._cdf_tk.data_classes import ( | ||
| BuildConfigYAML, | ||
| BuildVariables, | ||
| BuiltModuleList, | ||
| ModuleDirectories, | ||
| ) | ||
| from cognite_toolkit._cdf_tk.exceptions import ToolkitError | ||
| from cognite_toolkit._cdf_tk.hints import verify_module_directory | ||
| from cognite_toolkit._cdf_tk.tk_warnings import ToolkitWarning, WarningList | ||
| from cognite_toolkit._cdf_tk.utils.file import safe_rmtree | ||
| from cognite_toolkit._cdf_tk.validation import validate_module_selection, validate_modules_variables | ||
| from cognite_toolkit._version import __version__ | ||
|
|
||
|
|
||
| class BuildWarnings(TypedDict): | ||
| warning: ToolkitWarning | ||
| location: list[Path] | ||
|
|
||
|
|
||
| class BuildCommand(ToolkitCommand): | ||
| def __init__(self, print_warning: bool = True, skip_tracking: bool = False, silent: bool = False) -> None: | ||
| super().__init__(print_warning, skip_tracking, silent) | ||
| self.issues = BuildIssueList() | ||
|
|
||
| def execute( | ||
| self, | ||
| verbose: bool, | ||
| organization_dir: Path, | ||
| build_dir: Path, | ||
| selected: list[str | Path] | None, | ||
| build_env_name: str | None, | ||
| no_clean: bool, | ||
| client: ToolkitClient | None = None, | ||
| on_error: Literal["continue", "raise"] = "continue", | ||
| ) -> BuiltModuleList: | ||
| """ | ||
| Build the resources into deployable artifacts in the build directory. | ||
| """ | ||
|
|
||
| self.verbose = verbose | ||
| self.on_error = on_error | ||
|
|
||
| # Tracking the project and cluster for the build. | ||
| if client: | ||
| self._additional_tracking_info.project = client.config.project | ||
| self._additional_tracking_info.cluster = client.config.cdf_cluster | ||
|
|
||
| # Setting the parameters for the build. | ||
| input = BuildInput.load(organization_dir, build_dir, build_env_name, client, selected) | ||
|
|
||
| # Print the build input. | ||
| if self.verbose: | ||
| self._print_build_input(input) | ||
|
|
||
| # Capture warnings from module structure integrity | ||
| if module_selection_issues := self._verify_module_selection(input): | ||
| self.issues.extend(module_selection_issues) | ||
|
|
||
| # Logistics: clean and create build directory | ||
| if prepare_issues := self._prepare_target_directory(input, not no_clean): | ||
| self.issues.extend(prepare_issues) | ||
|
|
||
| # Compile the configuration and variables, | ||
| # check syntax on module and resource level | ||
| # for any "compilation errors and warnings" | ||
| built_modules, build_issues = self._build_configuration(input) | ||
| if build_issues: | ||
| self.issues.extend(build_issues) | ||
|
|
||
| # This is where we would add any recommendations for the user to improve the build. | ||
| if build_quality_issues := self._verify_build_quality(built_modules): | ||
| self.issues.extend(build_quality_issues) | ||
|
|
||
| # Finally, print warnings grouped by category/code and location. | ||
| self._print_or_log_warnings_by_category(self.issues) | ||
|
|
||
| return built_modules | ||
|
|
||
| def _print_build_input(self, input: BuildInput) -> None: | ||
| print( | ||
| Panel( | ||
| f"Building {input.organization_dir!s}:\n - Toolkit Version '{__version__!s}'\n" | ||
| f" - Environment name {input.build_env_name!r}, validation-type {input.config.environment.validation_type!r}.\n" | ||
| f" - Config '{input.config.filepath!s}'", | ||
| expand=False, | ||
| ) | ||
| ) | ||
|
|
||
| def _prepare_target_directory(self, input: BuildInput, clean: bool = False) -> BuildIssueList: | ||
| """ | ||
| Directory logistics | ||
| """ | ||
| issues = BuildIssueList() | ||
| if input.build_dir.exists() and any(input.build_dir.iterdir()): | ||
| if not clean: | ||
| raise ToolkitError("Build directory is not empty. Run without --no-clean to remove existing files.") | ||
|
|
||
| if self.verbose: | ||
| issues.append(BuildIssue(description=f"Build directory {input.build_dir!s} is not empty. Clearing.")) | ||
| safe_rmtree(input.build_dir) | ||
| input.build_dir.mkdir(parents=True, exist_ok=True) | ||
| return issues | ||
|
|
||
| def _verify_module_selection(self, input: BuildInput) -> BuildIssueList: | ||
| issues = BuildIssueList() | ||
| # Verify that the modules exists, are not duplicates, | ||
| # and at least one is selected | ||
| verify_module_directory(input.organization_dir, input.build_env_name) | ||
|
|
||
| # Validate module selection | ||
| user_selected_modules = input.config.environment.get_selected_modules({}) | ||
| module_warnings = validate_module_selection( | ||
| input.modules, | ||
| input.config, | ||
| {}, | ||
| user_selected_modules, | ||
| input.organization_dir, | ||
| ) | ||
| if module_warnings: | ||
| issues.extend(BuildIssueList.from_warning_list(module_warnings)) | ||
|
|
||
| # Validate variables. Note: this looks for non-replaced template | ||
| # variables <.*?> and can be improved in the future. | ||
| # Keeping for reference. | ||
| variables_warnings = validate_modules_variables(input.variables, input.config.filepath) | ||
| if variables_warnings: | ||
| issues.extend(BuildIssueList.from_warning_list(variables_warnings)) | ||
|
|
||
| # Track LOC of managed configuration | ||
| # Note: _track is not implemented yet, so we skip it for now | ||
| # self._track(input) | ||
|
|
||
| return issues | ||
|
|
||
| def _build_configuration(self, input: BuildInput) -> tuple[BuiltModuleList, BuildIssueList]: | ||
| issues = BuildIssueList() | ||
| # Use input.modules.selected directly (it's already a ModuleDirectories) | ||
| if not input.modules.selected: | ||
| return BuiltModuleList(), issues | ||
|
|
||
| # first collect variables into practical lookup | ||
| # TODO: parallelism is not implemented yet. I'm sure there are optimizations to be had here, but we'll focus on process parallelism since we believe loading yaml and file i/O are the biggest bottlenecks. | ||
|
|
||
| old_build_command = OldBuildCommand(print_warning=False, skip_tracking=False) | ||
| built_modules = old_build_command.build_config( | ||
| build_dir=input.build_dir, | ||
| organization_dir=input.organization_dir, | ||
| config=input.config, | ||
| packages={}, | ||
| clean=False, | ||
| verbose=self.verbose, | ||
| client=input.client, | ||
| progress_bar=False, | ||
| on_error=self.on_error, | ||
| ) | ||
| # Copy tracking info from old command to self | ||
| self._additional_tracking_info.package_ids.update(old_build_command._additional_tracking_info.package_ids) | ||
| self._additional_tracking_info.module_ids.update(old_build_command._additional_tracking_info.module_ids) | ||
|
|
||
| # Collect warnings from the old build command and convert to issues | ||
| # Always convert warnings to issues, even if the list appears empty | ||
| # (WarningList might have custom __bool__ behavior) | ||
| if old_build_command.warning_list: | ||
| converted_issues = BuildIssueList.from_warning_list(old_build_command.warning_list) | ||
| issues.extend(converted_issues) | ||
| return built_modules, issues | ||
|
|
||
| def _verify_build_quality(self, built_modules: BuiltModuleList) -> BuildIssueList: | ||
| issues = BuildIssueList() | ||
| return issues | ||
|
|
||
| def _write(self, input: BuildInput) -> None: | ||
| # Write the build to the build directory. | ||
| # Track lines of code built. | ||
| raise NotImplementedError() | ||
|
|
||
| def _track(self, input: BuildInput) -> None: | ||
| raise NotImplementedError() | ||
|
|
||
| def _print_or_log_warnings_by_category(self, issues: BuildIssueList) -> None: | ||
| pass | ||
|
|
||
| # Delegate to old BuildCommand for backward compatibility with tests | ||
| def build_modules( | ||
| self, | ||
| modules: ModuleDirectories, | ||
| build_dir: Path, | ||
| variables: BuildVariables, | ||
| verbose: bool = False, | ||
| progress_bar: bool = False, | ||
| on_error: Literal["continue", "raise"] = "continue", | ||
| ) -> BuiltModuleList: | ||
| """Delegate to old BuildCommand for backward compatibility.""" | ||
| old_cmd = OldBuildCommand() | ||
|
|
||
| built_modules = old_cmd.build_modules(modules, build_dir, variables, verbose, progress_bar, on_error) | ||
| self._additional_tracking_info.package_ids.update(old_cmd._additional_tracking_info.package_ids) | ||
| self._additional_tracking_info.module_ids.update(old_cmd._additional_tracking_info.module_ids) | ||
| self.issues.extend(BuildIssueList.from_warning_list(old_cmd.warning_list or WarningList[ToolkitWarning]())) | ||
| return built_modules | ||
|
|
||
| def build_config( | ||
| self, | ||
| build_dir: Path, | ||
| organization_dir: Path, | ||
| config: BuildConfigYAML, | ||
| packages: dict[str, list[str]], | ||
| clean: bool = False, | ||
| verbose: bool = False, | ||
| client: ToolkitClient | None = None, | ||
| progress_bar: bool = False, | ||
| on_error: Literal["continue", "raise"] = "continue", | ||
| ) -> BuiltModuleList: | ||
| """Delegate to old BuildCommand for backward compatibility.""" | ||
| old_cmd = OldBuildCommand() | ||
| return old_cmd.build_config( | ||
| build_dir, organization_dir, config, packages, clean, verbose, client, progress_bar, on_error | ||
| ) | ||
|
|
||
| def _replace_variables( | ||
| self, | ||
| resource_files: list[Path], | ||
| variables: BuildVariables, | ||
| resource_name: str, | ||
| module_dir: Path, | ||
| verbose: bool = False, | ||
| ) -> list[Any]: | ||
| """Delegate to old BuildCommand for backward compatibility.""" | ||
| old_cmd = OldBuildCommand() | ||
| return old_cmd._replace_variables(resource_files, variables, resource_name, module_dir, verbose) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will you be able to reuse the old CLI interface? I would expect that to change, but I might be mistaken.