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