sources: introduce per-source content_type

Introduce a new class member `content_type` that specifies what type of
items the source will store in the cache. Use that to generalize the
setup step, which is shared across all sources.
This commit is contained in:
Thomas Lavocat 2022-05-10 12:54:08 +02:00 committed by Thomas Lavocat
parent 34cd9ef9f0
commit 92fe237f24
5 changed files with 23 additions and 11 deletions

View file

@ -3,6 +3,7 @@ import contextlib
import os import os
import json import json
import tempfile import tempfile
from abc import abstractmethod
from . import host from . import host
from .objectstore import ObjectStore from .objectstore import ObjectStore
@ -54,12 +55,19 @@ class SourceService(host.Service):
def download(self, items, cache, options): def download(self, items, cache, options):
pass pass
def setup(self, cache, content_type): @property
self.cache = os.path.join(cache, content_type) @classmethod
@abstractmethod
def content_type(cls):
"""The content type of the source."""
def setup(self, args):
self.cache = os.path.join(args["cache"], self.content_type)
os.makedirs(self.cache, exist_ok=True) os.makedirs(self.cache, exist_ok=True)
def dispatch(self, method: str, args, fds): def dispatch(self, method: str, args, fds):
if method == "download": if method == "download":
self.setup(args)
with os.fdopen(fds.steal(0)) as f: with os.fdopen(fds.steal(0)) as f:
items = json.load(f) items = json.load(f)

View file

@ -165,8 +165,9 @@ def download(items, cache):
class CurlSource(sources.SourceService): class CurlSource(sources.SourceService):
def download(self, items, cache, _options): content_type = "org.osbuild.files"
self.setup(cache, "org.osbuild.files")
def download(self, items, _cache, _options):
download(items, self.cache) download(items, self.cache)

View file

@ -81,9 +81,10 @@ def process(items: Dict, cache: str, tmpdir):
class InlineSource(sources.SourceService): class InlineSource(sources.SourceService):
def download(self, items, cache, _options): content_type = "org.osbuild.files"
self.setup(cache, "org.osbuild.files")
with tempfile.TemporaryDirectory(prefix=".unverified-", dir=cache) as tmpdir: def download(self, items, _cache, _options):
with tempfile.TemporaryDirectory(prefix=".unverified-", dir=self.cache) as tmpdir:
process(items, self.cache, tmpdir) process(items, self.cache, tmpdir)

View file

@ -114,8 +114,9 @@ def download(items, cache):
class OSTreeSource(sources.SourceService): class OSTreeSource(sources.SourceService):
def download(self, items, cache, _options): content_type = "org.osbuild.ostree"
self.setup(cache, "org.osbuild.ostree")
def download(self, items, _cache, _options):
download(items, self.cache) download(items, self.cache)

View file

@ -118,8 +118,9 @@ def download(items, cache):
class SkopeoSource(sources.SourceService): class SkopeoSource(sources.SourceService):
def download(self, items, cache, _options): content_type = "org.osbuild.containers"
self.setup(cache, "org.osbuild.containers")
def download(self, items, _cache, _options):
download(items, self.cache) download(items, self.cache)