cognite-toolkit 0.7.47__py3-none-any.whl → 0.7.49__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cognite_toolkit/_cdf_tk/apps/_migrate_app.py +6 -6
- cognite_toolkit/_cdf_tk/client/_toolkit_client.py +6 -4
- cognite_toolkit/_cdf_tk/client/api/instances.py +139 -0
- cognite_toolkit/_cdf_tk/client/api/location_filters.py +177 -0
- cognite_toolkit/_cdf_tk/client/api/raw.py +2 -2
- cognite_toolkit/_cdf_tk/client/api/robotics.py +19 -0
- cognite_toolkit/_cdf_tk/client/api/robotics_capabilities.py +127 -0
- cognite_toolkit/_cdf_tk/client/api/robotics_data_postprocessing.py +138 -0
- cognite_toolkit/_cdf_tk/client/api/robotics_frames.py +122 -0
- cognite_toolkit/_cdf_tk/client/api/robotics_locations.py +127 -0
- cognite_toolkit/_cdf_tk/client/api/robotics_maps.py +122 -0
- cognite_toolkit/_cdf_tk/client/api/robotics_robots.py +122 -0
- cognite_toolkit/_cdf_tk/client/api/search_config.py +101 -0
- cognite_toolkit/_cdf_tk/client/api/streams.py +63 -55
- cognite_toolkit/_cdf_tk/client/api/three_d.py +293 -277
- cognite_toolkit/_cdf_tk/client/cdf_client/api.py +34 -5
- cognite_toolkit/_cdf_tk/client/http_client/_client.py +5 -2
- cognite_toolkit/_cdf_tk/client/http_client/_data_classes2.py +4 -3
- cognite_toolkit/_cdf_tk/client/request_classes/filters.py +45 -1
- cognite_toolkit/_cdf_tk/client/resource_classes/apm_config.py +128 -0
- cognite_toolkit/_cdf_tk/client/resource_classes/cognite_file.py +53 -0
- cognite_toolkit/_cdf_tk/client/resource_classes/data_modeling/__init__.py +4 -0
- cognite_toolkit/_cdf_tk/client/resource_classes/data_modeling/_instance.py +22 -11
- cognite_toolkit/_cdf_tk/client/resource_classes/identifiers.py +7 -0
- cognite_toolkit/_cdf_tk/client/resource_classes/location_filter.py +9 -2
- cognite_toolkit/_cdf_tk/client/resource_classes/resource_view_mapping.py +38 -0
- cognite_toolkit/_cdf_tk/client/resource_classes/robotics/_map.py +6 -1
- cognite_toolkit/_cdf_tk/client/resource_classes/robotics/_robot.py +10 -5
- cognite_toolkit/_cdf_tk/client/resource_classes/streams.py +1 -20
- cognite_toolkit/_cdf_tk/client/resource_classes/three_d.py +30 -9
- cognite_toolkit/_cdf_tk/client/testing.py +2 -2
- cognite_toolkit/_cdf_tk/commands/_migrate/command.py +103 -108
- cognite_toolkit/_cdf_tk/commands/_migrate/conversion.py +6 -1
- cognite_toolkit/_cdf_tk/commands/_migrate/data_mapper.py +119 -41
- cognite_toolkit/_cdf_tk/commands/_migrate/issues.py +21 -38
- cognite_toolkit/_cdf_tk/commands/_migrate/migration_io.py +14 -12
- cognite_toolkit/_cdf_tk/commands/build_v2/_module_parser.py +138 -0
- cognite_toolkit/_cdf_tk/commands/build_v2/_modules_parser.py +163 -0
- cognite_toolkit/_cdf_tk/commands/build_v2/build_cmd.py +83 -96
- cognite_toolkit/_cdf_tk/commands/build_v2/{build_input.py → build_parameters.py} +8 -22
- cognite_toolkit/_cdf_tk/commands/build_v2/data_classes/_modules.py +27 -0
- cognite_toolkit/_cdf_tk/commands/build_v2/data_classes/_resource.py +22 -0
- cognite_toolkit/_cdf_tk/cruds/__init__.py +11 -5
- cognite_toolkit/_cdf_tk/cruds/_resource_cruds/streams.py +14 -30
- cognite_toolkit/_cdf_tk/data_classes/__init__.py +3 -0
- cognite_toolkit/_cdf_tk/data_classes/_issues.py +36 -0
- cognite_toolkit/_cdf_tk/data_classes/_module_directories.py +2 -1
- cognite_toolkit/_cdf_tk/storageio/_base.py +2 -0
- cognite_toolkit/_cdf_tk/storageio/logger.py +162 -0
- cognite_toolkit/_cdf_tk/utils/__init__.py +8 -1
- cognite_toolkit/_cdf_tk/utils/interactive_select.py +3 -1
- cognite_toolkit/_cdf_tk/utils/modules.py +7 -0
- cognite_toolkit/_repo_files/GitHub/.github/workflows/deploy.yaml +1 -1
- cognite_toolkit/_repo_files/GitHub/.github/workflows/dry-run.yaml +1 -1
- cognite_toolkit/_resources/cdf.toml +1 -1
- cognite_toolkit/_version.py +1 -1
- {cognite_toolkit-0.7.47.dist-info → cognite_toolkit-0.7.49.dist-info}/METADATA +1 -1
- {cognite_toolkit-0.7.47.dist-info → cognite_toolkit-0.7.49.dist-info}/RECORD +61 -43
- cognite_toolkit/_cdf_tk/commands/build_v2/build_issues.py +0 -27
- /cognite_toolkit/_cdf_tk/client/resource_classes/{search_config_resource.py → search_config.py} +0 -0
- {cognite_toolkit-0.7.47.dist-info → cognite_toolkit-0.7.49.dist-info}/WHEEL +0 -0
- {cognite_toolkit-0.7.47.dist-info → cognite_toolkit-0.7.49.dist-info}/entry_points.txt +0 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from cognite_toolkit._cdf_tk.constants import EXCL_FILES, MODULES
|
|
4
|
+
from cognite_toolkit._cdf_tk.cruds import CRUDS_BY_FOLDER_NAME, CRUDS_BY_FOLDER_NAME_INCLUDE_ALPHA
|
|
5
|
+
from cognite_toolkit._cdf_tk.data_classes import IssueList
|
|
6
|
+
from cognite_toolkit._cdf_tk.data_classes._issues import ModuleLoadingIssue
|
|
7
|
+
from cognite_toolkit._cdf_tk.exceptions import ToolkitError
|
|
8
|
+
from cognite_toolkit._cdf_tk.utils import humanize_collection, module_path_display_name
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class ModulesParser:
|
|
12
|
+
def __init__(self, organization_dir: Path, selected: list[str | Path] | None = None):
|
|
13
|
+
self.organization_dir = organization_dir
|
|
14
|
+
self.selected = selected
|
|
15
|
+
|
|
16
|
+
def parse(self) -> tuple[list[Path], IssueList]:
|
|
17
|
+
modules_root = self.organization_dir / MODULES
|
|
18
|
+
if not modules_root.exists():
|
|
19
|
+
raise ToolkitError(f"Module root directory '{modules_root}' not found")
|
|
20
|
+
|
|
21
|
+
module_paths: list[Path] = []
|
|
22
|
+
excluded_module_paths: list[Path] = []
|
|
23
|
+
issues: IssueList = IssueList()
|
|
24
|
+
for resource_file in self.organization_dir.glob("**/*.y*ml"):
|
|
25
|
+
if resource_file.name in EXCL_FILES:
|
|
26
|
+
continue
|
|
27
|
+
|
|
28
|
+
# Get the module folder for the resource file.
|
|
29
|
+
module_path = self._get_module_path_from_resource_file_path(resource_file)
|
|
30
|
+
if not module_path:
|
|
31
|
+
continue
|
|
32
|
+
|
|
33
|
+
# If the module has already been processed, skip it.
|
|
34
|
+
if module_path in module_paths or module_path in excluded_module_paths:
|
|
35
|
+
continue
|
|
36
|
+
|
|
37
|
+
# Skip the modules that do not match the selection
|
|
38
|
+
if not self._matches_selection(module_path, modules_root, self.selected):
|
|
39
|
+
excluded_module_paths.append(module_path)
|
|
40
|
+
continue
|
|
41
|
+
|
|
42
|
+
module_paths.append(module_path)
|
|
43
|
+
|
|
44
|
+
deepest_module_paths = self._find_modules_with_submodules(module_paths)
|
|
45
|
+
parent_module_paths = set(module_paths) - set(deepest_module_paths)
|
|
46
|
+
if parent_module_paths:
|
|
47
|
+
module_paths = deepest_module_paths
|
|
48
|
+
issues.extend(
|
|
49
|
+
ModuleLoadingIssue(
|
|
50
|
+
message=f"Module {module_path_display_name(self.organization_dir, parent_module_path)!r} is skipped because it has submodules"
|
|
51
|
+
)
|
|
52
|
+
for parent_module_path in parent_module_paths
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
valid_module_paths: list[Path] = []
|
|
56
|
+
for module_path in module_paths:
|
|
57
|
+
valid_module_path, issue = self._check_resource_folder_content(module_path)
|
|
58
|
+
if issue:
|
|
59
|
+
issues.append(issue)
|
|
60
|
+
if valid_module_path:
|
|
61
|
+
valid_module_paths.append(valid_module_path)
|
|
62
|
+
|
|
63
|
+
return valid_module_paths, issues
|
|
64
|
+
|
|
65
|
+
def _get_module_path_from_resource_file_path(self, resource_file: Path) -> Path | None:
|
|
66
|
+
# recognize the module by containing a resource associated by a CRUD.
|
|
67
|
+
# Special case: if the resource folder is a subfolder of a CRUD, return the parent of the subfolder.
|
|
68
|
+
resource_folder = resource_file.parent
|
|
69
|
+
crud = next(iter(CRUDS_BY_FOLDER_NAME_INCLUDE_ALPHA.get(resource_folder.name, [])), None)
|
|
70
|
+
if crud:
|
|
71
|
+
# iterate over the parents of the resource folder until we find the module folder.
|
|
72
|
+
# This is to handle the special case of a subfolder of a CRUD, or yamls in for example function subfolders.
|
|
73
|
+
for p in resource_file.parents:
|
|
74
|
+
if p.name == crud.folder_name:
|
|
75
|
+
return p.parent
|
|
76
|
+
if p.name == MODULES:
|
|
77
|
+
return p
|
|
78
|
+
return None
|
|
79
|
+
|
|
80
|
+
def _check_resource_folder_content(self, module_path: Path) -> tuple[None | Path, ModuleLoadingIssue | None]:
|
|
81
|
+
resource_folder_names = {d.name for d in module_path.iterdir() if d.is_dir()}
|
|
82
|
+
unrecognized_resource_folder_names = resource_folder_names - CRUDS_BY_FOLDER_NAME.keys()
|
|
83
|
+
|
|
84
|
+
issue = (
|
|
85
|
+
ModuleLoadingIssue(
|
|
86
|
+
message=f"Module {module_path_display_name(self.organization_dir, module_path)!r} contains unrecognized resource folder(s): {humanize_collection(unrecognized_resource_folder_names)}"
|
|
87
|
+
)
|
|
88
|
+
if unrecognized_resource_folder_names
|
|
89
|
+
else None
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
has_valid_resource_folders = bool(resource_folder_names & CRUDS_BY_FOLDER_NAME.keys())
|
|
93
|
+
return (module_path if has_valid_resource_folders else None, issue)
|
|
94
|
+
|
|
95
|
+
def _matches_selection(self, module_path: Path, modules_root: Path, selected: list[str | Path] | None) -> bool:
|
|
96
|
+
if not selected:
|
|
97
|
+
return True
|
|
98
|
+
|
|
99
|
+
rel = module_path.relative_to(modules_root)
|
|
100
|
+
rel_parts = [p.lower() for p in rel.parts]
|
|
101
|
+
if not rel_parts:
|
|
102
|
+
# module_path is the modules_root itself
|
|
103
|
+
return False
|
|
104
|
+
name_lower = rel_parts[-1]
|
|
105
|
+
modules_lower = MODULES.lower()
|
|
106
|
+
|
|
107
|
+
for sel in selected:
|
|
108
|
+
sel_path = Path(sel) if isinstance(sel, str) else sel
|
|
109
|
+
|
|
110
|
+
sel_parts = [p.lower() for p in sel_path.parts]
|
|
111
|
+
if not sel_parts:
|
|
112
|
+
continue
|
|
113
|
+
|
|
114
|
+
if sel_parts[0] == modules_lower:
|
|
115
|
+
sel_parts = sel_parts[1:]
|
|
116
|
+
|
|
117
|
+
if not sel_parts:
|
|
118
|
+
return True
|
|
119
|
+
|
|
120
|
+
if len(sel_parts) == 1 and name_lower == sel_parts[0]:
|
|
121
|
+
return True
|
|
122
|
+
|
|
123
|
+
if rel_parts[: len(sel_parts)] == sel_parts:
|
|
124
|
+
return True
|
|
125
|
+
|
|
126
|
+
return False
|
|
127
|
+
|
|
128
|
+
def _find_modules_with_submodules(self, module_paths: list[Path]) -> list[Path]:
|
|
129
|
+
"""Remove parent modules when they have submodules. Keep only the deepest modules."""
|
|
130
|
+
return [
|
|
131
|
+
module_path
|
|
132
|
+
for module_path in module_paths
|
|
133
|
+
if not any(
|
|
134
|
+
module_path in other_module_path.parents
|
|
135
|
+
for other_module_path in module_paths
|
|
136
|
+
if other_module_path != module_path
|
|
137
|
+
)
|
|
138
|
+
]
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from cognite_toolkit._cdf_tk.constants import EXCL_FILES, MODULES
|
|
4
|
+
from cognite_toolkit._cdf_tk.cruds import CRUDS_BY_FOLDER_NAME, CRUDS_BY_FOLDER_NAME_INCLUDE_ALPHA
|
|
5
|
+
from cognite_toolkit._cdf_tk.data_classes import IssueList
|
|
6
|
+
from cognite_toolkit._cdf_tk.data_classes._issues import ModuleLoadingIssue
|
|
7
|
+
from cognite_toolkit._cdf_tk.exceptions import ToolkitError
|
|
8
|
+
from cognite_toolkit._cdf_tk.utils import module_path_display_name
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class ModulesParser:
|
|
12
|
+
def __init__(self, organization_dir: Path, selected: list[str | Path] | None = None):
|
|
13
|
+
self.organization_dir = organization_dir
|
|
14
|
+
self.selected = selected
|
|
15
|
+
self.issues = IssueList()
|
|
16
|
+
|
|
17
|
+
def parse(self) -> list[Path]:
|
|
18
|
+
modules_root = self.organization_dir / MODULES
|
|
19
|
+
if not modules_root.exists():
|
|
20
|
+
raise ToolkitError(f"Module root directory '{modules_root.as_posix()}' not found")
|
|
21
|
+
|
|
22
|
+
module_paths: list[Path] = []
|
|
23
|
+
for resource_file in self.organization_dir.glob("**/*.y*ml"):
|
|
24
|
+
if resource_file.name in EXCL_FILES:
|
|
25
|
+
continue
|
|
26
|
+
|
|
27
|
+
# Get the module folder for the resource file.
|
|
28
|
+
module_path = self._get_module_path_from_resource_file_path(resource_file)
|
|
29
|
+
if not module_path:
|
|
30
|
+
continue
|
|
31
|
+
|
|
32
|
+
# If the module has already been processed, skip it.
|
|
33
|
+
if module_path in module_paths:
|
|
34
|
+
continue
|
|
35
|
+
|
|
36
|
+
module_paths.append(module_path)
|
|
37
|
+
|
|
38
|
+
deepest_module_paths = self._find_modules_with_submodules(module_paths)
|
|
39
|
+
parent_module_paths = set(module_paths) - set(deepest_module_paths)
|
|
40
|
+
if parent_module_paths:
|
|
41
|
+
module_paths = deepest_module_paths
|
|
42
|
+
self.issues.extend(
|
|
43
|
+
ModuleLoadingIssue(
|
|
44
|
+
message=f"Module {module_path_display_name(self.organization_dir, parent_module_path)!r} is skipped because it has submodules",
|
|
45
|
+
)
|
|
46
|
+
for parent_module_path in parent_module_paths
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
all_module_paths = module_paths
|
|
50
|
+
|
|
51
|
+
if self.selected:
|
|
52
|
+
normalized_selected = self._normalize_selection(self.selected)
|
|
53
|
+
selected_module_paths = [
|
|
54
|
+
module_path
|
|
55
|
+
for module_path in all_module_paths
|
|
56
|
+
if self._matches_selection(module_path, modules_root, normalized_selected)
|
|
57
|
+
]
|
|
58
|
+
for selected_module in self.selected:
|
|
59
|
+
normalized_selected_item = self._normalize_selection([selected_module])
|
|
60
|
+
has_match = any(
|
|
61
|
+
self._matches_selection(found_module, modules_root, normalized_selected_item)
|
|
62
|
+
for found_module in all_module_paths
|
|
63
|
+
)
|
|
64
|
+
if not has_match:
|
|
65
|
+
self.issues.append(
|
|
66
|
+
ModuleLoadingIssue(
|
|
67
|
+
message=f"Module '{selected_module}' not found",
|
|
68
|
+
)
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
module_paths = selected_module_paths
|
|
72
|
+
|
|
73
|
+
valid_module_paths: list[Path] = []
|
|
74
|
+
for module_path in module_paths:
|
|
75
|
+
valid_module_path, issue = self._check_resource_folder_content(module_path)
|
|
76
|
+
if issue:
|
|
77
|
+
self.issues.append(issue)
|
|
78
|
+
if valid_module_path:
|
|
79
|
+
valid_module_paths.append(valid_module_path)
|
|
80
|
+
|
|
81
|
+
return valid_module_paths
|
|
82
|
+
|
|
83
|
+
def _get_module_path_from_resource_file_path(self, resource_file: Path) -> Path | None:
|
|
84
|
+
# recognize the module by traversing the parents of the resource file until we find a CRUD folder
|
|
85
|
+
|
|
86
|
+
for parent in resource_file.parents:
|
|
87
|
+
if parent.name in CRUDS_BY_FOLDER_NAME_INCLUDE_ALPHA:
|
|
88
|
+
# special case: if the crud is FunctionCRUD, the resource file has to be a direct descendent.
|
|
89
|
+
if parent.name == "functions" and resource_file.parent.name != "functions":
|
|
90
|
+
return None
|
|
91
|
+
return parent.parent
|
|
92
|
+
if parent.name == MODULES:
|
|
93
|
+
return parent
|
|
94
|
+
return None
|
|
95
|
+
|
|
96
|
+
def _check_resource_folder_content(self, module_path: Path) -> tuple[None | Path, ModuleLoadingIssue | None]:
|
|
97
|
+
resource_folder_names = {d.name for d in module_path.iterdir() if d.is_dir()}
|
|
98
|
+
unrecognized_resource_folder_names = resource_folder_names - CRUDS_BY_FOLDER_NAME.keys()
|
|
99
|
+
|
|
100
|
+
issue = (
|
|
101
|
+
ModuleLoadingIssue(
|
|
102
|
+
message=f"Module {module_path_display_name(self.organization_dir, module_path)!r} contains unrecognized resource folder(s): {', '.join(unrecognized_resource_folder_names)}"
|
|
103
|
+
)
|
|
104
|
+
if unrecognized_resource_folder_names
|
|
105
|
+
else None
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
has_valid_resource_folders = bool(resource_folder_names & CRUDS_BY_FOLDER_NAME.keys())
|
|
109
|
+
return (module_path if has_valid_resource_folders else None, issue)
|
|
110
|
+
|
|
111
|
+
def _matches_selection(
|
|
112
|
+
self, module_path: Path, modules_root: Path, normalized_selected: list[tuple[str, ...]] | None
|
|
113
|
+
) -> bool:
|
|
114
|
+
if not normalized_selected:
|
|
115
|
+
return True
|
|
116
|
+
|
|
117
|
+
rel = module_path.relative_to(modules_root)
|
|
118
|
+
rel_parts = tuple(p.lower() for p in rel.parts)
|
|
119
|
+
if not rel_parts:
|
|
120
|
+
# module_path is the modules_root itself
|
|
121
|
+
return False
|
|
122
|
+
name_lower = rel_parts[-1]
|
|
123
|
+
|
|
124
|
+
for sel_parts in normalized_selected:
|
|
125
|
+
if not sel_parts:
|
|
126
|
+
return True
|
|
127
|
+
|
|
128
|
+
if len(sel_parts) == 1 and name_lower == sel_parts[0]:
|
|
129
|
+
return True
|
|
130
|
+
|
|
131
|
+
if rel_parts[: len(sel_parts)] == sel_parts:
|
|
132
|
+
return True
|
|
133
|
+
|
|
134
|
+
return False
|
|
135
|
+
|
|
136
|
+
def _normalize_selection(self, selected: list[str | Path]) -> list[tuple[str, ...]]:
|
|
137
|
+
normalized: list[tuple[str, ...]] = []
|
|
138
|
+
modules_lower = MODULES.lower()
|
|
139
|
+
for sel in selected:
|
|
140
|
+
if isinstance(sel, Path):
|
|
141
|
+
sel_parts = sel.parts
|
|
142
|
+
else:
|
|
143
|
+
sel_parts = tuple(part for part in str(sel).replace("\\", "/").split("/") if part)
|
|
144
|
+
|
|
145
|
+
sel_parts_lower = tuple(part.lower() for part in sel_parts)
|
|
146
|
+
if sel_parts_lower and sel_parts_lower[0] == modules_lower:
|
|
147
|
+
sel_parts_lower = sel_parts_lower[1:]
|
|
148
|
+
|
|
149
|
+
normalized.append(sel_parts_lower)
|
|
150
|
+
|
|
151
|
+
return normalized
|
|
152
|
+
|
|
153
|
+
def _find_modules_with_submodules(self, module_paths: list[Path]) -> list[Path]:
|
|
154
|
+
"""Remove parent modules when they have submodules. Keep only the deepest modules."""
|
|
155
|
+
return [
|
|
156
|
+
module_path
|
|
157
|
+
for module_path in module_paths
|
|
158
|
+
if not any(
|
|
159
|
+
module_path in other_module_path.parents
|
|
160
|
+
for other_module_path in module_paths
|
|
161
|
+
if other_module_path != module_path
|
|
162
|
+
)
|
|
163
|
+
]
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from itertools import groupby
|
|
1
3
|
from pathlib import Path
|
|
2
|
-
from typing import Any, Literal
|
|
4
|
+
from typing import Any, Literal
|
|
3
5
|
|
|
4
6
|
from rich import print
|
|
5
7
|
from rich.panel import Panel
|
|
@@ -7,39 +9,29 @@ from rich.panel import Panel
|
|
|
7
9
|
from cognite_toolkit._cdf_tk.client import ToolkitClient
|
|
8
10
|
from cognite_toolkit._cdf_tk.commands._base import ToolkitCommand
|
|
9
11
|
from cognite_toolkit._cdf_tk.commands.build_cmd import BuildCommand as OldBuildCommand
|
|
10
|
-
from cognite_toolkit._cdf_tk.commands.build_v2.
|
|
11
|
-
from cognite_toolkit._cdf_tk.commands.build_v2.
|
|
12
|
-
from cognite_toolkit._cdf_tk.data_classes import
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
BuiltModuleList,
|
|
16
|
-
ModuleDirectories,
|
|
17
|
-
)
|
|
12
|
+
from cognite_toolkit._cdf_tk.commands.build_v2._modules_parser import ModulesParser
|
|
13
|
+
from cognite_toolkit._cdf_tk.commands.build_v2.build_parameters import BuildParameters
|
|
14
|
+
from cognite_toolkit._cdf_tk.commands.build_v2.data_classes._modules import Module
|
|
15
|
+
from cognite_toolkit._cdf_tk.data_classes import BuildConfigYAML, BuildVariables, BuiltModuleList
|
|
16
|
+
from cognite_toolkit._cdf_tk.data_classes._issues import Issue, IssueList
|
|
18
17
|
from cognite_toolkit._cdf_tk.exceptions import ToolkitError
|
|
19
|
-
from cognite_toolkit._cdf_tk.hints import verify_module_directory
|
|
20
18
|
from cognite_toolkit._cdf_tk.tk_warnings import ToolkitWarning, WarningList
|
|
21
19
|
from cognite_toolkit._cdf_tk.utils.file import safe_rmtree
|
|
22
|
-
from cognite_toolkit._cdf_tk.validation import validate_module_selection, validate_modules_variables
|
|
23
20
|
from cognite_toolkit._version import __version__
|
|
24
21
|
|
|
25
22
|
|
|
26
|
-
class BuildWarnings(TypedDict):
|
|
27
|
-
warning: ToolkitWarning
|
|
28
|
-
location: list[Path]
|
|
29
|
-
|
|
30
|
-
|
|
31
23
|
class BuildCommand(ToolkitCommand):
|
|
32
24
|
def __init__(self, print_warning: bool = True, skip_tracking: bool = False, silent: bool = False) -> None:
|
|
33
25
|
super().__init__(print_warning, skip_tracking, silent)
|
|
34
|
-
self.issues =
|
|
26
|
+
self.issues = IssueList()
|
|
35
27
|
|
|
36
28
|
def execute(
|
|
37
29
|
self,
|
|
38
30
|
verbose: bool,
|
|
39
|
-
|
|
31
|
+
base_dir: Path,
|
|
40
32
|
build_dir: Path,
|
|
41
33
|
selected: list[str | Path] | None,
|
|
42
|
-
|
|
34
|
+
build_env: str | None,
|
|
43
35
|
no_clean: bool,
|
|
44
36
|
client: ToolkitClient | None = None,
|
|
45
37
|
on_error: Literal["continue", "raise"] = "continue",
|
|
@@ -51,30 +43,48 @@ class BuildCommand(ToolkitCommand):
|
|
|
51
43
|
self.verbose = verbose
|
|
52
44
|
self.on_error = on_error
|
|
53
45
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
46
|
+
build_parameters = BuildParameters.load(
|
|
47
|
+
organization_dir=base_dir,
|
|
48
|
+
build_dir=build_dir,
|
|
49
|
+
build_env_name=build_env,
|
|
50
|
+
client=client,
|
|
51
|
+
user_selected=selected,
|
|
52
|
+
)
|
|
61
53
|
|
|
62
54
|
# Print the build input.
|
|
63
55
|
if self.verbose:
|
|
64
|
-
self._print_build_input(
|
|
56
|
+
self._print_build_input(build_parameters)
|
|
65
57
|
|
|
66
|
-
#
|
|
67
|
-
if
|
|
68
|
-
self.
|
|
58
|
+
# Tracking the project and cluster for the build.
|
|
59
|
+
if build_parameters.client:
|
|
60
|
+
self._additional_tracking_info.project = build_parameters.client.config.project
|
|
61
|
+
self._additional_tracking_info.cluster = build_parameters.client.config.cdf_cluster
|
|
62
|
+
|
|
63
|
+
# Load modules
|
|
64
|
+
modules_parser = ModulesParser(organization_dir=base_dir, selected=selected)
|
|
65
|
+
module_paths = modules_parser.parse()
|
|
66
|
+
module_loading_issues = modules_parser.issues
|
|
67
|
+
if module_loading_issues:
|
|
68
|
+
self.issues.extend(module_loading_issues)
|
|
69
|
+
self._print_or_log_issues_by_category(self.issues)
|
|
70
|
+
raise ToolkitError("Module loading issues encountered. Cannot continue. See above for details.")
|
|
71
|
+
|
|
72
|
+
# Load modules
|
|
73
|
+
if module_paths:
|
|
74
|
+
pass
|
|
75
|
+
|
|
76
|
+
# modules = [Module.load(path) for path in module_paths]
|
|
77
|
+
# for module in modules:
|
|
78
|
+
# continue
|
|
69
79
|
|
|
70
80
|
# Logistics: clean and create build directory
|
|
71
|
-
if prepare_issues := self._prepare_target_directory(
|
|
81
|
+
if prepare_issues := self._prepare_target_directory(build_dir, not no_clean):
|
|
72
82
|
self.issues.extend(prepare_issues)
|
|
73
83
|
|
|
74
84
|
# Compile the configuration and variables,
|
|
75
85
|
# check syntax on module and resource level
|
|
76
86
|
# for any "compilation errors and warnings"
|
|
77
|
-
built_modules, build_integrity_issues = self._build_configuration(
|
|
87
|
+
built_modules, build_integrity_issues = self._build_configuration(build_parameters)
|
|
78
88
|
if build_integrity_issues:
|
|
79
89
|
self.issues.extend(build_integrity_issues)
|
|
80
90
|
|
|
@@ -83,84 +93,48 @@ class BuildCommand(ToolkitCommand):
|
|
|
83
93
|
self.issues.extend(build_quality_issues)
|
|
84
94
|
|
|
85
95
|
# Finally, print warnings grouped by category/code and location.
|
|
86
|
-
self.
|
|
96
|
+
self._print_or_log_issues_by_category(self.issues)
|
|
87
97
|
|
|
88
98
|
return built_modules
|
|
89
99
|
|
|
90
|
-
def _print_build_input(self,
|
|
100
|
+
def _print_build_input(self, build_input: BuildParameters) -> None:
|
|
91
101
|
print(
|
|
92
102
|
Panel(
|
|
93
|
-
f"Building {
|
|
94
|
-
f" - Environment name {
|
|
95
|
-
f" - Config '{
|
|
103
|
+
f"Building {build_input.organization_dir!s}:\n - Toolkit Version '{__version__!s}'\n"
|
|
104
|
+
f" - Environment name {build_input.build_env_name!r}, validation-type {build_input.config.environment.validation_type!r}.\n"
|
|
105
|
+
f" - Config '{build_input.config.filepath!s}'",
|
|
96
106
|
expand=False,
|
|
97
107
|
)
|
|
98
108
|
)
|
|
99
109
|
|
|
100
|
-
def _prepare_target_directory(self,
|
|
110
|
+
def _prepare_target_directory(self, build_dir: Path, clean: bool = False) -> IssueList:
|
|
101
111
|
"""
|
|
102
112
|
Directory logistics
|
|
103
113
|
"""
|
|
104
|
-
issues =
|
|
105
|
-
if
|
|
114
|
+
issues = IssueList()
|
|
115
|
+
if build_dir.exists() and any(build_dir.iterdir()):
|
|
106
116
|
if not clean:
|
|
107
117
|
raise ToolkitError("Build directory is not empty. Run without --no-clean to remove existing files.")
|
|
108
118
|
|
|
109
119
|
if self.verbose:
|
|
110
|
-
issues.append(
|
|
111
|
-
safe_rmtree(
|
|
112
|
-
|
|
120
|
+
issues.append(Issue(code="BUILD_001"))
|
|
121
|
+
safe_rmtree(build_dir)
|
|
122
|
+
build_dir.mkdir(parents=True, exist_ok=True)
|
|
113
123
|
return issues
|
|
114
124
|
|
|
115
|
-
def
|
|
116
|
-
issues =
|
|
117
|
-
#
|
|
118
|
-
# and at least one is selected
|
|
119
|
-
verify_module_directory(input.organization_dir, input.build_env_name)
|
|
120
|
-
|
|
121
|
-
# Validate module selection
|
|
122
|
-
user_selected_modules = input.config.environment.get_selected_modules({})
|
|
123
|
-
module_warnings = validate_module_selection(
|
|
124
|
-
modules=input.modules,
|
|
125
|
-
config=input.config,
|
|
126
|
-
packages={},
|
|
127
|
-
selected_modules=user_selected_modules,
|
|
128
|
-
organization_dir=input.organization_dir,
|
|
129
|
-
)
|
|
130
|
-
if module_warnings:
|
|
131
|
-
issues.extend(BuildIssueList.from_warning_list(module_warnings))
|
|
132
|
-
|
|
133
|
-
# Validate variables. Note: this looks for non-replaced template
|
|
134
|
-
# variables <.*?> and can be improved in the future.
|
|
135
|
-
# Keeping for reference.
|
|
136
|
-
variables_warnings = validate_modules_variables(input.variables, input.config.filepath)
|
|
137
|
-
if variables_warnings:
|
|
138
|
-
issues.extend(BuildIssueList.from_warning_list(variables_warnings))
|
|
139
|
-
|
|
140
|
-
# Track LOC of managed configuration
|
|
141
|
-
# Note: _track is not implemented yet, so we skip it for now
|
|
142
|
-
# self._track(input)
|
|
143
|
-
|
|
144
|
-
return issues
|
|
145
|
-
|
|
146
|
-
def _build_configuration(self, input: BuildInput) -> tuple[BuiltModuleList, BuildIssueList]:
|
|
147
|
-
issues = BuildIssueList()
|
|
148
|
-
# Use input.modules.selected directly (it's already a ModuleDirectories)
|
|
149
|
-
if not input.modules.selected:
|
|
150
|
-
return BuiltModuleList(), issues
|
|
151
|
-
|
|
152
|
-
# first collect variables into practical lookup
|
|
153
|
-
# 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.
|
|
125
|
+
def _build_configuration(self, build_input: BuildParameters) -> tuple[BuiltModuleList, IssueList]:
|
|
126
|
+
issues = IssueList()
|
|
127
|
+
# Use build_input.modules directly (it is already filtered by selection)
|
|
154
128
|
|
|
155
129
|
old_build_command = OldBuildCommand(print_warning=False, skip_tracking=False)
|
|
156
130
|
built_modules = old_build_command.build_config(
|
|
157
|
-
build_dir=
|
|
158
|
-
organization_dir=
|
|
159
|
-
config=
|
|
131
|
+
build_dir=build_input.build_dir,
|
|
132
|
+
organization_dir=build_input.organization_dir,
|
|
133
|
+
config=build_input.config,
|
|
160
134
|
packages={},
|
|
161
135
|
clean=False,
|
|
162
136
|
verbose=self.verbose,
|
|
163
|
-
client=
|
|
137
|
+
client=build_input.client,
|
|
164
138
|
progress_bar=False,
|
|
165
139
|
on_error=self.on_error,
|
|
166
140
|
)
|
|
@@ -172,29 +146,42 @@ class BuildCommand(ToolkitCommand):
|
|
|
172
146
|
# Always convert warnings to issues, even if the list appears empty
|
|
173
147
|
# (WarningList might have custom __bool__ behavior)
|
|
174
148
|
if old_build_command.warning_list:
|
|
175
|
-
converted_issues =
|
|
149
|
+
converted_issues = IssueList.from_warning_list(old_build_command.warning_list)
|
|
176
150
|
issues.extend(converted_issues)
|
|
177
151
|
return built_modules, issues
|
|
178
152
|
|
|
179
|
-
def _verify_build_quality(self, built_modules: BuiltModuleList) ->
|
|
180
|
-
issues =
|
|
153
|
+
def _verify_build_quality(self, built_modules: BuiltModuleList) -> IssueList:
|
|
154
|
+
issues = IssueList()
|
|
181
155
|
return issues
|
|
182
156
|
|
|
183
|
-
def _write(self,
|
|
157
|
+
def _write(self, build_input: BuildParameters) -> None:
|
|
184
158
|
# Write the build to the build directory.
|
|
185
159
|
# Track lines of code built.
|
|
186
160
|
raise NotImplementedError()
|
|
187
161
|
|
|
188
|
-
def _track(self,
|
|
162
|
+
def _track(self, build_input: BuildParameters) -> None:
|
|
189
163
|
raise NotImplementedError()
|
|
190
164
|
|
|
191
|
-
def
|
|
192
|
-
|
|
165
|
+
def _print_or_log_issues_by_category(self, issues: IssueList) -> None:
|
|
166
|
+
issues_sorted = sorted(issues, key=self._issue_sort_key)
|
|
167
|
+
for code, grouped_issues in groupby(issues_sorted, key=lambda issue: issue.code or ""):
|
|
168
|
+
print(f"[bold]{code}[/]")
|
|
169
|
+
for issue in grouped_issues:
|
|
170
|
+
message = issue.message or ""
|
|
171
|
+
print(f" - {message}")
|
|
172
|
+
|
|
173
|
+
def _issue_sort_key(self, issue: Issue) -> tuple[str, str]:
|
|
174
|
+
code = issue.code or ""
|
|
175
|
+
if not issue.message:
|
|
176
|
+
return code, ""
|
|
177
|
+
match = re.search(r"'([^']+)'", issue.message)
|
|
178
|
+
path = match.group(1) if match else issue.message
|
|
179
|
+
return code, path
|
|
193
180
|
|
|
194
181
|
# Delegate to old BuildCommand for backward compatibility with tests
|
|
195
182
|
def build_modules(
|
|
196
183
|
self,
|
|
197
|
-
modules:
|
|
184
|
+
modules: list[Module],
|
|
198
185
|
build_dir: Path,
|
|
199
186
|
variables: BuildVariables,
|
|
200
187
|
verbose: bool = False,
|
|
@@ -204,10 +191,10 @@ class BuildCommand(ToolkitCommand):
|
|
|
204
191
|
"""Delegate to old BuildCommand for backward compatibility."""
|
|
205
192
|
old_cmd = OldBuildCommand()
|
|
206
193
|
|
|
207
|
-
built_modules = old_cmd.build_modules(modules, build_dir, variables, verbose, progress_bar, on_error)
|
|
194
|
+
built_modules = old_cmd.build_modules(modules, build_dir, variables, verbose, progress_bar, on_error) # type: ignore[arg-type]
|
|
208
195
|
self._additional_tracking_info.package_ids.update(old_cmd._additional_tracking_info.package_ids)
|
|
209
196
|
self._additional_tracking_info.module_ids.update(old_cmd._additional_tracking_info.module_ids)
|
|
210
|
-
self.issues.extend(
|
|
197
|
+
self.issues.extend(IssueList.from_warning_list(old_cmd.warning_list or WarningList[ToolkitWarning]()))
|
|
211
198
|
return built_modules
|
|
212
199
|
|
|
213
200
|
def build_config(
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import sys
|
|
2
|
-
from functools import cached_property
|
|
3
2
|
from pathlib import Path
|
|
4
3
|
|
|
5
4
|
if sys.version_info >= (3, 11):
|
|
@@ -13,14 +12,12 @@ from cognite_toolkit._cdf_tk.client import ToolkitClient
|
|
|
13
12
|
from cognite_toolkit._cdf_tk.constants import DEFAULT_ENV
|
|
14
13
|
from cognite_toolkit._cdf_tk.data_classes import (
|
|
15
14
|
BuildConfigYAML,
|
|
16
|
-
BuildVariables,
|
|
17
|
-
ModuleDirectories,
|
|
18
15
|
)
|
|
19
16
|
from cognite_toolkit._cdf_tk.tk_warnings import ToolkitWarning, WarningList
|
|
20
17
|
from cognite_toolkit._cdf_tk.utils.modules import parse_user_selected_modules
|
|
21
18
|
|
|
22
19
|
|
|
23
|
-
class
|
|
20
|
+
class BuildParameters(BaseModel):
|
|
24
21
|
"""Input to the build process."""
|
|
25
22
|
|
|
26
23
|
# need this until we turn BuildConfigYaml and ToolkitClient into Pydantic models
|
|
@@ -31,8 +28,8 @@ class BuildInput(BaseModel):
|
|
|
31
28
|
build_env_name: str
|
|
32
29
|
config: BuildConfigYAML
|
|
33
30
|
client: ToolkitClient | None = None
|
|
34
|
-
selected: list[str | Path] | None = None
|
|
35
31
|
warnings: WarningList[ToolkitWarning] | None = None
|
|
32
|
+
user_selected: list[str | Path] | None = None
|
|
36
33
|
|
|
37
34
|
@classmethod
|
|
38
35
|
def load(
|
|
@@ -41,24 +38,24 @@ class BuildInput(BaseModel):
|
|
|
41
38
|
build_dir: Path,
|
|
42
39
|
build_env_name: str | None,
|
|
43
40
|
client: ToolkitClient | None,
|
|
44
|
-
|
|
41
|
+
user_selected: list[str | Path] | None = None,
|
|
45
42
|
) -> Self:
|
|
46
43
|
resolved_org_dir = Path.cwd() if organization_dir in {Path("."), Path("./")} else organization_dir
|
|
47
44
|
resolved_env = build_env_name or DEFAULT_ENV
|
|
48
|
-
config, warnings = cls._load_config(resolved_org_dir, resolved_env,
|
|
45
|
+
config, warnings = cls._load_config(resolved_org_dir, resolved_env, user_selected)
|
|
49
46
|
return cls(
|
|
50
47
|
organization_dir=resolved_org_dir,
|
|
51
48
|
build_dir=build_dir,
|
|
52
49
|
build_env_name=resolved_env,
|
|
53
50
|
config=config,
|
|
54
51
|
client=client,
|
|
55
|
-
selected=selected,
|
|
56
52
|
warnings=warnings,
|
|
53
|
+
user_selected=user_selected,
|
|
57
54
|
)
|
|
58
55
|
|
|
59
56
|
@classmethod
|
|
60
57
|
def _load_config(
|
|
61
|
-
cls, organization_dir: Path, build_env_name: str,
|
|
58
|
+
cls, organization_dir: Path, build_env_name: str, user_selected: list[str | Path] | None
|
|
62
59
|
) -> tuple[BuildConfigYAML, WarningList[ToolkitWarning]]:
|
|
63
60
|
warnings: WarningList[ToolkitWarning] = WarningList[ToolkitWarning]()
|
|
64
61
|
if (organization_dir / BuildConfigYAML.get_filename(build_env_name or DEFAULT_ENV)).exists():
|
|
@@ -66,20 +63,9 @@ class BuildInput(BaseModel):
|
|
|
66
63
|
else:
|
|
67
64
|
# Loads the default environment
|
|
68
65
|
config = BuildConfigYAML.load_default(organization_dir)
|
|
69
|
-
if
|
|
70
|
-
config.environment.selected = parse_user_selected_modules(
|
|
66
|
+
if user_selected:
|
|
67
|
+
config.environment.selected = list(set(parse_user_selected_modules(list(user_selected), organization_dir)))
|
|
71
68
|
config.set_environment_variables()
|
|
72
69
|
if environment_warning := config.validate_environment():
|
|
73
70
|
warnings.append(environment_warning)
|
|
74
71
|
return config, warnings
|
|
75
|
-
|
|
76
|
-
@cached_property
|
|
77
|
-
def modules(self) -> ModuleDirectories:
|
|
78
|
-
user_selected_modules = self.config.environment.get_selected_modules({})
|
|
79
|
-
return ModuleDirectories.load(self.organization_dir, user_selected_modules)
|
|
80
|
-
|
|
81
|
-
@cached_property
|
|
82
|
-
def variables(self) -> BuildVariables:
|
|
83
|
-
return BuildVariables.load_raw(
|
|
84
|
-
self.config.variables, self.modules.available_paths, self.modules.selected.available_paths
|
|
85
|
-
)
|