Skip to content

Commit

Permalink
Added glob support for Snowpark and Streamlit
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-astus committed Nov 5, 2024
1 parent d9e46ce commit 4c3ee60
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 29 deletions.
2 changes: 0 additions & 2 deletions src/snowflake/cli/_plugins/snowpark/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ def build(
if FeatureFlag.ENABLE_SNOWPARK_NEW_BUILD.is_enabled():
for entity in get_snowpark_entities(pd).values():
for artifact in entity.artifacts:
# artifacts.add(Artefact(project_root=project_paths.project_root, path=Path(artifact.src), dest=artifact.dest if artifact.dest else None))
artifacts.add(project_paths.get_artefact_dto(artifact))

with cli_console.phase("Preparing artifacts for source code"):
Expand Down Expand Up @@ -418,7 +417,6 @@ def build(

with cli_console.phase("Preparing artifacts for source code"):
for artefact in artifacts:
# artefact_dto = project_paths.get_artefact_dto_old_build(artefact)
artefact.build()

return MessageResult(f"Build done.")
Expand Down
30 changes: 16 additions & 14 deletions src/snowflake/cli/_plugins/snowpark/snowpark_project_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def __init__(
self.project_root = project_root
self.path = path
self.dest = dest
if self.dest and not self.is_dest_a_file() and not self.dest.endswith("/"):
if self.dest and not self._is_dest_a_file() and not self.dest.endswith("/"):
self.dest = self.dest + "/"

@property
Expand All @@ -106,11 +106,10 @@ def _artefact_name(self) -> str:
before_wildcard = str(self.path).split("*")[0]
last_part = Path(before_wildcard).absolute().parts[-1]
return last_part + ".zip"
elif (self.project_root / self.path).is_dir():
if (self.project_root / self.path).is_dir():
return self.path.stem + ".zip"
elif (self.project_root / self.path).is_file():
if self.is_dest_a_file():
return Path(self.dest).name # type: ignore
if (self.project_root / self.path).is_file() and self._is_dest_a_file():
return Path(self.dest).name # type: ignore
return self.path.name

@property
Expand All @@ -120,6 +119,8 @@ def post_build_path(self) -> Path:
"""
deploy_root = self.deploy_root()
path = self._path_until_asterix() if "*" in str(self.path) else self.path.parent
if self._is_dest_a_file():
return deploy_root / self.dest # type: ignore
return deploy_root / (self.dest or path) / self._artefact_name

def upload_path(self, stage: FQN | str | None) -> str:
Expand All @@ -132,15 +133,16 @@ def upload_path(self, stage: FQN | str | None) -> str:

stage_path = PurePosixPath(f"@{stage}")
if self.dest:
if self.is_dest_a_file():
stage_path = stage_path / PurePosixPath(self.dest).parent
else:
stage_path = stage_path / self.dest
stage_path /= (
PurePosixPath(self.dest).parent if self._is_dest_a_file() else self.dest
)
else:
if "*" in str(self.path):
stage_path = stage_path / self._path_until_asterix()
else:
stage_path = stage_path / PurePosixPath(self.path).parent
stage_path /= (
self._path_until_asterix()
if "*" in str(self.path)
else PurePosixPath(self.path).parent
)

return str(stage_path) + "/"

def import_path(self, stage: FQN | str | None) -> str:
Expand All @@ -150,7 +152,7 @@ def import_path(self, stage: FQN | str | None) -> str:
def deploy_root(self) -> Path:
return self.project_root / "output"

def is_dest_a_file(self) -> bool:
def _is_dest_a_file(self) -> bool:
if not self.dest:
return False
return re.search(r"\.[a-zA-Z0-9]{2,4}$", self.dest) is not None
Expand Down
60 changes: 47 additions & 13 deletions tests/snowpark/test_project_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ def test_artifact_upload_path(mock_ctx_context, path, dest, is_file, expected_pa
("src/", None, False, Path("output") / "src.zip"),
("src", "source", False, Path("output") / "source" / "src.zip"),
("src/app.py", None, True, Path("output") / "src" / "app.py"),
# TODO add rename case later
# ("src/app.py", "source/new_app.py", True, "output/source/new_app.py"),
(
"src/app.py",
"source/new_app.py",
True,
Path("output") / "source" / "new_app.py",
),
("src/*", "source/new_app.py", True, Path("output") / "source" / "new_app.py"),
(
"src/dir/dir2/app.py",
None,
Expand Down Expand Up @@ -165,17 +170,46 @@ def test_artifact_upload_path_from_other_directory(
"path, dest, is_file, expected_path",
[
("src", None, False, Path.cwd().absolute() / "output" / "src.zip"),
# ("src/", None, False, "/tmp/output/src.zip"),
# ("src", "source", False, "/tmp/output/source/src.zip"),
# ("src/app.py", None, True, "/tmp/output/src/app.py"),
# # TODO add rename case later
# # ("src/app.py", "source/new_app.py", True, "/tmp/output/source/new_app.py"),
# ("src/dir/dir2/app.py", None, True, "/tmp/output/src/dir/dir2/app.py"),
# ("src/dir/dir2/app.py", "source/", True, "/tmp/output/source/app.py"),
# ("src/*", "source/", False, "/tmp/output/source/src.zip"),
# ("src/**/*.py", None, False, "/tmp/output/src.zip"),
# ("src/**/*.py", "source/", False, "/tmp/output/source/src.zip"),
# ("src/app*", None, False, "/tmp/output/src/app.zip"),
("src/", None, False, Path.cwd().absolute() / "output" / "src.zip"),
(
"src",
"source",
False,
Path.cwd().absolute() / "output" / "source" / "src.zip",
),
("src/app.py", None, True, Path.cwd().absolute() / "output" / "src" / "app.py"),
(
"src/app.py",
"source/new_app.py",
True,
Path.cwd().absolute() / "output" / "source" / "new_app.py",
),
(
"src/dir/dir2/app.py",
None,
True,
Path.cwd().absolute() / "output" / "src" / "dir" / "dir2" / "app.py",
),
(
"src/dir/dir2/app.py",
"source/",
True,
Path.cwd().absolute() / "output" / "source" / "app.py",
),
(
"src/*",
"source/",
False,
Path.cwd().absolute() / "output" / "source" / "src.zip",
),
("src/**/*.py", None, False, Path.cwd().absolute() / "output" / "src.zip"),
(
"src/**/*.py",
"source/",
False,
Path.cwd().absolute() / "output" / "source" / "src.zip",
),
("src/app*", None, False, Path.cwd().absolute() / "output" / "src" / "app.zip"),
],
)
def test_artifact_post_build_path_from_other_directory(
Expand Down

0 comments on commit 4c3ee60

Please sign in to comment.