snowflake-cli 3.3.0__py3-none-any.whl → 3.4.1__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.
Files changed (77) hide show
  1. snowflake/cli/__about__.py +1 -1
  2. snowflake/cli/_app/__main__.py +2 -2
  3. snowflake/cli/_app/cli_app.py +224 -192
  4. snowflake/cli/_app/commands_registration/commands_registration_with_callbacks.py +1 -27
  5. snowflake/cli/_plugins/cortex/commands.py +2 -4
  6. snowflake/cli/_plugins/git/manager.py +1 -1
  7. snowflake/cli/_plugins/nativeapp/artifacts.py +6 -624
  8. snowflake/cli/_plugins/nativeapp/bundle_context.py +1 -1
  9. snowflake/cli/_plugins/nativeapp/codegen/artifact_processor.py +1 -1
  10. snowflake/cli/_plugins/nativeapp/codegen/compiler.py +1 -3
  11. snowflake/cli/_plugins/nativeapp/codegen/setup/native_app_setup_processor.py +2 -2
  12. snowflake/cli/_plugins/nativeapp/codegen/snowpark/python_processor.py +2 -2
  13. snowflake/cli/_plugins/nativeapp/codegen/templates/templates_processor.py +2 -2
  14. snowflake/cli/_plugins/nativeapp/commands.py +21 -19
  15. snowflake/cli/_plugins/nativeapp/entities/application.py +16 -19
  16. snowflake/cli/_plugins/nativeapp/entities/application_package.py +142 -55
  17. snowflake/cli/_plugins/nativeapp/release_channel/commands.py +37 -3
  18. snowflake/cli/_plugins/nativeapp/release_directive/commands.py +80 -2
  19. snowflake/cli/_plugins/nativeapp/sf_sql_facade.py +224 -44
  20. snowflake/cli/_plugins/nativeapp/v2_conversions/compat.py +2 -2
  21. snowflake/cli/_plugins/nativeapp/version/commands.py +1 -1
  22. snowflake/cli/_plugins/notebook/commands.py +55 -2
  23. snowflake/cli/_plugins/notebook/exceptions.py +1 -1
  24. snowflake/cli/_plugins/notebook/manager.py +3 -3
  25. snowflake/cli/_plugins/notebook/notebook_entity.py +120 -0
  26. snowflake/cli/_plugins/notebook/notebook_entity_model.py +42 -0
  27. snowflake/cli/_plugins/notebook/notebook_project_paths.py +15 -0
  28. snowflake/cli/_plugins/notebook/types.py +3 -0
  29. snowflake/cli/_plugins/snowpark/commands.py +48 -30
  30. snowflake/cli/_plugins/snowpark/common.py +47 -2
  31. snowflake/cli/_plugins/snowpark/snowpark_entity.py +38 -25
  32. snowflake/cli/_plugins/snowpark/snowpark_entity_model.py +18 -30
  33. snowflake/cli/_plugins/snowpark/snowpark_project_paths.py +156 -23
  34. snowflake/cli/_plugins/snowpark/zipper.py +33 -1
  35. snowflake/cli/_plugins/spcs/services/commands.py +0 -3
  36. snowflake/cli/_plugins/stage/commands.py +2 -1
  37. snowflake/cli/_plugins/stage/diff.py +60 -39
  38. snowflake/cli/_plugins/stage/manager.py +24 -11
  39. snowflake/cli/_plugins/stage/utils.py +1 -1
  40. snowflake/cli/_plugins/streamlit/commands.py +10 -1
  41. snowflake/cli/_plugins/streamlit/manager.py +62 -21
  42. snowflake/cli/_plugins/streamlit/streamlit_entity.py +20 -41
  43. snowflake/cli/_plugins/streamlit/streamlit_entity_model.py +14 -24
  44. snowflake/cli/_plugins/streamlit/streamlit_project_paths.py +30 -0
  45. snowflake/cli/_plugins/workspace/commands.py +3 -3
  46. snowflake/cli/_plugins/workspace/manager.py +1 -1
  47. snowflake/cli/api/artifacts/__init__.py +13 -0
  48. snowflake/cli/api/artifacts/bundle_map.py +500 -0
  49. snowflake/cli/api/artifacts/common.py +78 -0
  50. snowflake/cli/api/artifacts/utils.py +82 -0
  51. snowflake/cli/api/cli_global_context.py +14 -1
  52. snowflake/cli/api/commands/flags.py +10 -4
  53. snowflake/cli/api/commands/utils.py +28 -2
  54. snowflake/cli/api/constants.py +1 -0
  55. snowflake/cli/api/entities/common.py +14 -32
  56. snowflake/cli/api/entities/resolver.py +160 -0
  57. snowflake/cli/api/entities/utils.py +56 -15
  58. snowflake/cli/api/errno.py +3 -0
  59. snowflake/cli/api/feature_flags.py +1 -2
  60. snowflake/cli/api/project/definition_conversion.py +3 -2
  61. snowflake/cli/api/project/project_paths.py +28 -0
  62. snowflake/cli/api/project/schemas/entities/common.py +130 -1
  63. snowflake/cli/api/project/schemas/entities/entities.py +4 -0
  64. snowflake/cli/api/project/schemas/project_definition.py +27 -0
  65. snowflake/cli/api/project/schemas/updatable_model.py +2 -2
  66. snowflake/cli/api/project/schemas/v1/native_app/native_app.py +5 -7
  67. snowflake/cli/api/secure_path.py +6 -0
  68. snowflake/cli/api/sql_execution.py +5 -1
  69. snowflake/cli/api/stage_path.py +7 -2
  70. snowflake/cli/api/utils/graph.py +3 -0
  71. snowflake/cli/api/utils/path_utils.py +24 -0
  72. {snowflake_cli-3.3.0.dist-info → snowflake_cli-3.4.1.dist-info}/METADATA +8 -9
  73. {snowflake_cli-3.3.0.dist-info → snowflake_cli-3.4.1.dist-info}/RECORD +76 -67
  74. snowflake/cli/api/project/schemas/v1/native_app/path_mapping.py +0 -65
  75. {snowflake_cli-3.3.0.dist-info → snowflake_cli-3.4.1.dist-info}/WHEEL +0 -0
  76. {snowflake_cli-3.3.0.dist-info → snowflake_cli-3.4.1.dist-info}/entry_points.txt +0 -0
  77. {snowflake_cli-3.3.0.dist-info → snowflake_cli-3.4.1.dist-info}/licenses/LICENSE +0 -0
