Skip to content

Commit

Permalink
chore: rename commit-hash to cache-key
Browse files Browse the repository at this point in the history
  • Loading branch information
18alantom committed Jan 19, 2024
1 parent 42ac015 commit c5ec4f7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
18 changes: 9 additions & 9 deletions bench/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,15 @@ def __init__(
branch: str = None,
bench: "Bench" = None,
soft_link: bool = False,
commit_hash = None,
cache_key = None,
*args,
**kwargs,
):
self.bench = bench
self.soft_link = soft_link
self.required_by = None
self.local_resolution = []
self.commit_hash = commit_hash
self.cache_key = cache_key
super().__init__(name, branch, *args, **kwargs)

@step(title="Fetching App {repo}", success="App {repo} Fetched")
Expand Down Expand Up @@ -314,15 +314,15 @@ def get_app_path(self) -> Path:
return Path(self.bench.name) / "apps" / self.app_name

def get_app_cache_path(self, is_compressed=False) -> Path:
assert self.commit_hash is not None
assert self.cache_key is not None

cache_path = get_bench_cache_path("apps")
ext = "tgz" if is_compressed else "tar"
tarfile_name = f"{self.app_name}-{self.commit_hash[:10]}.{ext}"
tarfile_name = f"{self.app_name}-{self.cache_key[:10]}.{ext}"
return cache_path / tarfile_name

def get_cached(self) -> bool:
if not self.commit_hash:
if not self.cache_key:
return False

cache_path = self.get_app_cache_path()
Expand All @@ -348,7 +348,7 @@ def get_cached(self) -> bool:
return True

def set_cache(self, compress_artifacts=False) -> bool:
if not self.commit_hash:
if not self.cache_key:
return False

app_path = self.get_app_path()
Expand All @@ -359,7 +359,7 @@ def set_cache(self, compress_artifacts=False) -> bool:
cache_path = self.get_app_cache_path(compress_artifacts)
mode = "w:gz" if compress_artifacts else "w"

message = f"Caching ${self.app_name} app directory"
message = f"Caching {self.app_name} app directory"
if compress_artifacts:
message += " (compressed)"
click.secho(message)
Expand Down Expand Up @@ -481,7 +481,7 @@ def get_app(
soft_link=False,
init_bench=False,
resolve_deps=False,
commit_hash=None,
cache_key=None,
compress_artifacts=False,
):
"""bench get-app clones a Frappe App from remote (GitHub or any other git server),
Expand All @@ -497,7 +497,7 @@ def get_app(
from bench.utils.app import check_existing_dir

bench = Bench(bench_path)
app = App(git_url, branch=branch, bench=bench, soft_link=soft_link, commit_hash=commit_hash)
app = App(git_url, branch=branch, bench=bench, soft_link=soft_link, cache_key=cache_key)
git_url = app.url
repo_name = app.repo
branch = app.tag
Expand Down
20 changes: 14 additions & 6 deletions bench/commands/make.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,18 @@ def drop(path):
default=False,
help="Resolve dependencies before installing app",
)
@click.option("--commit-hash", default=None, help="Required for caching get-app artifacts.")
@click.option("--cache-artifacts", is_flag=True, default=False, help="Whether to cache get-app artifacts. Needs commit-hash.")
@click.option("--compress-artifacts", is_flag=True, default=False, help="Whether to gzip get-app artifacts that are to be cached.")
@click.option(
"--cache-key",
type=str,
default=None,
help="Caches get-app artifacts if provided (only first 10 chars is used)",
)
@click.option(
"--compress-artifacts",
is_flag=True,
default=False,
help="Whether to gzip get-app artifacts that are to be cached",
)
def get_app(
git_url,
branch,
Expand All @@ -163,8 +172,7 @@ def get_app(
soft_link=False,
init_bench=False,
resolve_deps=False,
commit_hash=None,
cache_artifacts=False,
cache_key=None,
compress_artifacts=False,
):
"clone an app from the internet and set it up in your bench"
Expand All @@ -178,7 +186,7 @@ def get_app(
soft_link=soft_link,
init_bench=init_bench,
resolve_deps=resolve_deps,
commit_hash=commit_hash if cache_artifacts else None,
cache_key=cache_key,
compress_artifacts=compress_artifacts,
)

Expand Down
8 changes: 4 additions & 4 deletions bench/commands/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ def migrate_env(python, backup=True):
help="Removes all items that match provided app name",
)
@click.option(
"--remove-hash",
"--remove-key",
default="",
help="Removes all items that matches provided commit-hash",
help="Removes all items that matches provided cache key",
)
def app_cache_helper(clear=False, remove_app="", remove_hash=""):
def app_cache_helper(clear=False, remove_app="", remove_key=""):
from bench.utils.bench import cache_helper

cache_helper(clear, remove_app, remove_hash)
cache_helper(clear, remove_app, remove_key)
17 changes: 9 additions & 8 deletions bench/utils/bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,12 +644,12 @@ def validate_branch():
sys.exit(1)


def cache_helper(clear=False, remove_app="", remove_hash="") -> None:
can_remove = bool(remove_hash or remove_app)
def cache_helper(clear=False, remove_app="", remove_key="") -> None:
can_remove = bool(remove_key or remove_app)
if not clear and not can_remove:
cache_list()
elif can_remove:
cache_remove(remove_app, remove_hash)
cache_remove(remove_app, remove_key)
elif clear:
cache_clear()
else:
Expand Down Expand Up @@ -696,17 +696,18 @@ def cache_list() -> None:
f"{created:%Y-%m-%d %H:%M:%S} "
f"{accessed:%Y-%m-%d %H:%M:%S} "
)

if tot_items:
click.echo(f"Total size {tot_size / 1_000_000:.3f} MB belonging to {tot_items} items")
else:
click.echo("No cached items")


def cache_remove(app: str = "", hash: str = "") -> None:
def cache_remove(app: str = "", key: str = "") -> None:
rem_items = 0
rem_size = 0
for item in get_bench_cache_path("apps").iterdir():
if not should_remove_item(item, app, hash):
if not should_remove_item(item, app, key):
continue

rem_items += 1
Expand All @@ -720,18 +721,18 @@ def cache_remove(app: str = "", hash: str = "") -> None:
click.echo("No items removed")


def should_remove_item(item: Path, app: str, hash: str) -> bool:
def should_remove_item(item: Path, app: str, key: str) -> bool:
if item.suffix not in [".tar", ".tgz"]:
return False

name = item.name
if app and hash and name.startswith(f"{app}-{hash[:10]}."):
if app and key and name.startswith(f"{app}-{key[:10]}."):
return True

if app and name.startswith(f"{app}-"):
return True

if hash and f"-{hash[:10]}." in name:
if key and f"-{key[:10]}." in name:
return True

return False
Expand Down

0 comments on commit c5ec4f7

Please sign in to comment.