Пример #1
0
    def get(tag="latest"):
        url = Iso.distribution[tag]
        destination = Image().directory + os.path.basename(url)

        Shell.download(url, destination, provider='system')
Пример #2
0
    def fetch(self, url=None, tag=None, verify=True):
        """
        Download the image from the URL in self.image_name
        If it is 'latest', download the latest image - afterwards use
          cm-pi-burn image ls
          to get the name of the downloaded latest image.
        """

        if url is None:
            data = Image().create_version_cache(refresh=False)  # noqa: F841

            image = Image().find(tag=tag)

            if image is None:
                Console.error("No matching image found.")
                return None
            elif len(image) > 1:
                Console.error("Too many images found")
                print(
                    Printer.write(image,
                                  order=["tag", "version"],
                                  header=["Tag", "Version"]))
                return None

            image = image[0]

            # image_path = Image().directory + "/" + Image.get_name(image[
            # "url"]) + ".img"

        if not os.path.exists(self.directory):
            os.makedirs(self.directory)
        os.chdir(self.directory)
        # get image URL metadata, including the name of the latest image after
        #   the 'latest' URL redirects to the URL of the actual image

        if "ubuntu" in image["url"]:
            source_url = requests.head(image["url"], allow_redirects=True).url
            size = requests.get(image["url"], verify=False,
                                stream=True).headers['Content-length']

            xz_filename = os.path.basename(source_url)
            img_filename = xz_filename.replace('.xz', '')
            img_file = Path(Path(self.directory) / Path(img_filename))
            # xz_file = Path(Path(self.directory) / Path(xz_filename))

            print(f"Downloading {xz_filename}")

            os.chdir(path_expand("~/.cloudmesh/cmburn/images"))

            if os.path.exists(img_filename):
                print()
                Console.warning(
                    f"The file is already downloaded. Found at:\n\n"
                    f"    {img_file}\n")
                return img_file
            Shell.download(image["url"], xz_filename, provider='system')
            # wget.download(image["url"], out=xz_filename)
            # os.system(f'wget -O {xz_filename} {image["url"]}')

            print(f"Extracting {img_filename}")
            self.unzip_image(xz_filename)
            try:
                Path(xz_filename).unlink()
            except:  # noqa: E722
                pass
            return img_filename

        else:
            source_url = requests.head(image["url"], allow_redirects=True).url
            size = requests.get(image["url"], verify=False,
                                stream=True).headers['Content-length']
            zip_filename = os.path.basename(source_url)
            img_filename = zip_filename.replace('.zip', '.img')
            sha1_filename = zip_filename + '.sha1'
            sha256_filename = zip_filename + '.sha256'

            print(f"Downloading {zip_filename}")

            img_file = Path(Path(self.directory) / Path(img_filename))
            zip_file = Path(Path(self.directory) / Path(zip_filename))
            # sha1_file = Path(Path(self.directory) / Path(sha1_filename))
            # sha256_file = Path(Path(self.directory) / Path(sha256_filename))

            # cancel if image already downloaded
            if os.path.exists(img_filename):
                print()
                Console.warning(
                    f"The file is already downloaded. Found at:\n\n"
                    f"    {img_file}\n")
                return img_file

            # download the image, unzip it, and delete the zip file
            image['sha1'] = image['url'] + ".sha1"
            image['sha256'] = image['url'] + ".sha256"
            if verify:
                Shell.download(image["sha1"], sha1_filename, provider='system')
                Shell.download(image["sha256"],
                               sha256_filename,
                               provider='system')
                # wget.download(image["sha1"], out=sha1_filename)
                # wget.download(image["sha256"], out=sha256_filename)
                # os.system(f'wget -O {sha1_filename} {image["sha1"]}')
                # os.system(f'wget -O {sha256_filename} {image["sha256"]}')

            Shell.download(image["url"], zip_filename, provider='system')
            # wget.download(image["url"], out=zip_filename)
            # os.system(f'wget -O {zip_filename} {image["url"]}')

            if verify:
                sha1 = sha1sum(zip_file)
                sha256 = sha256sum(zip_filename)

                f_sha1 = readfile(sha1_filename).split(" ")[0]
                f_sha256 = readfile(sha256_filename).split(" ")[0]

                if f_sha1 == sha1:
                    Console.ok("SHA1 is ok")
                if f_sha256 == sha256:
                    Console.ok("SHA256 is ok")

            zip_size = os.path.getsize(zip_file)
            if int(size) != zip_size:
                Console.error(
                    f"Repository reported zip size {size} does not equal "
                    f"download size {zip_size}. Please try again.")
                return None

            #   if latest:  # rename filename from 'latest' to the actual image name
            #        Path('raspbian_lite_latest').rename(zip_filename)

            print(f"Extracting {img_filename}")
            self.unzip_image(zip_filename)
            Path(zip_filename).unlink()
            return img_filename