snowflake-cli 2.8.2__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 (240) hide show
  1. snowflake/cli/__about__.py +17 -0
  2. snowflake/cli/__init__.py +13 -0
  3. snowflake/cli/api/__init__.py +48 -0
  4. snowflake/cli/api/cli_global_context.py +390 -0
  5. snowflake/cli/api/commands/__init__.py +13 -0
  6. snowflake/cli/api/commands/alias.py +23 -0
  7. snowflake/cli/api/commands/decorators.py +354 -0
  8. snowflake/cli/api/commands/execution_metadata.py +40 -0
  9. snowflake/cli/api/commands/experimental_behaviour.py +19 -0
  10. snowflake/cli/api/commands/flags.py +662 -0
  11. snowflake/cli/api/commands/project_initialisation.py +65 -0
  12. snowflake/cli/api/commands/snow_typer.py +237 -0
  13. snowflake/cli/api/commands/typer_pre_execute.py +26 -0
  14. snowflake/cli/api/config.py +348 -0
  15. snowflake/cli/api/console/__init__.py +17 -0
  16. snowflake/cli/api/console/abc.py +89 -0
  17. snowflake/cli/api/console/console.py +134 -0
  18. snowflake/cli/api/console/enum.py +17 -0
  19. snowflake/cli/api/constants.py +79 -0
  20. snowflake/cli/api/errno.py +27 -0
  21. snowflake/cli/api/exceptions.py +164 -0
  22. snowflake/cli/api/feature_flags.py +55 -0
  23. snowflake/cli/api/identifiers.py +167 -0
  24. snowflake/cli/api/output/__init__.py +13 -0
  25. snowflake/cli/api/output/formats.py +20 -0
  26. snowflake/cli/api/output/types.py +118 -0
  27. snowflake/cli/api/plugins/__init__.py +13 -0
  28. snowflake/cli/api/plugins/command/__init__.py +72 -0
  29. snowflake/cli/api/plugins/command/plugin_hook_specs.py +21 -0
  30. snowflake/cli/api/plugins/plugin_config.py +32 -0
  31. snowflake/cli/api/project/__init__.py +13 -0
  32. snowflake/cli/api/project/definition.py +84 -0
  33. snowflake/cli/api/project/definition_manager.py +134 -0
  34. snowflake/cli/api/project/errors.py +56 -0
  35. snowflake/cli/api/project/project_verification.py +23 -0
  36. snowflake/cli/api/project/schemas/__init__.py +13 -0
  37. snowflake/cli/api/project/schemas/entities/application_entity.py +44 -0
  38. snowflake/cli/api/project/schemas/entities/application_package_entity.py +66 -0
  39. snowflake/cli/api/project/schemas/entities/common.py +78 -0
  40. snowflake/cli/api/project/schemas/entities/entities.py +30 -0
  41. snowflake/cli/api/project/schemas/identifier_model.py +49 -0
  42. snowflake/cli/api/project/schemas/native_app/__init__.py +13 -0
  43. snowflake/cli/api/project/schemas/native_app/application.py +62 -0
  44. snowflake/cli/api/project/schemas/native_app/native_app.py +93 -0
  45. snowflake/cli/api/project/schemas/native_app/package.py +78 -0
  46. snowflake/cli/api/project/schemas/native_app/path_mapping.py +65 -0
  47. snowflake/cli/api/project/schemas/project_definition.py +199 -0
  48. snowflake/cli/api/project/schemas/snowpark/__init__.py +13 -0
  49. snowflake/cli/api/project/schemas/snowpark/argument.py +28 -0
  50. snowflake/cli/api/project/schemas/snowpark/callable.py +69 -0
  51. snowflake/cli/api/project/schemas/snowpark/snowpark.py +36 -0
  52. snowflake/cli/api/project/schemas/streamlit/__init__.py +13 -0
  53. snowflake/cli/api/project/schemas/streamlit/streamlit.py +46 -0
  54. snowflake/cli/api/project/schemas/template.py +77 -0
  55. snowflake/cli/api/project/schemas/updatable_model.py +194 -0
  56. snowflake/cli/api/project/util.py +261 -0
  57. snowflake/cli/api/rendering/__init__.py +13 -0
  58. snowflake/cli/api/rendering/jinja.py +112 -0
  59. snowflake/cli/api/rendering/project_definition_templates.py +39 -0
  60. snowflake/cli/api/rendering/project_templates.py +98 -0
  61. snowflake/cli/api/rendering/sql_templates.py +60 -0
  62. snowflake/cli/api/rest_api.py +172 -0
  63. snowflake/cli/api/sanitizers.py +43 -0
  64. snowflake/cli/api/secure_path.py +362 -0
  65. snowflake/cli/api/secure_utils.py +29 -0
  66. snowflake/cli/api/sql_execution.py +260 -0
  67. snowflake/cli/api/utils/__init__.py +13 -0
  68. snowflake/cli/api/utils/cursor.py +34 -0
  69. snowflake/cli/api/utils/definition_rendering.py +383 -0
  70. snowflake/cli/api/utils/dict_utils.py +73 -0
  71. snowflake/cli/api/utils/error_handling.py +23 -0
  72. snowflake/cli/api/utils/graph.py +97 -0
  73. snowflake/cli/api/utils/models.py +63 -0
  74. snowflake/cli/api/utils/naming_utils.py +13 -0
  75. snowflake/cli/api/utils/path_utils.py +36 -0
  76. snowflake/cli/api/utils/templating_functions.py +144 -0
  77. snowflake/cli/api/utils/types.py +35 -0
  78. snowflake/cli/app/__init__.py +22 -0
  79. snowflake/cli/app/__main__.py +31 -0
  80. snowflake/cli/app/api_impl/__init__.py +13 -0
  81. snowflake/cli/app/api_impl/plugin/__init__.py +13 -0
  82. snowflake/cli/app/api_impl/plugin/plugin_config_provider_impl.py +66 -0
  83. snowflake/cli/app/build_and_push.sh +8 -0
  84. snowflake/cli/app/cli_app.py +243 -0
  85. snowflake/cli/app/commands_registration/__init__.py +33 -0
  86. snowflake/cli/app/commands_registration/builtin_plugins.py +54 -0
  87. snowflake/cli/app/commands_registration/command_plugins_loader.py +169 -0
  88. snowflake/cli/app/commands_registration/commands_registration_with_callbacks.py +105 -0
  89. snowflake/cli/app/commands_registration/exception_logging.py +26 -0
  90. snowflake/cli/app/commands_registration/threadsafe.py +48 -0
  91. snowflake/cli/app/commands_registration/typer_registration.py +153 -0
  92. snowflake/cli/app/constants.py +19 -0
  93. snowflake/cli/app/dev/__init__.py +13 -0
  94. snowflake/cli/app/dev/commands_structure.py +48 -0
  95. snowflake/cli/app/dev/docs/__init__.py +13 -0
  96. snowflake/cli/app/dev/docs/commands_docs_generator.py +100 -0
  97. snowflake/cli/app/dev/docs/generator.py +35 -0
  98. snowflake/cli/app/dev/docs/project_definition_docs_generator.py +58 -0
  99. snowflake/cli/app/dev/docs/project_definition_generate_json_schema.py +227 -0
  100. snowflake/cli/app/dev/docs/template_utils.py +23 -0
  101. snowflake/cli/app/dev/docs/templates/definition_description.rst.jinja2 +38 -0
  102. snowflake/cli/app/dev/docs/templates/overview.rst.jinja2 +9 -0
  103. snowflake/cli/app/dev/docs/templates/usage.rst.jinja2 +57 -0
  104. snowflake/cli/app/dev/pycharm_remote_debug.py +46 -0
  105. snowflake/cli/app/loggers.py +199 -0
  106. snowflake/cli/app/main_typer.py +62 -0
  107. snowflake/cli/app/printing.py +181 -0
  108. snowflake/cli/app/snow_connector.py +243 -0
  109. snowflake/cli/app/telemetry.py +189 -0
  110. snowflake/cli/plugins/__init__.py +13 -0
  111. snowflake/cli/plugins/connection/__init__.py +13 -0
  112. snowflake/cli/plugins/connection/commands.py +330 -0
  113. snowflake/cli/plugins/connection/plugin_spec.py +30 -0
  114. snowflake/cli/plugins/connection/util.py +179 -0
  115. snowflake/cli/plugins/cortex/__init__.py +13 -0
  116. snowflake/cli/plugins/cortex/commands.py +327 -0
  117. snowflake/cli/plugins/cortex/constants.py +17 -0
  118. snowflake/cli/plugins/cortex/manager.py +189 -0
  119. snowflake/cli/plugins/cortex/plugin_spec.py +30 -0
  120. snowflake/cli/plugins/cortex/types.py +22 -0
  121. snowflake/cli/plugins/git/__init__.py +13 -0
  122. snowflake/cli/plugins/git/commands.py +354 -0
  123. snowflake/cli/plugins/git/manager.py +105 -0
  124. snowflake/cli/plugins/git/plugin_spec.py +30 -0
  125. snowflake/cli/plugins/init/__init__.py +13 -0
  126. snowflake/cli/plugins/init/commands.py +248 -0
  127. snowflake/cli/plugins/init/plugin_spec.py +30 -0
  128. snowflake/cli/plugins/nativeapp/__init__.py +13 -0
  129. snowflake/cli/plugins/nativeapp/artifacts.py +742 -0
  130. snowflake/cli/plugins/nativeapp/codegen/__init__.py +13 -0
  131. snowflake/cli/plugins/nativeapp/codegen/artifact_processor.py +91 -0
  132. snowflake/cli/plugins/nativeapp/codegen/compiler.py +130 -0
  133. snowflake/cli/plugins/nativeapp/codegen/sandbox.py +306 -0
  134. snowflake/cli/plugins/nativeapp/codegen/setup/native_app_setup_processor.py +172 -0
  135. snowflake/cli/plugins/nativeapp/codegen/setup/setup_driver.py.source +56 -0
  136. snowflake/cli/plugins/nativeapp/codegen/snowpark/callback_source.py.jinja +181 -0
  137. snowflake/cli/plugins/nativeapp/codegen/snowpark/extension_function_utils.py +217 -0
  138. snowflake/cli/plugins/nativeapp/codegen/snowpark/models.py +61 -0
  139. snowflake/cli/plugins/nativeapp/codegen/snowpark/python_processor.py +528 -0
  140. snowflake/cli/plugins/nativeapp/commands.py +439 -0
  141. snowflake/cli/plugins/nativeapp/common_flags.py +44 -0
  142. snowflake/cli/plugins/nativeapp/constants.py +27 -0
  143. snowflake/cli/plugins/nativeapp/exceptions.py +122 -0
  144. snowflake/cli/plugins/nativeapp/feature_flags.py +24 -0
  145. snowflake/cli/plugins/nativeapp/init.py +345 -0
  146. snowflake/cli/plugins/nativeapp/manager.py +823 -0
  147. snowflake/cli/plugins/nativeapp/plugin_spec.py +30 -0
  148. snowflake/cli/plugins/nativeapp/policy.py +50 -0
  149. snowflake/cli/plugins/nativeapp/project_model.py +195 -0
  150. snowflake/cli/plugins/nativeapp/run_processor.py +389 -0
  151. snowflake/cli/plugins/nativeapp/teardown_processor.py +301 -0
  152. snowflake/cli/plugins/nativeapp/utils.py +98 -0
  153. snowflake/cli/plugins/nativeapp/v2_conversions/v2_to_v1_decorator.py +135 -0
  154. snowflake/cli/plugins/nativeapp/version/__init__.py +13 -0
  155. snowflake/cli/plugins/nativeapp/version/commands.py +170 -0
  156. snowflake/cli/plugins/nativeapp/version/version_processor.py +362 -0
  157. snowflake/cli/plugins/notebook/__init__.py +13 -0
  158. snowflake/cli/plugins/notebook/commands.py +85 -0
  159. snowflake/cli/plugins/notebook/exceptions.py +20 -0
  160. snowflake/cli/plugins/notebook/manager.py +71 -0
  161. snowflake/cli/plugins/notebook/plugin_spec.py +30 -0
  162. snowflake/cli/plugins/notebook/types.py +15 -0
  163. snowflake/cli/plugins/object/__init__.py +13 -0
  164. snowflake/cli/plugins/object/command_aliases.py +95 -0
  165. snowflake/cli/plugins/object/commands.py +181 -0
  166. snowflake/cli/plugins/object/common.py +85 -0
  167. snowflake/cli/plugins/object/manager.py +97 -0
  168. snowflake/cli/plugins/object/plugin_spec.py +30 -0
  169. snowflake/cli/plugins/object_stage_deprecated/__init__.py +15 -0
  170. snowflake/cli/plugins/object_stage_deprecated/commands.py +122 -0
  171. snowflake/cli/plugins/object_stage_deprecated/plugin_spec.py +32 -0
  172. snowflake/cli/plugins/snowpark/__init__.py +13 -0
  173. snowflake/cli/plugins/snowpark/commands.py +546 -0
  174. snowflake/cli/plugins/snowpark/common.py +307 -0
  175. snowflake/cli/plugins/snowpark/manager.py +109 -0
  176. snowflake/cli/plugins/snowpark/models.py +157 -0
  177. snowflake/cli/plugins/snowpark/package/__init__.py +13 -0
  178. snowflake/cli/plugins/snowpark/package/anaconda_packages.py +233 -0
  179. snowflake/cli/plugins/snowpark/package/commands.py +256 -0
  180. snowflake/cli/plugins/snowpark/package/manager.py +44 -0
  181. snowflake/cli/plugins/snowpark/package/utils.py +26 -0
  182. snowflake/cli/plugins/snowpark/package_utils.py +354 -0
  183. snowflake/cli/plugins/snowpark/plugin_spec.py +30 -0
  184. snowflake/cli/plugins/snowpark/snowpark_package_paths.py +65 -0
  185. snowflake/cli/plugins/snowpark/snowpark_shared.py +95 -0
  186. snowflake/cli/plugins/snowpark/zipper.py +81 -0
  187. snowflake/cli/plugins/spcs/__init__.py +35 -0
  188. snowflake/cli/plugins/spcs/common.py +99 -0
  189. snowflake/cli/plugins/spcs/compute_pool/__init__.py +13 -0
  190. snowflake/cli/plugins/spcs/compute_pool/commands.py +241 -0
  191. snowflake/cli/plugins/spcs/compute_pool/manager.py +121 -0
  192. snowflake/cli/plugins/spcs/image_registry/__init__.py +13 -0
  193. snowflake/cli/plugins/spcs/image_registry/commands.py +65 -0
  194. snowflake/cli/plugins/spcs/image_registry/manager.py +105 -0
  195. snowflake/cli/plugins/spcs/image_repository/__init__.py +13 -0
  196. snowflake/cli/plugins/spcs/image_repository/commands.py +202 -0
  197. snowflake/cli/plugins/spcs/image_repository/manager.py +84 -0
  198. snowflake/cli/plugins/spcs/jobs/__init__.py +13 -0
  199. snowflake/cli/plugins/spcs/jobs/commands.py +78 -0
  200. snowflake/cli/plugins/spcs/jobs/manager.py +53 -0
  201. snowflake/cli/plugins/spcs/plugin_spec.py +30 -0
  202. snowflake/cli/plugins/spcs/services/__init__.py +13 -0
  203. snowflake/cli/plugins/spcs/services/commands.py +312 -0
  204. snowflake/cli/plugins/spcs/services/manager.py +170 -0
  205. snowflake/cli/plugins/sql/__init__.py +13 -0
  206. snowflake/cli/plugins/sql/commands.py +83 -0
  207. snowflake/cli/plugins/sql/manager.py +92 -0
  208. snowflake/cli/plugins/sql/plugin_spec.py +30 -0
  209. snowflake/cli/plugins/sql/snowsql_templating.py +28 -0
  210. snowflake/cli/plugins/stage/__init__.py +13 -0
  211. snowflake/cli/plugins/stage/commands.py +263 -0
  212. snowflake/cli/plugins/stage/diff.py +326 -0
  213. snowflake/cli/plugins/stage/manager.py +577 -0
  214. snowflake/cli/plugins/stage/md5.py +160 -0
  215. snowflake/cli/plugins/stage/plugin_spec.py +30 -0
  216. snowflake/cli/plugins/streamlit/__init__.py +13 -0
  217. snowflake/cli/plugins/streamlit/commands.py +179 -0
  218. snowflake/cli/plugins/streamlit/manager.py +222 -0
  219. snowflake/cli/plugins/streamlit/plugin_spec.py +30 -0
  220. snowflake/cli/plugins/workspace/__init__.py +13 -0
  221. snowflake/cli/plugins/workspace/commands.py +35 -0
  222. snowflake/cli/plugins/workspace/plugin_spec.py +30 -0
  223. snowflake/cli/templates/default_snowpark/.gitignore +4 -0
  224. snowflake/cli/templates/default_snowpark/app/__init__.py +0 -0
  225. snowflake/cli/templates/default_snowpark/app/common.py +2 -0
  226. snowflake/cli/templates/default_snowpark/app/functions.py +15 -0
  227. snowflake/cli/templates/default_snowpark/app/procedures.py +22 -0
  228. snowflake/cli/templates/default_snowpark/requirements.txt +1 -0
  229. snowflake/cli/templates/default_snowpark/snowflake.yml +23 -0
  230. snowflake/cli/templates/default_streamlit/.gitignore +4 -0
  231. snowflake/cli/templates/default_streamlit/common/hello.py +2 -0
  232. snowflake/cli/templates/default_streamlit/environment.yml +6 -0
  233. snowflake/cli/templates/default_streamlit/pages/my_page.py +3 -0
  234. snowflake/cli/templates/default_streamlit/snowflake.yml +10 -0
  235. snowflake/cli/templates/default_streamlit/streamlit_app.py +4 -0
  236. snowflake_cli-2.8.2.dist-info/METADATA +325 -0
  237. snowflake_cli-2.8.2.dist-info/RECORD +240 -0
  238. snowflake_cli-2.8.2.dist-info/WHEEL +4 -0
  239. snowflake_cli-2.8.2.dist-info/entry_points.txt +2 -0
  240. snowflake_cli-2.8.2.dist-info/licenses/LICENSE +201 -0
