Skip to content

Commit

Permalink
fix: update pre url coercion check
Browse files Browse the repository at this point in the history
  • Loading branch information
18alantom committed May 6, 2024
1 parent efb5171 commit 6f074a7
Showing 1 changed file with 44 additions and 58 deletions.
102 changes: 44 additions & 58 deletions bench/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def get_app_cache_temp_path(self, is_compressed=False) -> Path:
tarfile_name = f"{self.app_name}.{uuid.uuid4().hex}.{ext}"
return cache_path / tarfile_name

def get_app_cache_hashed_name(self, temp_path: Path) -> Path:
def get_app_cache_hashed_path(self, temp_path: Path) -> Path:
assert self.cache_key is not None

ext = temp_path.suffix[1:]
Expand All @@ -355,44 +355,11 @@ def get_app_cache_hashed_name(self, temp_path: Path) -> Path:

return temp_path.with_name(tarfile_name)

def get_app_cache_path(self) -> "Optional[Path]":
assert self.cache_key is not None

cache_path = get_bench_cache_path("apps")
glob_pattern = f"{self.app_name}.{self.cache_key}.md5-*"

for app_cache_path in cache_path.glob(glob_pattern):
return app_cache_path

return None

def validate_cache_and_get_path(self) -> "Optional[Path]":
def get_cached(self) -> bool:
if not self.cache_key:
return

if not (cache_path := self.get_app_cache_path()):
return

if not cache_path.is_file():
click.secho(
f"Bench app-cache: file check failed for {cache_path.as_posix()}, skipping cache",
fg="yellow",
)
unlink_no_throw(cache_path)
return

if not is_cache_hash_valid(cache_path):
click.secho(
f"Bench app-cache: hash validation failed for {cache_path.as_posix()}, skipping cache",
fg="yellow",
)
unlink_no_throw(cache_path)
return

return cache_path
return False

def get_cached(self) -> bool:
if not (cache_path := self.validate_cache_and_get_path()):
if not (cache_path := validate_cache_and_get_path(self.app_name, self.cache_key)):
return False

app_path = self.get_app_path()
Expand Down Expand Up @@ -443,7 +410,7 @@ def set_cache(self, compress_artifacts=False) -> bool:
with tarfile.open(cache_path, mode) as tar:
tar.add(app_path.name)

hashed_path = self.get_app_cache_hashed_name(cache_path)
hashed_path = self.get_app_cache_hashed_path(cache_path)
unlink_no_throw(hashed_path)

cache_path.rename(hashed_path)
Expand Down Expand Up @@ -478,28 +445,11 @@ def can_get_cached(app_name: str, cache_key: str) -> bool:
checking local remote and fetching can be skipped while keeping
get-app command params the same.
"""
cache_path = get_bench_cache_path("apps")
tarfile_path = cache_path / get_cache_filename(
app_name,
cache_key,
True,
)

if tarfile_path.is_file():
return True
if cache_path := get_app_cache_path(app_name, cache_key):
return cache_path.exists()

tarfile_path = cache_path / get_cache_filename(
app_name,
cache_key,
False,
)

return tarfile_path.is_file()


def get_cache_filename(app_name: str, cache_key: str, is_compressed=False):
ext = "tgz" if is_compressed else "tar"
return f"{app_name}-{cache_key[:10]}.{ext}"
return False


def can_frappe_use_cached(app: App) -> bool:
Expand Down Expand Up @@ -1100,3 +1050,39 @@ def unlink_no_throw(path: Path):
path.unlink(True)
except Exception:
pass


def get_app_cache_path(app_name: str, cache_key: str) -> "Optional[Path]":
cache_path = get_bench_cache_path("apps")
glob_pattern = f"{app_name}.{cache_key}.md5-*"

for app_cache_path in cache_path.glob(glob_pattern):
return app_cache_path

return None


def validate_cache_and_get_path(app_name: str, cache_key: str) -> "Optional[Path]":
if not cache_key:
return

if not (cache_path := get_app_cache_path(app_name, cache_key)):
return

if not cache_path.is_file():
click.secho(
f"Bench app-cache: file check failed for {cache_path.as_posix()}, skipping cache",
fg="yellow",
)
unlink_no_throw(cache_path)
return

if not is_cache_hash_valid(cache_path):
click.secho(
f"Bench app-cache: hash validation failed for {cache_path.as_posix()}, skipping cache",
fg="yellow",
)
unlink_no_throw(cache_path)
return

return cache_path

0 comments on commit 6f074a7

Please sign in to comment.