Simplify cache handling.

This commit is contained in:
Jelmer Vernooij 2021-03-16 20:07:42 +00:00
parent b6414238bf
commit b7206647b1

View file

@ -42,7 +42,7 @@ def read_contents_file(f):
yield path, rest yield path, rest
def contents_urls_from_sources_entry(source, arches): def contents_urls_from_sources_entry(source, arches, load_url):
if source.invalid or source.disabled: if source.invalid or source.disabled:
return return
if source.type == "deb-src": if source.type == "deb-src":
@ -59,28 +59,26 @@ def contents_urls_from_sources_entry(source, arches):
dists_url = base_url dists_url = base_url
if components: if components:
for component in components: for component in components:
for arch, mandatory in arches: for arch in arches:
yield ( yield (
"%s/%s/%s/Contents-%s" "%s/%s/%s/Contents-%s"
% (dists_url, name, component, arch), % (dists_url, name, component, arch)
mandatory,
) )
else: else:
for arch, mandatory in arches: for arch in arches:
yield ( yield (
"%s/%s/Contents-%s" % (dists_url, name.rstrip("/"), arch), "%s/%s/Contents-%s" % (dists_url, name.rstrip("/"), arch)
mandatory,
) )
def contents_urls_from_sourceslist(sl, arch): def contents_urls_from_sourceslist(sl, arch, load_url):
# TODO(jelmer): Verify signatures, etc. # TODO(jelmer): Verify signatures, etc.
arches = [(arch, True), ("all", False)] arches = [arch, "all"]
for source in sl.list: for source in sl.list:
yield from contents_urls_from_sources_entry(source, arches) yield from contents_urls_from_sources_entry(source, arches, load_url)
def load_contents_url(url): def load_direct_url(url):
from urllib.error import HTTPError from urllib.error import HTTPError
from urllib.request import urlopen, Request from urllib.request import urlopen, Request
@ -114,11 +112,20 @@ def load_contents_url(url):
return f return f
def load_apt_cache_file(cache_dir, url): def load_url_with_cache(url, cache_dirs):
for cache_dir in cache_dirs:
try:
return load_apt_cache_file(url, cache_dir)
except FileNotFoundError:
pass
return load_direct_url(url)
def load_apt_cache_file(url, cache_dir):
fn = apt_pkg.uri_to_filename(url) fn = apt_pkg.uri_to_filename(url)
p = os.path.join(cache_dir, fn + ".lz4") p = os.path.join(cache_dir, fn + ".lz4")
if not os.path.exists(p): if not os.path.exists(p):
return None raise FileNotFoundError(p)
logging.debug("Loading cached contents file %s", p) logging.debug("Loading cached contents file %s", p)
#return os.popen('/usr/lib/apt/apt-helper cat-file %s' % p) #return os.popen('/usr/lib/apt/apt-helper cat-file %s' % p)
import lz4.frame import lz4.frame
@ -145,11 +152,15 @@ class AptCachedContentsFileSearcher(FileSearcher):
sl.load("/etc/apt/sources.list") sl.load("/etc/apt/sources.list")
from .build import get_build_architecture from .build import get_build_architecture
cache_dirs = set(["/var/lib/apt/lists"])
def load_url(url):
return load_url_with_cache(url, cache_dirs)
urls = list( urls = list(
contents_urls_from_sourceslist(sl, get_build_architecture())) contents_urls_from_sourceslist(sl, get_build_architecture(),
cache_dirs = set(["/var/lib/apt/lists"]) load_url))
self._load_urls(urls, cache_dirs) self._load_urls(urls, cache_dirs, load_url)
def load_from_session(self, session): def load_from_session(self, session):
# TODO(jelmer): what about sources.list.d? # TODO(jelmer): what about sources.list.d?
@ -160,38 +171,26 @@ class AptCachedContentsFileSearcher(FileSearcher):
from .build import get_build_architecture from .build import get_build_architecture
urls = list(
contents_urls_from_sourceslist(sl, get_build_architecture()))
cache_dirs = set([ cache_dirs = set([
os.path.join(session.location, "var/lib/apt/lists"), os.path.join(session.location, "var/lib/apt/lists"),
"/var/lib/apt/lists", "/var/lib/apt/lists",
]) ])
self._load_urls(urls, cache_dirs)
def _load_urls(self, urls, cache_dirs): def load_url(url):
for url, mandatory in urls: return load_url_with_cache(url, cache_dirs)
for cache_dir in cache_dirs:
f = load_apt_cache_file(cache_dir, url) urls = list(
if f is not None: contents_urls_from_sourceslist(sl, get_build_architecture(), load_url))
self.load_file(f, url) self._load_urls(urls, cache_dirs, load_url)
break
else: def _load_urls(self, urls, cache_dirs, load_url):
if not mandatory and self._db: for url in urls:
logging.debug( logging.debug("Fetching contents file %s", url)
"Not attempting to fetch optional contents " "file %s", url try:
) f = load_url(url)
else: self.load_file(f, url)
logging.debug("Fetching contents file %s", url) except ContentsFileNotFound:
try: logging.warning("Unable to fetch contents file %s", url)
f = load_contents_url(url)
self.load_file(f, url)
except ContentsFileNotFound:
if mandatory:
logging.warning("Unable to fetch contents file %s", url)
else:
logging.debug(
"Unable to fetch optional contents file %s", url
)
def __setitem__(self, path, package): def __setitem__(self, path, package):
self._db[path] = package self._db[path] = package