@@ -14,642 +14,24 @@
14
14
 
15
15
  from __future__ import annotations
16
16
 
17
- import itertools
18
17
  import os
19
18
  from collections import namedtuple
20
19
  from pathlib import Path
21
- from textwrap import dedent
22
- from typing import Any, Callable, Dict, Iterable, Iterator, List, Optional, Tuple, Union
20
+ from typing import Any, Dict, List, Optional
23
21
 
24
22
  from click.exceptions import ClickException
23
+ from snowflake.cli.api.artifacts.bundle_map import BundleMap
24
+ from snowflake.cli.api.artifacts.common import ArtifactError, DeployRootError
25
+ from snowflake.cli.api.artifacts.utils import symlink_or_copy
25
26
  from snowflake.cli.api.cli_global_context import span
26
27
  from snowflake.cli.api.constants import DEFAULT_SIZE_LIMIT_MB
27
- from snowflake.cli.api.project.schemas.v1.native_app.path_mapping import PathMapping
28
+ from snowflake.cli.api.project.schemas.entities.common import PathMapping
28
29
  from snowflake.cli.api.project.util import to_identifier
29
30
  from snowflake.cli.api.secure_path import SecurePath
31
+ from snowflake.cli.api.utils.path_utils import delete
30
32
  from yaml import safe_load
31
33
 
32
34
 
