""" centos stream download """
import os

from virt.qemutest.distro.core import FetchDistroCore, FetchNetinst

class FetchCentosCloud(FetchDistroCore):
    """ class for centos stream cloud image download """

    centos = "https://cloud.centos.org/centos/"

    def __init__(self, mirror = None, arch = None):
        super().__init__(arch)
        self.mirror = mirror
        if mirror is None:
            self.mirror = self.centos

    def baseurl(self, release):
        return f'{self.mirror}{release}-stream/{self.arch}/images/'

    def image_id(self, release):
        return f'centos-stream-{release}-{self.arch}-cloud'

    def find_image(self, release, pattern):
        checksum = f'{self.baseurl(release)}CHECKSUM'
        return self.checksum_find_latest(checksum, pattern)

    def fetch_image(self, release = 10, pattern = 'GenericCloud', imgdir = 'distro'):
        print(f'# ----- centos {release} ({pattern}) -----')
        (latest, checksum) = self.find_image(release, pattern)
        url = f'{self.baseurl(release)}{latest}'
        out = f'{imgdir}/{latest}'
        os.makedirs(imgdir, exist_ok = True)
        self.fetch_image_checksum(url, out, checksum)
        self.index_update(imgdir, self.image_id(release), latest, checksum)

class FetchCentosNetinst(FetchNetinst):
    """ class for centos stream netinst iso download """

    centos = "https://mirror.stream.centos.org/"

    def __init__(self, mirror = None, arch = None):
        if mirror is None:
            mirror = self.centos
        super().__init__(mirror, arch)

    def baseurl(self, release):
        repo = f'{self.mirror}{release}-stream/BaseOS/{self.arch}/os/'
        return repo
