optimize cached file locations and add option for cache directory

This commit is contained in:
2022-11-03 13:17:49 +01:00
parent 7678967ae0
commit 639096b548
3 changed files with 30 additions and 18 deletions

View File

@@ -6,6 +6,7 @@ import urllib.parse
import xml.etree.ElementTree as ET
from urllib.error import HTTPError
from pathlib import Path
from lib.proxy_sha256 import md5
import osc.core
@@ -160,16 +161,22 @@ class OBS:
name: str,
revision: str,
dirpath: str,
cachedir: str,
file_md5: str,
) -> None:
cached_file = self._path_from_md5(name, dirpath, file_md5)
if not self.in_cache(name, dirpath, file_md5):
cached_file = self._path_from_md5(name, cachedir, file_md5)
if not self.in_cache(name, cachedir, file_md5):
with (dirpath / name).open("wb") as f:
f.write(self._download(project, package, name, revision).read())
shutil.copy(dirpath / name, cached_file)
else:
shutil.copy(cached_file, dirpath / name)
# Validate the MD5 of the downloaded file
if md5(dirpath / name) != file_md5:
raise Exception(f"Download error in {name}")
def list(self, project, package, srcmd5, linkrev):
params = {"rev": srcmd5, "expand": "1"}
if linkrev:
@@ -187,15 +194,11 @@ class OBS:
return root
def _path_from_md5(self, name, dirpath, md5):
cache = dirpath.joinpath(".cache/")
if not Path(cache).exists():
cache.mkdir()
filepath = cache.joinpath(f"{md5[0:3]}/{md5[3:6]}/{md5[6:9]}/")
def _path_from_md5(self, name, cachedir, md5):
filepath = cachedir / md5[:3]
cached_file = f"{md5[3:]}-{name}"
filepath.mkdir(parents=True, exist_ok=True)
return filepath.joinpath(f"{md5[9:]}-{name}")
return filepath / cached_file
def in_cache(self, name, dirpath, md5):
if self._path_from_md5(name, dirpath, md5).is_file():
return True
return False
def in_cache(self, name, cachedir, md5):
return self._path_from_md5(name, cachedir, md5).exists()