Skip to content

Commit

Permalink
Refactor cleanup_after_install as a decorator (#885)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-turbaszek committed Mar 8, 2024
1 parent ed437e4 commit d53aacd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/snowflake/cli/plugins/snowpark/package/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@


@app.command("lookup", requires_connection=True)
@cleanup_after_install
def package_lookup(
name: str = typer.Argument(..., help="Name of the package."),
install_packages: bool = install_option,
Expand All @@ -55,7 +56,6 @@ def package_lookup(
install_packages = deprecated_install_option

lookup_result = lookup(name=name, install_packages=install_packages)
cleanup_after_install()
return MessageResult(lookup_result.message)


Expand Down Expand Up @@ -89,6 +89,7 @@ def package_upload(


@app.command("create", requires_connection=True)
@cleanup_after_install
def package_create(
name: str = typer.Argument(
...,
Expand Down Expand Up @@ -117,6 +118,4 @@ def package_create(
message += "\n" + lookup_result.message
else:
message = lookup_result.message

cleanup_after_install()
return MessageResult(message)
14 changes: 11 additions & 3 deletions src/snowflake/cli/plugins/snowpark/package/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging
import os.path
from functools import wraps
from pathlib import Path

from requirements.requirement import Requirement
Expand Down Expand Up @@ -71,6 +72,13 @@ def create(zip_name: str):
return CreatedSuccessfully(zip_name, Path(file_name))


def cleanup_after_install():
if PACKAGES_DIR.exists():
SecurePath(PACKAGES_DIR).rmdir(recursive=True)
def cleanup_after_install(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
finally:
if PACKAGES_DIR.exists():
SecurePath(PACKAGES_DIR).rmdir(recursive=True)

return wrapper

0 comments on commit d53aacd

Please sign in to comment.