2
0
mirror of https://github.com/frappe/bench.git synced 2025-01-09 08:30:39 +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, branch: str = None,
bench: "Bench" = None, bench: "Bench" = None,
soft_link: bool = False, soft_link: bool = False,
commit_hash = None, cache_key = None,
*args, *args,
**kwargs, **kwargs,
): ):
@ -177,7 +177,7 @@ class App(AppMeta):
self.soft_link = soft_link self.soft_link = soft_link
self.required_by = None self.required_by = None
self.local_resolution = [] self.local_resolution = []
self.commit_hash = commit_hash self.cache_key = cache_key
super().__init__(name, branch, *args, **kwargs) super().__init__(name, branch, *args, **kwargs)
@step(title="Fetching App {repo}", success="App {repo} Fetched") @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 return Path(self.bench.name) / "apps" / self.app_name
def get_app_cache_path(self, is_compressed=False) -> Path: 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") cache_path = get_bench_cache_path("apps")
ext = "tgz" if is_compressed else "tar" 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 return cache_path / tarfile_name
def get_cached(self) -> bool: def get_cached(self) -> bool:
if not self.commit_hash: if not self.cache_key:
return False return False
cache_path = self.get_app_cache_path() cache_path = self.get_app_cache_path()
@ -348,7 +348,7 @@ class App(AppMeta):
return True return True
def set_cache(self, compress_artifacts=False) -> bool: def set_cache(self, compress_artifacts=False) -> bool:
if not self.commit_hash: if not self.cache_key:
return False return False
app_path = self.get_app_path() app_path = self.get_app_path()
@ -359,7 +359,7 @@ class App(AppMeta):
cache_path = self.get_app_cache_path(compress_artifacts) cache_path = self.get_app_cache_path(compress_artifacts)
mode = "w:gz" if compress_artifacts else "w" 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: if compress_artifacts:
message += " (compressed)" message += " (compressed)"
click.secho(message) click.secho(message)
@ -481,7 +481,7 @@ def get_app(
soft_link=False, soft_link=False,
init_bench=False, init_bench=False,
resolve_deps=False, resolve_deps=False,
commit_hash=None, cache_key=None,
compress_artifacts=False, compress_artifacts=False,
): ):
"""bench get-app clones a Frappe App from remote (GitHub or any other git server), """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 from bench.utils.app import check_existing_dir
bench = Bench(bench_path) 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 git_url = app.url
repo_name = app.repo repo_name = app.repo
branch = app.tag branch = app.tag

View File

@ -151,9 +151,18 @@ def drop(path):
default=False, default=False,
help="Resolve dependencies before installing app", help="Resolve dependencies before installing app",
) )
@click.option("--commit-hash", default=None, help="Required for caching get-app artifacts.") @click.option(
@click.option("--cache-artifacts", is_flag=True, default=False, help="Whether to cache get-app artifacts. Needs commit-hash.") "--cache-key",
@click.option("--compress-artifacts", is_flag=True, default=False, help="Whether to gzip get-app artifacts that are to be cached.") 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( def get_app(
git_url, git_url,
branch, branch,
@ -163,8 +172,7 @@ def get_app(
soft_link=False, soft_link=False,
init_bench=False, init_bench=False,
resolve_deps=False, resolve_deps=False,
commit_hash=None, cache_key=None,
cache_artifacts=False,
compress_artifacts=False, compress_artifacts=False,
): ):
"clone an app from the internet and set it up in your bench" "clone an app from the internet and set it up in your bench"
@ -178,7 +186,7 @@ def get_app(
soft_link=soft_link, soft_link=soft_link,
init_bench=init_bench, init_bench=init_bench,
resolve_deps=resolve_deps, resolve_deps=resolve_deps,
commit_hash=commit_hash if cache_artifacts else None, cache_key=cache_key,
compress_artifacts=compress_artifacts, 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", help="Removes all items that match provided app name",
) )
@click.option( @click.option(
"--remove-hash", "--remove-key",
default="", 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 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) sys.exit(1)
def cache_helper(clear=False, remove_app="", remove_hash="") -> None: def cache_helper(clear=False, remove_app="", remove_key="") -> None:
can_remove = bool(remove_hash or remove_app) can_remove = bool(remove_key or remove_app)
if not clear and not can_remove: if not clear and not can_remove:
cache_list() cache_list()
elif can_remove: elif can_remove:
cache_remove(remove_app, remove_hash) cache_remove(remove_app, remove_key)
elif clear: elif clear:
cache_clear() cache_clear()
else: else:
@ -696,17 +696,18 @@ def cache_list() -> None:
f"{created:%Y-%m-%d %H:%M:%S} " f"{created:%Y-%m-%d %H:%M:%S} "
f"{accessed:%Y-%m-%d %H:%M:%S} " f"{accessed:%Y-%m-%d %H:%M:%S} "
) )
if tot_items: if tot_items:
click.echo(f"Total size {tot_size / 1_000_000:.3f} MB belonging to {tot_items} items") click.echo(f"Total size {tot_size / 1_000_000:.3f} MB belonging to {tot_items} items")
else: else:
click.echo("No cached items") 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_items = 0
rem_size = 0 rem_size = 0
for item in get_bench_cache_path("apps").iterdir(): 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 continue
rem_items += 1 rem_items += 1
@ -720,18 +721,18 @@ def cache_remove(app: str = "", hash: str = "") -> None:
click.echo("No items removed") 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"]: if item.suffix not in [".tar", ".tgz"]:
return False return False
name = item.name 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 return True
if app and name.startswith(f"{app}-"): if app and name.startswith(f"{app}-"):
return True return True
if hash and f"-{hash[:10]}." in name: if key and f"-{key[:10]}." in name:
return True return True
return False return False