Skip to content

Commit

Permalink
Add rpi official images download type
Browse files Browse the repository at this point in the history
  • Loading branch information
guysoft committed Jul 27, 2024
1 parent af3bd56 commit 615598b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 13 deletions.
57 changes: 53 additions & 4 deletions src/custompios_core/base_image_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import hashlib
import shutil
import re
from typing import Dict, Any, Optional, cast
from typing import Dict, Any, Optional, cast, Tuple
from common import get_image_config, read_images
PRECENT_PROGRESS_SIZE = 5

Expand All @@ -27,6 +27,31 @@ def ensure_dir(d, chmod=0o777):
return False
return True


def download_webpage(url: str) -> Optional[str]:
try:
with urllib.request.urlopen(url) as response:
# Decode the response to a string
webpage = response.read().decode('utf-8')
return webpage
except Exception as e:
print(str(e))
return None

def get_location_header(url: str) -> str:
try:
with urllib.request.urlopen(url) as response:
response_url = response.url

if response_url is None:
raise Exception("Location header is None, can't determine latest rpi image")
return response_url
except Exception as e:
print(str(e))
print("Error: Failed to determine latest rpi image")
raise e


class DownloadProgress:
last_precent: float = 0
def show_progress(self, block_num, block_size, total_size):
Expand All @@ -52,7 +77,9 @@ def get_sha256(filename):
def download_image_http(board: Dict[str, Any], dest_folder: str, redownload: bool = False):
url = board["url"]
checksum = board["checksum"]
download_http(url, checksum)

def download_http(url: str, checksum_url: str, dest_folder: str, redownload: bool = False):
with tempfile.TemporaryDirectory() as tmpdirname:
print('created temporary directory', tmpdirname)
temp_file_name = os.path.join(tmpdirname, "image.xz")
Expand All @@ -62,8 +89,8 @@ def download_image_http(board: Dict[str, Any], dest_folder: str, redownload: boo
try:
# Get sha and confirm its the right image
download_progress = DownloadProgress()
_, headers_checksum = urllib.request.urlretrieve(checksum, temp_file_checksum, download_progress.show_progress)
file_name_checksum = get_file_name(headers_checksum, checksum)
_, headers_checksum = urllib.request.urlretrieve(checksum_url, temp_file_checksum, download_progress.show_progress)
file_name_checksum = get_file_name(headers_checksum, checksum_url)

checksum_data = None
with open(temp_file_checksum, 'r') as f:
Expand All @@ -78,7 +105,7 @@ def download_image_http(board: Dict[str, Any], dest_folder: str, redownload: boo
if os.path.isfile(dest_file_name):
file_checksum = get_sha256(dest_file_name)
if file_checksum == online_checksum:
# We got file and checksum is right
print("We got base image file and checksum is right")
return
# Get the file
download_progress = DownloadProgress()
Expand All @@ -104,6 +131,25 @@ def download_image_http(board: Dict[str, Any], dest_folder: str, redownload: boo
break
return


def download_image_rpi(board: Dict[str, Any], dest_folder: str):
port = board.get("port", "lite_armhf")
os_name = f"raspios"
distribution = board.get("distribution", "bookworm")
version_file = board.get("version_file", "latest")
version_folder = board.get("version_folder", "latest")

latest_url = f"https://downloads.raspberrypi.org/{os_name}_{port}_latest"

download_url = f"https://downloads.raspberrypi.org/{os_name}_{port}/images/{os_name}_{port}-{version_folder}/{version_file}-{os_name}-{distribution}-{port}.img.xz"
if version_file == "latest" or version_folder == "latest":
download_url = get_location_header(latest_url)

checksum_url = f"{download_url}.sha256"
download_http(download_url, checksum_url, dest_folder)
return


if __name__ == "__main__":
parser = argparse.ArgumentParser(add_help=True, description='Download images based on BASE_BOARD and BASE_O')
parser.add_argument('WORKSPACE_SUFFIX', nargs='?', default="default", help="The workspace folder suffix used folder")
Expand All @@ -125,6 +171,9 @@ def download_image_http(board: Dict[str, Any], dest_folder: str, redownload: boo
if image_config["type"] == "http":
print(f"Downloading image for {base_board}")
download_image_http(image_config, base_image_path)
elif image_config["type"] == "rpi":
print(f"Downloading Raspberry Pi image for {base_board}")
download_image_rpi(image_config, base_image_path)
elif image_config["type"] == "torrent":
print("Error: Torrent not implemented")
exit(1)
Expand Down
24 changes: 15 additions & 9 deletions src/images.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
images:
raspberrypiarmhf:
description: "Official raspberrypi lite 32bit image"
# url: "https://downloads.raspberrypi.com/raspios_oldstable_lite_armhf/images/raspios_oldstable_lite_armhf-2023-10-10/2023-05-03-raspios-bullseye-armhf-lite.img.xz.torrent"
# checksum: "https://downloads.raspberrypi.com/raspios_oldstable_lite_armhf/images/raspios_oldstable_lite_armhf-2023-10-10/2023-05-03-raspios-bullseye-armhf-lite.img.xz.sha256"
# type: torrent
url: "https://downloads.raspberrypi.org/raspios_lite_armhf/images/raspios_lite_armhf-2024-03-15/2024-03-15-raspios-bookworm-armhf-lite.img.xz"
checksum: "https://downloads.raspberrypi.org/raspios_lite_armhf/images/raspios_lite_armhf-2024-03-15/2024-03-15-raspios-bookworm-armhf-lite.img.xz.sha256"
type: http
type: rpi
env:
BASE_ARCH: armhf
raspberrypiarm64:
description: "Official raspberrypi lite 64bit image"
url: "https://downloads.raspberrypi.com/raspios_oldstable_lite_arm64/images/raspios_oldstable_lite_arm64-2023-10-10/2023-05-03-raspios-bullseye-arm64-lite.img.xz.torrent"
checksum: "https://downloads.raspberrypi.com/raspios_oldstable_lite_arm64/images/raspios_oldstable_lite_arm64-2023-10-10/2023-05-03-raspios-bullseye-arm64-lite.img.xz.sha256"
type: torrent
type: rpi
port: lite_arm64
env:
BASE_ARCH: arm64
raspberrypiarmhf_full:
description: "Official raspberrypi full 32bit image"
type: rpi
port: full_armhf
env:
BASE_ARCH: armhf
raspberrypiarm64:
description: "Official raspberrypi full 64bit image"
type: rpi
port: full_arm64
env:
BASE_ARCH: arm64
orangepi_orangepi_zero2:
Expand Down

0 comments on commit 615598b

Please sign in to comment.