33
- class DeployRootError(ClickException):
34
- """
35
- The deploy root was incorrectly specified.
36
- """
37
-
38
- def __init__(self, msg: str):
39
- super().__init__(msg)
40
-
41
-
42
- class ArtifactError(ClickException):
43
- """
44
- Could not parse source or destination artifact.
45
- """
46
-
47
- def __init__(self, msg: str):
48
- super().__init__(msg)
49
-
50
-
51
- class SourceNotFoundError(ClickException):
52
- """
53
- No match was found for the specified source in the project directory
54
- """
55
-
56
- def __init__(self, src: Union[str, Path]):
57
- super().__init__(f"{dedent(str(self.__doc__))}: {src}".strip())
58
-
59
-
60
- class TooManyFilesError(ClickException):
61
- """
62
- Multiple file or directories were mapped to one output destination.
63
- """
64
-
65
- dest_path: Path
66
-
67
- def __init__(self, dest_path: Path):
68
- super().__init__(
69
- f"{dedent(str(self.__doc__))}\ndestination = {dest_path}".strip()
70
- )
71
- self.dest_path = dest_path
72
-
73
-
74
- class NotInDeployRootError(ClickException):
75
- """
76
- The specified destination path is outside of the deploy root, or
77
- would entirely replace it. This can happen when a relative path
78
- with ".." is provided, or when "." is used as the destination
79
- (use "./" instead to copy into the deploy root).
80
- """
81
-
82
- dest_path: Union[str, Path]
83
- deploy_root: Path
84
- src_path: Optional[Union[str, Path]]
85
-
86
- def __init__(
87
- self,
88
- *,
89
- dest_path: Union[Path, str],
90
- deploy_root: Path,
91
- src_path: Optional[Union[str, Path]] = None,
92
- ):
93
- message = dedent(str(self.__doc__))
94
- message += f"\ndestination = {dest_path}"
95
- message += f"\ndeploy root = {deploy_root}"
96
- if src_path is not None:
97
- message += f"""\nsource = {src_path}"""
98
- super().__init__(message.strip())
99
- self.dest_path = dest_path
100
- self.deploy_root = deploy_root
101
- self.src_path = src_path
102
-
103
-
104
- ArtifactPredicate = Callable[[Path, Path], bool]
105
-
106
-
107
- class _ArtifactPathMap:
108
- """
109
- A specialized version of an ordered multimap used to keep track of artifact
110
- source-destination mappings. The mapping is bidirectional, so it can be queried
111
- by source or destination paths. All paths manipulated by this class must be in
112
- relative, canonical form (relative to the project or deploy roots, as appropriate).
113
- """
114
-
115
- def __init__(self, project_root: Path):
116
- self._project_root = project_root
117
-
118
- # All (src,dest) pairs in inserting order, for iterating
119
- self.__src_dest_pairs: List[Tuple[Path, Path]] = []
120
- # built-in dict instances are ordered as of Python 3.7
121
- self.__src_to_dest: Dict[Path, List[Path]] = {}
122
- self.__dest_to_src: Dict[Path, Optional[Path]] = {}
123
-
124
- # This dictionary accumulates keys for each directory or file to be created in
125
- # the deploy root for any artifact mapping rule being processed. This includes
126
- # children of directories that are copied to the deploy root. Having this
127
- # information available is critical to detect possible clashes between rules.
128
- self._dest_is_dir: Dict[Path, bool] = {}
129
-
130
- def put(self, src: Path, dest: Path, dest_is_dir: bool) -> None:
131
- """
132
- Adds a new source-destination mapping pair to this map, if necessary. Note that
133
- this is internal logic that assumes that src-dest pairs have already been preprocessed
134
- by the enclosing BundleMap (for example, only file -> file and
135
- directory -> directory mappings are possible here due to the preprocessing step).
136
-
137
- Arguments:
138
- src {Path} -- the source path, in canonical form.
139
- dest {Path} -- the destination path, in canonical form.
140
- dest_is_dir {bool} -- whether the destination path is a directory.
141
- """
142
- # Both paths should be in canonical form
143
- assert not src.is_absolute()
144
- assert not dest.is_absolute()
145
-
146
- absolute_src = self._project_root / src
147
-
148
- current_source = self.__dest_to_src.get(dest)
149
- src_is_dir = absolute_src.is_dir()
150
- if dest_is_dir:
151
- assert src_is_dir # file -> directory is not possible here given how rules are processed
152
-
153
- # directory -> directory
154
- # Check that dest is currently unmapped
155
- current_is_dir = self._dest_is_dir.get(dest, False)
156
- if current_is_dir:
157
- # mapping to an existing directory is not allowed
158
- raise TooManyFilesError(dest)
159
- else:
160
- # file -> file
161
- # Check that there is no previous mapping for the same file.
162
- if current_source is not None and current_source != src:
163
- # There is already a different source mapping to this destination
164
- raise TooManyFilesError(dest)
165
-
166
- if src_is_dir:
167
- # mark all subdirectories of this source as directories so that we can
168
- # detect accidental clobbering
169
- for root, _, files in os.walk(absolute_src, followlinks=True):
170
- canonical_subdir = Path(root).relative_to(absolute_src)
171
- canonical_dest_subdir = dest / canonical_subdir
172
- self._update_dest_is_dir(canonical_dest_subdir, is_dir=True)
173
- for f in files:
174
- self._update_dest_is_dir(canonical_dest_subdir / f, is_dir=False)
175
-
176
- # make sure we check for dest_is_dir consistency regardless of whether the
177
- # insertion happened. This update can fail, so we need to do it first to
178
- # avoid applying partial updates to the underlying data storage.
179
- self._update_dest_is_dir(dest, dest_is_dir)
180
-
181
- dests = self.__src_to_dest.setdefault(src, [])
182
- if dest not in dests:
183
- dests.append(dest)
184
- self.__dest_to_src[dest] = src
185
- self.__src_dest_pairs.append((src, dest))
186
-
187
- def get_source(self, dest: Path) -> Optional[Path]:
188
- """
189
- Returns the source path associated with the provided destination path, if any.
190
- """
191
- return self.__dest_to_src.get(dest)
192
-
193
- def get_destinations(self, src: Path) -> Iterable[Path]:
194
- """
195
- Returns all destination paths associated with the provided source path, in insertion order.
196
- """
197
- return self.__src_to_dest.get(src, [])
198
-
199
- def all_sources(self) -> Iterable[Path]:
200
- """
201
- Returns all source paths associated with this map, in insertion order.
202
- """
203
- return self.__src_to_dest.keys()
204
-
205
- def is_empty(self) -> bool:
206
- """
207
- Returns True if this map has no source-destination mappings.
208
- """
209
- return len(self.__src_dest_pairs) == 0
210
-
211
- def __iter__(self) -> Iterator[Tuple[Path, Path]]:
212
- """
213
- Returns all (source, destination) pairs known to this map, in insertion order.
214
- """
215
- return iter(self.__src_dest_pairs)
216
-
217
- def _update_dest_is_dir(self, dest: Path, is_dir: bool) -> None:
218
- """
219
- Recursively marks seen destination paths as either files or folders, raising an error if any inconsistencies
220
- from previous invocations of this method are encountered.
221
-
222
- Arguments:
223
- dest {Path} -- the destination path, in canonical form.
224
- is_dir {bool} -- whether the destination path is a directory.
225
- """
226
- assert not dest.is_absolute() # dest must be in canonical relative form
227
-
228
- current_is_dir = self._dest_is_dir.get(dest, None)
229
- if current_is_dir is not None and current_is_dir != is_dir:
230
- raise ArtifactError(f"Conflicting type for destination path: {dest}")
231
-
232
- parent = dest.parent
233
- if parent != dest:
234
- self._update_dest_is_dir(parent, True)
235
-
236
- self._dest_is_dir[dest] = is_dir
237
-
238
-
239
- class BundleMap:
240
- """
241
- Computes the mapping between project directory artifacts (aka source artifacts) to their deploy root location
242
- (aka destination artifact). This information is primarily used when bundling a native applications project.
243
-
244
- :param project_root: The root directory of the project and base for all relative paths. Must be an absolute path.
245
- :param deploy_root: The directory where artifacts should be copied to. Must be an absolute path.
246
- """
247
-
248
- def __init__(self, *, project_root: Path, deploy_root: Path):
249
- # If a relative path ends up here, it's a bug in the app and can lead to other
250
- # subtle bugs as paths would be resolved relative to the current working directory.
251
- assert (
252
- project_root.is_absolute()
253
- ), f"Project root {project_root} must be an absolute path."
254
- assert (
255
- deploy_root.is_absolute()
256
- ), f"Deploy root {deploy_root} must be an absolute path."
257
-
258
- self._project_root: Path = resolve_without_follow(project_root)
259
- self._deploy_root: Path = resolve_without_follow(deploy_root)
260
- self._artifact_map = _ArtifactPathMap(project_root=self._project_root)
261
-
262
- def is_empty(self) -> bool:
263
- return self._artifact_map.is_empty()
264
-
265
- def deploy_root(self) -> Path:
266
- return self._deploy_root
267
-
268
- def project_root(self) -> Path:
269
- return self._project_root
270
-
271
- def _add(self, src: Path, dest: Path, map_as_child: bool) -> None:
272
- """
273
- Adds the specified artifact mapping rule to this map.
274
-
275
- Arguments:
276
- src {Path} -- the source path
277
- dest {Path} -- the destination path
278
- map_as_child {bool} -- when True, the source will be added as a child of the specified destination.
279
- """
280
- absolute_src = self._absolute_src(src)
281
- absolute_dest = self._absolute_dest(dest, src_path=src)
282
- dest_is_dir = absolute_src.is_dir() or map_as_child
283
-
284
- # Check for the special case of './' as a target ('.' is not allowed)
285
- if absolute_dest == self._deploy_root and not map_as_child:
286
- raise NotInDeployRootError(
287
- dest_path=dest, deploy_root=self._deploy_root, src_path=src
288
- )
289
-
290
- if self._deploy_root in absolute_src.parents:
291
- # ignore this item since it's in the deploy root. This can happen if the bundle map is created
292
- # after the bundle step and a project is using rules that are not sufficiently constrained.
293
- # Since the bundle step starts with deleting the deploy root, we wouldn't normally encounter this situation.
294
- return
295
-
296
- canonical_src = self._canonical_src(src)
297
- canonical_dest = self._canonical_dest(dest)
298
-
299
- if map_as_child:
300
- # Make sure the destination is a child of the original, since this was requested
301
- canonical_dest = canonical_dest / canonical_src.name
302
- dest_is_dir = absolute_src.is_dir()
303
-
304
- self._artifact_map.put(
305
- src=canonical_src, dest=canonical_dest, dest_is_dir=dest_is_dir
306
- )
307
-
308
- def _add_mapping(self, src: str, dest: Optional[str] = None):
309
- """
310
- Adds the specified artifact rule to this instance. The source should be relative to the project directory. It
311
- is interpreted as a file, directory or glob pattern. If the destination path is not specified, each source match
312
- is mapped to an identical path in the deploy root.
313
- """
314
- match_found = False
315
-
316
- src_path = Path(src)
317
- if src_path.is_absolute():
318
- raise ArtifactError("Source path must be a relative path")
319
-
320
- for resolved_src in self._project_root.glob(src):
321
- match_found = True
322
-
323
- if dest:
324
- dest_stem = dest.rstrip("/")
325
- if not dest_stem:
326
- # handle '/' as the destination as a special case. This is because specifying only '/' as a
327
- # a destination looks like '.' once all forwards slashes are stripped. If we don't handle it
328
- # specially here, `dest: /` would incorrectly be allowed.
329
- raise NotInDeployRootError(
330
- dest_path=dest,
331
- deploy_root=self._deploy_root,
332
- src_path=resolved_src,
333
- )
334
- dest_path = Path(dest.rstrip("/"))
335
- if dest_path.is_absolute():
336
- raise ArtifactError("Destination path must be a relative path")
337
- self._add(resolved_src, dest_path, specifies_directory(dest))
338
- else:
339
- self._add(
340
- resolved_src,
341
- resolved_src.relative_to(self._project_root),
342
- False,
343
- )
344
-
345
- if not match_found:
346
- raise SourceNotFoundError(src)
347
-
348
- def add(self, mapping: PathMapping) -> None:
349
- """
350
- Adds an artifact mapping rule to this instance.
351
- """
352
- self._add_mapping(mapping.src, mapping.dest)
353
-
354
- def _expand_artifact_mapping(
355
- self,
356
- src: Path,
357
- dest: Path,
358
- absolute: bool = False,
359
- expand_directories: bool = False,
360
- predicate: ArtifactPredicate = lambda src, dest: True,
361
- ) -> Iterator[Tuple[Path, Path]]:
362
- """
363
- Expands the specified source-destination mapping according to the provided options.
364
- The original mapping is yielded, followed by any expanded mappings derived from
365
- it.
366
-
367
- Arguments:
368
- src {Path} -- the source path
369
- dest {Path} -- the destination path
370
- absolute {bool} -- when True, all mappings will be yielded as absolute paths
371
- expand_directories {bool} -- when True, child mappings are yielded if the source path is a directory.
372
- predicate {ArtifactPredicate} -- when specified, only mappings satisfying this predicate will be yielded.
373
- """
374
- canonical_src = self._canonical_src(src)
375
- canonical_dest = self._canonical_dest(dest)
376
-
377
- absolute_src = self._absolute_src(canonical_src)
378
- absolute_dest = self._absolute_dest(canonical_dest)
379
- src_for_output = self._to_output_src(absolute_src, absolute)
380
- dest_for_output = self._to_output_dest(absolute_dest, absolute)
381
-
382
- if predicate(src_for_output, dest_for_output):
383
- yield src_for_output, dest_for_output
384
-
385
- if absolute_src.is_dir() and expand_directories:
386
- # both src and dest are directories, and expanding directories was requested. Traverse src, and map each
387
- # file to the dest directory
388
- for root, subdirs, files in os.walk(absolute_src, followlinks=True):
389
- relative_root = Path(root).relative_to(absolute_src)
390
- for name in itertools.chain(subdirs, files):
391
- src_file_for_output = src_for_output / relative_root / name
392
- dest_file_for_output = dest_for_output / relative_root / name
393
- if predicate(src_file_for_output, dest_file_for_output):
394
- yield src_file_for_output, dest_file_for_output
395
-
396
- def all_mappings(
397
- self,
398
- absolute: bool = False,
399
- expand_directories: bool = False,
400
- predicate: ArtifactPredicate = lambda src, dest: True,
401
- ) -> Iterator[Tuple[Path, Path]]:
402
- """
403
- Yields a (src, dest) pair for each deployed artifact in the project. Each pair corresponds to a single file
404
- in the project. Source directories are resolved as needed to resolve their contents.
405
-
406
- Arguments:
407
- self: this instance
408
- absolute (bool): Specifies whether the yielded paths should be joined with the project or deploy roots,
409
- as appropriate.
410
- expand_directories (bool): Specifies whether directory to directory mappings should be expanded to
411
- resolve their contained files.
412
- predicate (PathPredicate): If provided, the predicate is invoked with both the source path and the
413
- destination path as arguments. Only pairs selected by the predicate are returned.
414
-
415
- Returns:
416
- An iterator over all matching deployed artifacts.
417
- """
418
- for src, dest in self._artifact_map:
419
- for deployed_src, deployed_dest in self._expand_artifact_mapping(
420
- src,
421
- dest,
422
- absolute=absolute,
423
- expand_directories=expand_directories,
424
- predicate=predicate,
425
- ):
426
- yield deployed_src, deployed_dest
427
-
428
- def to_deploy_paths(self, src: Path) -> List[Path]:
429
- """
430
- Converts a source path to its corresponding deploy root path. If the input path is relative to the project root,
431
- paths relative to the deploy root are returned. If the input path is absolute, absolute paths are returned.
432
-
433
- Note that the provided source path must be part of a mapping. If the source path is not part of any mapping,
434
- an empty list is returned. For example, if `app/*` is specified as the source of a mapping,
435
- `to_deploy_paths(Path("app"))` will not yield any result.
436
-
437
- Arguments:
438
- src {Path} -- the source path within the project root, in canonical or absolute form.
439
-
440
- Returns:
441
- The deploy root paths for the given source path, or an empty list if no such path exists.
442
- """
443
- is_absolute = src.is_absolute()
444
-
445
- try:
446
- absolute_src = self._absolute_src(src)
447
- if not absolute_src.exists():
448
- return []
449
- canonical_src = self._canonical_src(absolute_src)
450
- except ArtifactError:
451
- # No mapping is possible for this src path
452
- return []
453
-
454
- output_destinations: List[Path] = []
455
-
456
- # 1. Check for exact rule matches for this path
457
- canonical_dests = self._artifact_map.get_destinations(canonical_src)
458
- if canonical_dests:
459
- for d in canonical_dests:
460
- output_destinations.append(self._to_output_dest(d, is_absolute))
461
-
462
- # 2. Check for any matches to parent directories for this path that would
463
- # cause this path to be part of the recursive copy
464
- canonical_parent = canonical_src.parent
465
- canonical_parent_dests = self.to_deploy_paths(canonical_parent)
466
- if canonical_parent_dests:
467
- canonical_child = canonical_src.relative_to(canonical_parent)
468
- for d in canonical_parent_dests:
469
- output_destinations.append(
470
- self._to_output_dest(d / canonical_child, is_absolute)
471
- )
472
-
473
- return output_destinations
474
-
475
- def all_sources(self, absolute: bool = False) -> Iterator[Path]:
476
- """
477
- Yields each registered artifact source in the project.
478
-
479
- Arguments:
480
- self: this instance
481
- absolute (bool): Specifies whether the yielded paths should be joined with the absolute project root.
482
- Returns:
483
- An iterator over all artifact mapping source paths.
484
- """
485
- for src in self._artifact_map.all_sources():
486
- yield self._to_output_src(src, absolute)
487
-
488
- def to_project_path(self, dest: Path) -> Optional[Path]:
489
- """
490
- Converts a deploy root path to its corresponding project source path. If the input path is relative to the
491
- deploy root, a path relative to the project root is returned. If the input path is absolute, an absolute path is
492
- returned.
493
-
494
- Arguments:
495
- dest {Path} -- the destination path within the deploy root, in canonical or absolute form.
496
-
497
- Returns:
498
- The project root path for the given deploy root path, or None if no such path exists.
499
- """
500
- is_absolute = dest.is_absolute()
501
- try:
502
- canonical_dest = self._canonical_dest(dest)
503
- except NotInDeployRootError:
504
- # No mapping possible for the dest path
505
- return None
506
-
507
- # 1. Look for an exact rule matching this path. If we find any, then
508
- # stop searching. This is because each destination path can only originate
509
- # from a single source (however, one source can be copied to multiple destinations).
510
- canonical_src = self._artifact_map.get_source(canonical_dest)
511
- if canonical_src is not None:
512
- return self._to_output_src(canonical_src, is_absolute)
513
-
514
- # 2. No exact match was found, look for a match for parent directories of this
515
- # path, recursively. Stop when a match is found
516
- canonical_parent = canonical_dest.parent
517
- if canonical_parent == canonical_dest:
518
- return None
519
- canonical_parent_src = self.to_project_path(canonical_parent)
520
- if canonical_parent_src is not None:
521
- canonical_child = canonical_dest.relative_to(canonical_parent)
522
- canonical_child_candidate = canonical_parent_src / canonical_child
523
- if self._absolute_src(canonical_child_candidate).exists():
524
- return self._to_output_src(canonical_child_candidate, is_absolute)
525
-
526
- # No mapping for this destination path
527
- return None
528
-
529
- def _absolute_src(self, src: Path) -> Path:
530
- if src.is_absolute():
531
- resolved_src = resolve_without_follow(src)
532
- else:
533
- resolved_src = resolve_without_follow(self._project_root / src)
534
- if self._project_root not in resolved_src.parents:
535
- raise ArtifactError(
536
- f"Source is not in the project root: {src}, root={self._project_root}"
537
- )
538
- return resolved_src
539
-
540
- def _absolute_dest(self, dest: Path, src_path: Optional[Path] = None) -> Path:
541
- if dest.is_absolute():
542
- resolved_dest = resolve_without_follow(dest)
543
- else:
544
- resolved_dest = resolve_without_follow(self._deploy_root / dest)
545
- if (
546
- self._deploy_root != resolved_dest
547
- and self._deploy_root not in resolved_dest.parents
548
- ):
549
- raise NotInDeployRootError(
550
- dest_path=dest, deploy_root=self._deploy_root, src_path=src_path
551
- )
552
-
553
- return resolved_dest
554
-
555
- def _canonical_src(self, src: Path) -> Path:
556
- """
557
- Returns the canonical version of a source path, relative to the project root.
558
- """
559
- absolute_src = self._absolute_src(src)
560
- return absolute_src.relative_to(self._project_root)
561
-
562
- def _canonical_dest(self, dest: Path) -> Path:
563
- """
564
- Returns the canonical version of a destination path, relative to the deploy root.
565
- """
566
- absolute_dest = self._absolute_dest(dest)
567
- return absolute_dest.relative_to(self._deploy_root)
568
-
569
- def _to_output_dest(self, dest: Path, absolute: bool) -> Path:
570
- return self._absolute_dest(dest) if absolute else self._canonical_dest(dest)
571
-
572
- def _to_output_src(self, src: Path, absolute: bool) -> Path:
573
- return self._absolute_src(src) if absolute else self._canonical_src(src)
574
-
575
-
576
- def specifies_directory(s: str) -> bool:
577
- """
578
- Does the path (as seen from the project definition) refer to
579
- a directory? For destination paths, we enforce the usage of a
580
- trailing forward slash (/). Note that we use the forward slash
581
- even on Windows so that snowflake.yml can be shared between OSes.
582
-
583
- This means that to put a file in the root of the stage, we need
584
- to specify "./" as its destination, or omit it (but only if the
585
- file already lives in the project root).
586
- """
587
- return s.endswith("/")
588
-
589
-
590
- def delete(path: Path) -> None:
591
- """
592
- Obliterates whatever is at the given path, or is a no-op if the
593
- given path does not represent a file or directory that exists.
594
- """
595
- spath = SecurePath(path)
596
- if spath.path.is_file():
597
- spath.unlink() # remove the file
598
- elif spath.path.is_dir():
599
- spath.rmdir(recursive=True) # remove dir and all contains
600
-
601
-
602
- def symlink_or_copy(src: Path, dst: Path, deploy_root: Path) -> None:
603
- """
604
- Symlinks files from src to dst. If the src contains parent directories, then copies the empty directory shell to the deploy root.
605
- The directory hierarchy above dst is created if any of those directories do not exist.
606
- """
607
- ssrc = SecurePath(src)
608
- sdst = SecurePath(dst)
609
- sdst.parent.mkdir(parents=True, exist_ok=True)
610
-
611
- # Verify that the mapping isn't accidentally trying to create a file in the project source through symlinks.
612
- # We need to ensure we're resolving symlinks for this check to be effective.
613
- # We are unlikely to hit this if calling the function through bundle map, keeping it here for other future use cases outside bundle.
614
- resolved_dst = dst.resolve()
615
- resolved_deploy_root = deploy_root.resolve()
616
- dst_is_deploy_root = resolved_deploy_root == resolved_dst
617
- if (not dst_is_deploy_root) and (resolved_deploy_root not in resolved_dst.parents):
618
- raise NotInDeployRootError(dest_path=dst, deploy_root=deploy_root, src_path=src)
619
-
620
- absolute_src = resolve_without_follow(src)
621
- if absolute_src.is_file():
622
- delete(dst)
623
- try:
624
- os.symlink(absolute_src, dst)
625
- except OSError:
626
- ssrc.copy(dst)
627
- else:
628
- # 1. Create a new directory in the deploy root
629
- dst.mkdir(exist_ok=True)
630
- # 2. For all children of src, create their counterparts in dst now that it exists
631
- for root, _, files in sorted(os.walk(absolute_src, followlinks=True)):
632
- relative_root = Path(root).relative_to(absolute_src)
633
- absolute_root_in_deploy = Path(dst, relative_root)
634
- absolute_root_in_deploy.mkdir(parents=True, exist_ok=True)
635
- for file in sorted(files):
636
- absolute_file_in_project = Path(absolute_src, relative_root, file)
637
- absolute_file_in_deploy = Path(absolute_root_in_deploy, file)
638
- symlink_or_copy(
639
- src=absolute_file_in_project,
640
- dst=absolute_file_in_deploy,
641
- deploy_root=deploy_root,
642
- )
643
-
644
-
645
- def resolve_without_follow(path: Path) -> Path:
646
- """
647
- Resolves a Path to an absolute version of itself, without following
648
- symlinks like Path.resolve() does.
649
- """
650
- return Path(os.path.abspath(path))
651
-
652
-
653
35
  @span("bundle")
