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

fix: calc md5 for app cache

This commit is contained in:
18alantom 2024-05-06 12:23:18 +05:30
parent 1751b2db5b
commit 16f89067c6
2 changed files with 36 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import re
import shutil
import subprocess
import sys
import uuid
import tarfile
import typing
from collections import OrderedDict
@ -34,6 +35,7 @@ from bench.utils import (
is_valid_frappe_branch,
log,
run_frappe_cmd,
get_file_md5,
)
from bench.utils.bench import build_assets, install_python_dev_dependencies
from bench.utils.render import step
@ -338,6 +340,20 @@ class App(AppMeta):
def get_app_path(self) -> Path:
return Path(self.bench.name) / "apps" / self.app_name
def get_app_cache_temp_path(self, is_compressed=False) -> Path:
cache_path = get_bench_cache_path("apps")
ext = "tgz" if is_compressed else "tar"
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:
assert self.cache_key is not None
ext = temp_path.suffix[1:]
md5 = get_file_md5(temp_path.as_posix())
tarfile_name = f"{self.app_name}.{self.cache_key}.md5-{md5}.{ext}"
return temp_path.with_name(tarfile_name)
def get_app_cache_path(self, is_compressed=False) -> Path:
assert self.cache_key is not None
@ -392,7 +408,7 @@ class App(AppMeta):
return False
cwd = os.getcwd()
cache_path = self.get_app_cache_path(compress_artifacts)
cache_path = self.get_app_cache_temp_path(compress_artifacts)
mode = "w:gz" if compress_artifacts else "w"
message = f"Caching {self.app_name} app directory"
@ -407,9 +423,12 @@ class App(AppMeta):
try:
with tarfile.open(cache_path, mode) as tar:
tar.add(app_path.name)
hashed_path = self.get_app_cache_hashed_name(cache_path)
cache_path.rename(hashed_path)
success = True
except Exception:
log(f"Failed to cache {app_path}", level=3)
except Exception as exc:
log(f"Failed to cache {app_path} {exc}", level=3)
success = False
finally:
os.chdir(cwd)

View File

@ -5,6 +5,7 @@ import os
import re
import subprocess
import sys
import hashlib
from functools import lru_cache
from glob import glob
from pathlib import Path
@ -23,6 +24,12 @@ from bench.exceptions import (
InvalidRemoteException,
)
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Optional
logger = logging.getLogger(PROJECT_NAME)
paths_in_app = ("hooks.py", "modules.txt", "patches.txt")
paths_in_bench = ("apps", "sites", "config", "logs", "config/pids")
@ -605,3 +612,10 @@ def get_app_cache_extract_filter(
return None
return filter_function
def get_file_md5(p: str) -> "str":
with open(p, "rb") as f:
file_md5 = hashlib.md5()
while chunk := f.read(2**16):
file_md5.update(chunk)
return file_md5.hexdigest()