Skip to content

Commit

Permalink
Added full global support
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-astus committed Nov 12, 2024
1 parent 849677b commit 4e569bd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
7 changes: 4 additions & 3 deletions src/snowflake/cli/_plugins/snowpark/snowpark_entity_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from __future__ import annotations

import glob
from typing import List, Literal, Optional, Union

from pydantic import Field, field_validator
Expand Down Expand Up @@ -55,9 +56,9 @@ def _convert_artifacts(cls, artifacts: Union[dict, str]):
_artifacts = []
for artifact in artifacts:
if (
"*" in artifact
and FeatureFlag.ENABLE_SNOWPARK_BUNDLE_MAP_BUILD.is_disabled()
):
(isinstance(artifact, str) and glob.has_magic(artifact))
or (isinstance(artifact, PathMapping) and glob.has_magic(artifact.src))
) and FeatureFlag.ENABLE_SNOWPARK_BUNDLE_MAP_BUILD.is_disabled():
raise ValueError(
"If you want to use glob patterns in artifacts, you need to enable the Snowpark new build feature flag (ENABLE_SNOWPARK_BUNDLE_MAP_BUILD=true)"
)
Expand Down
41 changes: 33 additions & 8 deletions src/snowflake/cli/_plugins/snowpark/snowpark_project_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.
from __future__ import annotations

import glob
import os.path
import re
from dataclasses import dataclass
from pathlib import Path, PurePosixPath
Expand Down Expand Up @@ -92,9 +94,20 @@ def __init__(

@property
def _artefact_name(self) -> str:
if "*" in str(self.path):
before_wildcard = str(self.path).split("*")[0]
last_part = Path(before_wildcard).absolute().parts[-1]
if glob.has_magic(str(self.path)):
last_part = None
for part in self.path.parts:
if glob.has_magic(part):
break
else:
last_part = part
if not last_part:
last_part = os.path.commonpath(
[str(self.path), str(self.path.absolute())]
)
raise ValueError("Doopa")
# before_wildcard = str(self.path).split("*")[0]
# last_part = Path(before_wildcard).absolute().parts[-1]
return last_part + ".zip"
if (self.project_root / self.path).is_dir():
return self.path.stem + ".zip"
Expand All @@ -109,7 +122,9 @@ def post_build_path(self) -> Path:
"""
deploy_root = self.deploy_root()
path = (
self._path_until_asterisk() if "*" in str(self.path) else self.path.parent
self._path_until_asterisk()
if glob.has_magic(str(self.path))
else self.path.parent
)
if self._is_dest_a_file():
return deploy_root / self.dest # type: ignore
Expand All @@ -131,7 +146,7 @@ def upload_path(self, stage: FQN | str | None) -> str:
else:
stage_path /= (
self._path_until_asterisk()
if "*" in str(self.path)
if glob.has_magic(str(self.path))
else PurePosixPath(self.path).parent
)

Expand All @@ -150,9 +165,19 @@ def _is_dest_a_file(self) -> bool:
return re.search(r"\.[a-zA-Z0-9]{2,4}$", self.dest) is not None

def _path_until_asterisk(self) -> Path:
before_wildcard = str(self.path).split("*")[0]
parts = Path(before_wildcard).parts[:-1]
return Path(*parts)
# before_wildcard = str(self.path).split("*")[0]
# parts = Path(before_wildcard).parts[:-1]
# return Path(*parts)

path = []
for part in self.path.parts:
if glob.has_magic(part):
break
else:
path.append(part)
# before_wildcard = str(self.path).split("*")[0]
# parts = Path(before_wildcard).parts[:-1]
return Path(*path[:-1])

# Can be removed after removing ENABLE_SNOWPARK_BUNDLE_MAP_BUILD feature flag.
def build(self) -> None:
Expand Down
18 changes: 12 additions & 6 deletions tests/snowpark/test_project_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
("src/*", "source/", False, "@db.public.stage/source/src.zip"),
("src/**/*.py", None, False, "@db.public.stage/src.zip"),
("src/**/*.py", "source/", False, "@db.public.stage/source/src.zip"),
("src/app*", None, False, "@db.public.stage/src/app.zip"),
("src/app*", None, False, "@db.public.stage/src.zip"),
("src/app[1-5].py", None, False, "@db.public.stage/src.zip"),
],
)
@mock.patch("snowflake.cli.api.cli_global_context.get_cli_context")
Expand Down Expand Up @@ -48,7 +49,8 @@ def test_artifact_import_path(mock_ctx_context, path, dest, is_file, expected_pa
("src/*", "source/", False, "@db.public.stage/source/"),
("src/**/*.py", None, False, "@db.public.stage/"),
("src/**/*.py", "source/", False, "@db.public.stage/source/"),
("src/app*", None, False, "@db.public.stage/src/"),
("src/app*", None, False, "@db.public.stage/"),
("src/app[1-5].py", None, False, "@db.public.stage/"),
],
)
@mock.patch("snowflake.cli.api.cli_global_context.get_cli_context")
Expand Down Expand Up @@ -93,7 +95,8 @@ def test_artifact_upload_path(mock_ctx_context, path, dest, is_file, expected_pa
("src/*", "source/", False, Path("output") / "source" / "src.zip"),
("src/**/*.py", None, False, Path("output") / "src.zip"),
("src/**/*.py", "source/", False, Path("output") / "source" / "src.zip"),
("src/app*", None, False, Path("output") / "src" / "app.zip"),
("src/app*", None, False, Path("output") / "src.zip"),
("src/app[1-5].py", None, False, Path("output") / "src.zip"),
],
)
def test_artifact_post_build_path(path, dest, is_file, expected_path):
Expand All @@ -116,7 +119,8 @@ def test_artifact_post_build_path(path, dest, is_file, expected_path):
("src/*", "source/", False, "@db.public.stage/source/src.zip"),
("src/**/*.py", None, False, "@db.public.stage/src.zip"),
("src/**/*.py", "source/", False, "@db.public.stage/source/src.zip"),
("src/app*", None, False, "@db.public.stage/src/app.zip"),
("src/app*", None, False, "@db.public.stage/src.zip"),
("src/app[1-5].py", None, False, "@db.public.stage/src.zip"),
],
)
@mock.patch("snowflake.cli.api.cli_global_context.get_cli_context")
Expand Down Expand Up @@ -148,7 +152,8 @@ def test_artifact_import_path_from_other_directory(
("src/*", "source/", False, "@db.public.stage/source/"),
("src/**/*.py", None, False, "@db.public.stage/"),
("src/**/*.py", "source/", False, "@db.public.stage/source/"),
("src/app*", None, False, "@db.public.stage/src/"),
("src/app*", None, False, "@db.public.stage/"),
("src/app[1-5].py", None, False, "@db.public.stage/"),
],
)
@mock.patch("snowflake.cli.api.cli_global_context.get_cli_context")
Expand Down Expand Up @@ -209,7 +214,8 @@ def test_artifact_upload_path_from_other_directory(
False,
Path.cwd().absolute() / "output" / "source" / "src.zip",
),
("src/app*", None, False, Path.cwd().absolute() / "output" / "src" / "app.zip"),
("src/app*", None, False, Path.cwd().absolute() / "output" / "src.zip"),
("src/app[1-5].py", None, False, Path.cwd().absolute() / "output" / "src.zip"),
],
)
def test_artifact_post_build_path_from_other_directory(
Expand Down

0 comments on commit 4e569bd

Please sign in to comment.