654
36
  def build_bundle(
655
37
  project_root: Path,
@@ -18,7 +18,7 @@ from typing import (
18
18
  List,
19
19
  )
20
20
 
21
- from snowflake.cli.api.project.schemas.v1.native_app.path_mapping import PathMapping
21
+ from snowflake.cli.api.project.schemas.entities.common import PathMapping
22
22
 
23
23
 
24
24
  @dataclass
@@ -20,7 +20,7 @@ from typing import Optional
20
20
 
21
21
  from click import ClickException
22
22
  from snowflake.cli._plugins.nativeapp.bundle_context import BundleContext
23
- from snowflake.cli.api.project.schemas.v1.native_app.path_mapping import (
23
+ from snowflake.cli.api.project.schemas.entities.common import (
24
24
  PathMapping,
25
25
  ProcessorMapping,
26
26
  )
@@ -36,9 +36,7 @@ from snowflake.cli._plugins.nativeapp.codegen.templates.templates_processor impo
36
36
  from snowflake.cli.api.cli_global_context import get_cli_context
37
37
  from snowflake.cli.api.console import cli_console as cc
38
38
  from snowflake.cli.api.metrics import CLICounterField
39
- from snowflake.cli.api.project.schemas.v1.native_app.path_mapping import (
40
- ProcessorMapping,
41
- )
39
+ from snowflake.cli.api.project.schemas.entities.common import ProcessorMapping
42
40
 
43
41
  ProcessorClassType = type[ArtifactProcessor]
44
42