2
0
mirror of https://github.com/frappe/bench.git synced 2024-06-15 22:52:22 +00:00

chore: rename commit-hash to cache-key

This commit is contained in:
18alantom 2024-01-19 19:18:13 +05:30
parent 42ac015bff
commit c5ec4f7528
4 changed files with 36 additions and 27 deletions

View File

@ -169,7 +169,7 @@ class App(AppMeta):
branch: str = None,
bench: "Bench" = None,
soft_link: bool = False,
commit_hash = None,
cache_key = None,
*args,
**kwargs,
):
@ -177,7 +177,7 @@ class App(AppMeta):
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")
@ -314,15 +314,15 @@ class App(AppMeta):
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()
@ -348,7 +348,7 @@ class App(AppMeta):
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()
@ -359,7 +359,7 @@ class App(AppMeta):
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)
@ -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),
@ -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

View File

@ -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,
@ -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"
@ -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,
)

View File

@ -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)

View File

@ -644,12 +644,12 @@ To switch to your required branch, run the following commands: bench switch-to-b
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:
@ -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
@ -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