@@ -0,0 +1,546 @@
1
+ # Copyright (c) 2024 Snowflake Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from __future__ import annotations
16
+
17
+ import logging
18
+ from enum import Enum
19
+ from typing import Dict, List, Optional, Set, Tuple
20
+
21
+ import typer
22
+ from click import ClickException
23
+ from snowflake.cli.api.cli_global_context import cli_context
24
+ from snowflake.cli.api.commands.decorators import (
25
+ with_project_definition,
26
+ )
27
+ from snowflake.cli.api.commands.flags import (
28
+ ReplaceOption,
29
+ deprecated_flag_callback_enum,
30
+ execution_identifier_argument,
31
+ identifier_argument,
32
+ like_option,
33
+ )
34
+ from snowflake.cli.api.commands.project_initialisation import add_init_command
35
+ from snowflake.cli.api.commands.snow_typer import SnowTyperFactory
36
+ from snowflake.cli.api.constants import (
37
+ DEFAULT_SIZE_LIMIT_MB,
38
+ DEPLOYMENT_STAGE,
39
+ ObjectType,
40
+ )
41
+ from snowflake.cli.api.exceptions import SecretsWithoutExternalAccessIntegrationError
42
+ from snowflake.cli.api.identifiers import FQN
43
+ from snowflake.cli.api.output.types import (
44
+ CollectionResult,
45
+ CommandResult,
46
+ MessageResult,
47
+ SingleQueryResult,
48
+ )
49
+ from snowflake.cli.api.project.project_verification import assert_project_type
50
+ from snowflake.cli.api.project.schemas.snowpark.callable import (
51
+ FunctionSchema,
52
+ ProcedureSchema,
53
+ )
54
+ from snowflake.cli.api.secure_path import SecurePath
55
+ from snowflake.cli.plugins.object.commands import (
56
+ describe as object_describe,
57
+ )
58
+ from snowflake.cli.plugins.object.commands import (
59
+ drop as object_drop,
60
+ )
61
+ from snowflake.cli.plugins.object.commands import (
62
+ list_ as object_list,
63
+ )
64
+ from snowflake.cli.plugins.object.commands import (
65
+ scope_option,
66
+ )
67
+ from snowflake.cli.plugins.object.manager import ObjectManager
68
+ from snowflake.cli.plugins.snowpark import package_utils
69
+ from snowflake.cli.plugins.snowpark.common import (
70
+ FunctionOrProcedure,
71
+ UdfSprocIdentifier,
72
+ check_if_replace_is_required,
73
+ )
74
+ from snowflake.cli.plugins.snowpark.manager import FunctionManager, ProcedureManager
75
+ from snowflake.cli.plugins.snowpark.models import YesNoAsk
76
+ from snowflake.cli.plugins.snowpark.package.anaconda_packages import (
77
+ AnacondaPackages,
78
+ AnacondaPackagesManager,
79
+ )
80
+ from snowflake.cli.plugins.snowpark.package.commands import app as package_app
81
+ from snowflake.cli.plugins.snowpark.snowpark_package_paths import SnowparkPackagePaths
82
+ from snowflake.cli.plugins.snowpark.snowpark_shared import (
83
+ AllowSharedLibrariesOption,
84
+ DeprecatedCheckAnacondaForPyPiDependencies,
85
+ IgnoreAnacondaOption,
86
+ IndexUrlOption,
87
+ SkipVersionCheckOption,
88
+ deprecated_allow_native_libraries_option,
89
+ resolve_allow_shared_libraries_yes_no_ask,
90
+ )
91
+ from snowflake.cli.plugins.snowpark.zipper import zip_dir
92
+ from snowflake.cli.plugins.stage.manager import StageManager
93
+ from snowflake.connector import DictCursor, ProgrammingError
94
+
95
+ log = logging.getLogger(__name__)
96
+
97
+ app = SnowTyperFactory(
98
+ name="snowpark",
99
+ help="Manages procedures and functions.",
100
+ )
101
+ app.add_typer(package_app)
102
+
103
+ ObjectTypeArgument = typer.Argument(
104
+ help="Type of Snowpark object",
105
+ case_sensitive=False,
106
+ show_default=False,
107
+ )
108
+ IdentifierArgument = identifier_argument(
109
+ "function/procedure",
110
+ example="hello(int, string)",
111
+ )
112
+ LikeOption = like_option(
113
+ help_example='`list function --like "my%"` lists all functions that begin with “my”',
114
+ )
115
+ add_init_command(app, project_type="Snowpark", template="default_snowpark")
116
+
117
+
118
+ @app.command("deploy", requires_connection=True)
119
+ @with_project_definition()
120
+ def deploy(
121
+ replace: bool = ReplaceOption(
122
+ help="Replaces procedure or function, even if no detected changes to metadata"
123
+ ),
124
+ **options,
125
+ ) -> CommandResult:
126
+ """
127
+ Deploys procedures and functions defined in project. Deploying the project alters all objects defined in it.
128
+ By default, if any of the objects exist already the commands will fail unless `--replace` flag is provided.
129
+ All deployed objects use the same artifact which is deployed only once.
130
+ """
131
+
132
+ assert_project_type("snowpark")
133
+
134
+ snowpark = cli_context.project_definition.snowpark
135
+ paths = SnowparkPackagePaths.for_snowpark_project(
136
+ project_root=SecurePath(cli_context.project_root),
137
+ snowpark_project_definition=snowpark,
138
+ )
139
+
140
+ procedures = snowpark.procedures
141
+ functions = snowpark.functions
142
+
143
+ if not procedures and not functions:
144
+ raise ClickException(
145
+ "No procedures or functions were specified in the project definition."
146
+ )
147
+
148
+ if not paths.artifact_file.exists():
149
+ raise ClickException(
150
+ "Artifact required for deploying the project does not exist in this directory. "
151
+ "Please use build command to create it."
152
+ )
153
+
154
+ pm = ProcedureManager()
155
+ fm = FunctionManager()
156
+ om = ObjectManager()
157
+
158
+ _assert_object_definitions_are_correct("function", functions)
159
+ _assert_object_definitions_are_correct("procedure", procedures)
160
+ _check_if_all_defined_integrations_exists(om, functions, procedures)
161
+
162
+ existing_functions = _find_existing_objects(ObjectType.FUNCTION, functions, om)
163
+ existing_procedures = _find_existing_objects(ObjectType.PROCEDURE, procedures, om)
164
+
165
+ if (existing_functions or existing_procedures) and not replace:
166
+ msg = "Following objects already exists. Consider using --replace.\n"
167
+ msg += "\n".join(f"function: {n}" for n in existing_functions)
168
+ msg += "\n" if existing_functions and existing_procedures else ""
169
+ msg += "\n".join(f"procedure: {n}" for n in existing_procedures)
170
+ raise ClickException(msg)
171
+
172
+ # Create stage
173
+ stage_name = snowpark.stage_name
174
+ stage_manager = StageManager()
175
+ stage_name = FQN.from_string(stage_name).using_context()
176
+ stage_manager.create(fqn=stage_name, comment="deployments managed by Snowflake CLI")
177
+
178
+ snowflake_dependencies = _read_snowflake_requrements_file(
179
+ paths.snowflake_requirements_file
180
+ )
181
+
182
+ artifact_stage_directory = get_app_stage_path(stage_name, snowpark.project_name)
183
+ artifact_stage_target = (
184
+ f"{artifact_stage_directory}/{paths.artifact_file.path.name}"
185
+ )
186
+
187
+ stage_manager.put(
188
+ local_path=paths.artifact_file.path,
189
+ stage_path=artifact_stage_directory,
190
+ overwrite=True,
191
+ )
192
+
193
+ deploy_status = []
194
+ # Procedures
195
+ for procedure in procedures:
196
+ operation_result = _deploy_single_object(
197
+ manager=pm,
198
+ object_type=ObjectType.PROCEDURE,
199
+ object_definition=procedure,
200
+ existing_objects=existing_procedures,
201
+ snowflake_dependencies=snowflake_dependencies,
202
+ stage_artifact_path=artifact_stage_target,
203
+ )
204
+ deploy_status.append(operation_result)
205
+
206
+ # Functions
207
+ for function in functions:
208
+ operation_result = _deploy_single_object(
209
+ manager=fm,
210
+ object_type=ObjectType.FUNCTION,
211
+ object_definition=function,
212
+ existing_objects=existing_functions,
213
+ snowflake_dependencies=snowflake_dependencies,
214
+ stage_artifact_path=artifact_stage_target,
215
+ )
216
+ deploy_status.append(operation_result)
217
+
218
+ return CollectionResult(deploy_status)
219
+
220
+
221
+ def _assert_object_definitions_are_correct(
222
+ object_type, object_definitions: List[FunctionOrProcedure]
223
+ ):
224
+ for definition in object_definitions:
225
+ database = definition.database
226
+ schema = definition.schema_name
227
+ name = definition.name
228
+ fqn_parts = len(name.split("."))
229
+ if fqn_parts == 3 and database:
230
+ raise ClickException(
231
+ f"database of {object_type} {name} is redefined in its name"
232
+ )
233
+ if fqn_parts >= 2 and schema:
234
+ raise ClickException(
235
+ f"schema of {object_type} {name} is redefined in its name"
236
+ )
237
+
238
+
239
+ def _find_existing_objects(
240
+ object_type: ObjectType,
241
+ objects: List[FunctionOrProcedure],
242
+ om: ObjectManager,
243
+ ):
244
+ existing_objects = {}
245
+ for object_definition in objects:
246
+ identifier = UdfSprocIdentifier.from_definition(
247
+ object_definition
248
+ ).identifier_with_arg_types
249
+ try:
250
+ current_state = om.describe(
251
+ object_type=object_type.value.sf_name,
252
+ fqn=FQN.from_string(identifier),
253
+ )
254
+ existing_objects[identifier] = current_state
255
+ except ProgrammingError:
256
+ pass
257
+ return existing_objects
258
+
259
+
260
+ def _check_if_all_defined_integrations_exists(
261
+ om: ObjectManager,
262
+ functions: List[FunctionSchema],
263
+ procedures: List[ProcedureSchema],
264
+ ):
265
+ existing_integrations = {
266
+ i["name"].lower()
267
+ for i in om.show(object_type="integration", cursor_class=DictCursor, like=None)
268
+ if i["type"] == "EXTERNAL_ACCESS"
269
+ }
270
+ declared_integration: Set[str] = set()
271
+ for object_definition in [*functions, *procedures]:
272
+ external_access_integrations = {
273
+ s.lower() for s in object_definition.external_access_integrations
274
+ }
275
+ secrets = [s.lower() for s in object_definition.secrets]
276
+
277
+ if not external_access_integrations and secrets:
278
+ raise SecretsWithoutExternalAccessIntegrationError(object_definition.name)
279
+
280
+ declared_integration = declared_integration | external_access_integrations
281
+
282
+ missing = declared_integration - existing_integrations
283
+ if missing:
284
+ raise ClickException(
285
+ f"Following external access integration does not exists in Snowflake: {', '.join(missing)}"
286
+ )
287
+
288
+
289
+ def get_app_stage_path(stage_name: Optional[str], project_name: str) -> str:
290
+ artifact_stage_directory = f"@{(stage_name or DEPLOYMENT_STAGE)}/{project_name}"
291
+ return artifact_stage_directory
292
+
293
+
294
+ def _deploy_single_object(
295
+ manager: FunctionManager | ProcedureManager,
296
+ object_type: ObjectType,
297
+ object_definition: FunctionOrProcedure,
298
+ existing_objects: Dict[str, Dict],
299
+ snowflake_dependencies: List[str],
300
+ stage_artifact_path: str,
301
+ ):
302
+
303
+ identifiers = UdfSprocIdentifier.from_definition(object_definition)
304
+
305
+ log.info(
306
+ "Deploying %s: %s", object_type, identifiers.identifier_with_arg_names_types
307
+ )
308
+
309
+ handler = object_definition.handler
310
+ returns = object_definition.returns
311
+ imports = object_definition.imports
312
+ external_access_integrations = object_definition.external_access_integrations
313
+ runtime_ver = object_definition.runtime
314
+ execute_as_caller = None
315
+ if object_type == ObjectType.PROCEDURE:
316
+ execute_as_caller = object_definition.execute_as_caller
317
+ replace_object = False
318
+
319
+ object_exists = identifiers.identifier_with_arg_types in existing_objects
320
+ if object_exists:
321
+ replace_object = check_if_replace_is_required(
322
+ object_type=object_type,
323
+ current_state=existing_objects[identifiers.identifier_with_arg_types],
324
+ handler=handler,
325
+ return_type=returns,
326
+ snowflake_dependencies=snowflake_dependencies,
327
+ external_access_integrations=external_access_integrations,
328
+ imports=imports,
329
+ stage_artifact_file=stage_artifact_path,
330
+ runtime_ver=runtime_ver,
331
+ execute_as_caller=execute_as_caller,
332
+ )
333
+
334
+ if object_exists and not replace_object:
335
+ return {
336
+ "object": identifiers.identifier_with_arg_names_types_defaults,
337
+ "type": str(object_type),
338
+ "status": "packages updated",
339
+ }
340
+
341
+ create_or_replace_kwargs = {
342
+ "identifier": identifiers,
343
+ "handler": handler,
344
+ "return_type": returns,
345
+ "artifact_file": stage_artifact_path,
346
+ "packages": snowflake_dependencies,
347
+ "runtime": object_definition.runtime,
348
+ "external_access_integrations": object_definition.external_access_integrations,
349
+ "secrets": object_definition.secrets,
350
+ "imports": imports,
351
+ }
352
+ if object_type == ObjectType.PROCEDURE:
353
+ create_or_replace_kwargs[
354
+ "execute_as_caller"
355
+ ] = object_definition.execute_as_caller
356
+ manager.create_or_replace(**create_or_replace_kwargs)
357
+
358
+ status = "created" if not object_exists else "definition updated"
359
+ return {
360
+ "object": identifiers.identifier_with_arg_names_types_defaults,
361
+ "type": str(object_type),
362
+ "status": status,
363
+ }
364
+
365
+
366
+ deprecated_pypi_download_option = typer.Option(
367
+ YesNoAsk.NO.value,
368
+ "--pypi-download",
369
+ help="Whether to download non-Anaconda packages from PyPi.",
370
+ hidden=True,
371
+ callback=deprecated_flag_callback_enum(
372
+ "--pypi-download flag is deprecated. Snowpark build command"
373
+ " always tries to download non-Anaconda packages from external index (PyPi by default)."
374
+ ),
375
+ )
376
+
377
+
378
+ def _read_snowflake_requrements_file(file_path: SecurePath):
379
+ if not file_path.exists():
380
+ return []
381
+ return file_path.read_text(file_size_limit_mb=DEFAULT_SIZE_LIMIT_MB).splitlines()
382
+
383
+
384
+ @app.command("build", requires_connection=True)
385
+ @with_project_definition()
386
+ def build(
387
+ ignore_anaconda: bool = IgnoreAnacondaOption,
388
+ allow_shared_libraries: bool = AllowSharedLibrariesOption,
389
+ index_url: Optional[str] = IndexUrlOption,
390
+ skip_version_check: bool = SkipVersionCheckOption,
391
+ deprecated_package_native_libraries: YesNoAsk = deprecated_allow_native_libraries_option(
392
+ "--package-native-libraries"
393
+ ),
394
+ deprecated_check_anaconda_for_pypi_deps: bool = DeprecatedCheckAnacondaForPyPiDependencies,
395
+ _deprecated_pypi_download: YesNoAsk = deprecated_pypi_download_option,
396
+ **options,
397
+ ) -> CommandResult:
398
+ """
399
+ Builds the Snowpark project as a `.zip` archive that can be used by `deploy` command.
400
+ The archive is built using only the `src` directory specified in the project file.
401
+ """
402
+
403
+ assert_project_type("snowpark")
404
+
405
+ if not deprecated_check_anaconda_for_pypi_deps:
406
+ ignore_anaconda = True
407
+ snowpark_paths = SnowparkPackagePaths.for_snowpark_project(
408
+ project_root=SecurePath(cli_context.project_root),
409
+ snowpark_project_definition=cli_context.project_definition.snowpark,
410
+ )
411
+ log.info("Building package using sources from: %s", snowpark_paths.source.path)
412
+
413
+ anaconda_packages_manager = AnacondaPackagesManager()
414
+
415
+ with SecurePath.temporary_directory() as packages_dir:
416
+ if snowpark_paths.defined_requirements_file.exists():
417
+ log.info("Resolving any requirements from requirements.txt...")
418
+ requirements = package_utils.parse_requirements(
419
+ requirements_file=snowpark_paths.defined_requirements_file,
420
+ )
421
+ anaconda_packages = (
422
+ AnacondaPackages.empty()
423
+ if ignore_anaconda
424
+ else anaconda_packages_manager.find_packages_available_in_snowflake_anaconda()
425
+ )
426
+ download_result = package_utils.download_unavailable_packages(
427
+ requirements=requirements,
428
+ target_dir=packages_dir,
429
+ anaconda_packages=anaconda_packages,
430
+ skip_version_check=skip_version_check,
431
+ pip_index_url=index_url,
432
+ )
433
+ if not download_result.succeeded:
434
+ raise ClickException(download_result.error_message)
435
+
436
+ log.info("Checking to see if packages have shared (.so/.dll) libraries...")
437
+ if package_utils.detect_and_log_shared_libraries(
438
+ download_result.downloaded_packages_details
439
+ ):
440
+ # TODO: yes/no/ask logic should be removed in 3.0
441
+ if not (
442
+ allow_shared_libraries
443
+ or resolve_allow_shared_libraries_yes_no_ask(
444
+ deprecated_package_native_libraries
445
+ )
446
+ ):
447
+ raise ClickException(
448
+ "Some packages contain shared (.so/.dll) libraries. "
449
+ "Try again with --allow-shared-libraries."
450
+ )
451
+ if download_result.anaconda_packages:
452
+ anaconda_packages.write_requirements_file_in_snowflake_format( # type: ignore
453
+ file_path=snowpark_paths.snowflake_requirements_file,
454
+ requirements=download_result.anaconda_packages,
455
+ )
456
+
457
+ zip_dir(
458
+ source=snowpark_paths.source.path,
459
+ dest_zip=snowpark_paths.artifact_file.path,
460
+ )
461
+ if any(packages_dir.iterdir()):
462
+ # if any packages were generated, append them to the .zip
463
+ zip_dir(
464
+ source=packages_dir.path,
465
+ dest_zip=snowpark_paths.artifact_file.path,
466
+ mode="a",
467
+ )
468
+
469
+ log.info("Package now ready: %s", snowpark_paths.artifact_file.path)
470
+
471
+ return MessageResult(
472
+ f"Build done. Artifact path: {snowpark_paths.artifact_file.path}"
473
+ )
474
+
475
+
476
+ class _SnowparkObject(Enum):
477
+ """This clas is used only for Snowpark execute where choice is limited."""
478
+
479
+ PROCEDURE = str(ObjectType.PROCEDURE)
480
+ FUNCTION = str(ObjectType.FUNCTION)
481
+
482
+
483
+ def _execute_object_method(
484
+ method_name: str,
485
+ object_type: _SnowparkObject,
486
+ **kwargs,
487
+ ):
488
+ if object_type == _SnowparkObject.PROCEDURE:
489
+ manager = ProcedureManager()
490
+ elif object_type == _SnowparkObject.FUNCTION:
491
+ manager = FunctionManager()
492
+ else:
493
+ raise ClickException(f"Unknown object type: {object_type}")
494
+
495
+ return getattr(manager, method_name)(**kwargs)
496
+
497
+
498
+ @app.command("execute", requires_connection=True)
499
+ def execute(
500
+ object_type: _SnowparkObject = ObjectTypeArgument,
501
+ execution_identifier: str = execution_identifier_argument(
502
+ "procedure/function", "hello(1, 'world')"
503
+ ),
504
+ **options,
505
+ ) -> CommandResult:
506
+ """Executes a procedure or function in a specified environment."""
507
+ cursor = _execute_object_method(
508
+ "execute", object_type=object_type, execution_identifier=execution_identifier
509
+ )
510
+ return SingleQueryResult(cursor)
511
+
512
+
513
+ @app.command("list", requires_connection=True)
514
+ def list_(
515
+ object_type: _SnowparkObject = ObjectTypeArgument,
516
+ like: str = LikeOption,
517
+ scope: Tuple[str, str] = scope_option(
518
+ help_example="`list function --in database my_db`"
519
+ ),
520
+ **options,
521
+ ):
522
+ """Lists all available procedures or functions."""
523
+ return object_list(object_type=object_type.value, like=like, scope=scope, **options)
524
+
525
+
526
+ @app.command("drop", requires_connection=True)
527
+ def drop(
528
+ object_type: _SnowparkObject = ObjectTypeArgument,
529
+ identifier: FQN = IdentifierArgument,
530
+ **options,
531
+ ):
532
+ """Drop procedure or function."""
533
+ return object_drop(object_type=object_type.value, object_name=identifier, **options)
534
+
535
+
536
+ @app.command("describe", requires_connection=True)
537
+ def describe(
538
+ object_type: _SnowparkObject = ObjectTypeArgument,
539
+ identifier: FQN = IdentifierArgument,
540
+ **options,
541
+ ):
542
+ """Provides description of a procedure or function."""
543
+
544
+ return object_describe(
545
+ object_type=object_type.value, object_name=identifier, **options
546
+ )