Skip to content

Commit aba4be3

Browse files
committed
Add base board config generation from yaml, refactor and add a common.py and typing #214
1 parent c107c3b commit aba4be3

File tree

6 files changed

+58
-15
lines changed

6 files changed

+58
-15
lines changed

src/base_image_downloader.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
import hashlib
88
import shutil
99
import re
10+
from typing import Dict, Any, Optional, cast
11+
from common import get_image_config, read_images
1012
PRECENT_PROGRESS_SIZE = 5
1113

1214
class ChecksumFailException(Exception):
1315
pass
1416

15-
IMAGES_CONFIG = os.path.join(os.path.dirname(__file__), "images.yml")
1617
RETRY = 3
1718

1819
def ensure_dir(d, chmod=0o777):
@@ -26,13 +27,6 @@ def ensure_dir(d, chmod=0o777):
2627
return False
2728
return True
2829

29-
def read_images():
30-
if not os.path.isfile(IMAGES_CONFIG):
31-
raise Exception(f"Error: Remotes config file not found: {IMAGES_CONFIG}")
32-
with open(IMAGES_CONFIG,'r') as f:
33-
output = yaml.safe_load(f)
34-
return output
35-
3630
class DownloadProgress:
3731
last_precent: float = 0
3832
def show_progress(self, block_num, block_size, total_size):
@@ -53,7 +47,7 @@ def get_sha256(filename):
5347
return file_checksum
5448
return
5549

56-
def download_image_http(board: str, dest_folder: str, redownload: bool = False):
50+
def download_image_http(board: Dict[str, Any], dest_folder: str, redownload: bool = False):
5751
url = board["url"]
5852
checksum = board["checksum"]
5953

@@ -118,14 +112,21 @@ def download_image_http(board: str, dest_folder: str, redownload: bool = False):
118112
base_board = os.environ.get("BASE_BOARD", None)
119113
base_image_path = os.environ.get("BASE_IMAGE_PATH", None)
120114

121-
if base_board is not None and base_board in images["images"]:
122-
if images["images"][base_board]["type"] == "http":
123-
download_image_http(images["images"][base_board], base_image_path)
124-
elif images["images"][base_board]["type"] == "torrent":
115+
if base_image_path is None:
116+
print(f'Error: did not find image config file')
117+
exit(1)
118+
cast(str, base_image_path)
119+
120+
image_config = get_image_config()
121+
if image_config is not None:
122+
if image_config["type"] == "http":
123+
download_image_http(image_config, base_image_path)
124+
elif image_config["type"] == "torrent":
125125
print("Error: Torrent not implemented")
126126
exit(1)
127127
else:
128-
print("Error: Unsupported image download type")
128+
print(f'Error: Unsupported image download type: {image_config["type"]}')
129129
exit(1)
130+
130131

131132
print("Done")

src/build

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ define(){ IFS='\n' read -r -d '' ${1} || true; }
1010

1111
define SCRIPT <<'EOF'
1212
BUILD_SCRIPT_PATH=$(dirname $(realpath -s $BASH_SOURCE))
13+
export EXTRA_BAORD_CONFIG=$(mktemp)
14+
${BUILD_SCRIPT_PATH}/generate_board_config.py "${EXTRA_BAORD_CONFIG}"
15+
echo "Temp source file: ${EXTRA_BAORD_CONFIG}"
16+
1317
source ${BUILD_SCRIPT_PATH}/common.sh
1418
install_cleanup_trap
1519
1620
CUSTOM_OS_PATH=$(dirname $(realpath -s $0))
1721
18-
source ${CUSTOM_PI_OS_PATH}/config ${@}
22+
source ${CUSTOM_PI_OS_PATH}/config ${1} "${EXTRA_BAORD_CONFIG}" ${@}
1923
${CUSTOM_PI_OS_PATH}/config_sanity
2024
2125
[ "$CONFIG_ONLY" == "yes" ] || source ${CUSTOM_OS_PATH}/custompios ${@}

src/common.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
""" Common functions between CustomPiOS python scripts"""
2+
from typing import Dict, Any, Optional, cast
3+
import yaml
4+
import os
5+
6+
IMAGES_CONFIG = os.path.join(os.path.dirname(__file__), "images.yml")
7+
8+
def read_images():
9+
if not os.path.isfile(IMAGES_CONFIG):
10+
raise Exception(f"Error: Remotes config file not found: {IMAGES_CONFIG}")
11+
with open(IMAGES_CONFIG,'r') as f:
12+
output = yaml.safe_load(f)
13+
return output
14+
15+
def get_image_config() -> Optional[Dict["str", Any]]:
16+
images = read_images()
17+
18+
base_board = os.environ.get("BASE_BOARD", None)
19+
base_image_path = os.environ.get("BASE_IMAGE_PATH", None)
20+
21+
if base_board is not None and base_board in images["images"]:
22+
return images["images"][base_board]
23+
return None

src/config

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export BUILD_VARIANT=""
77
BUILD_VARIANT="$1"
88
: ${BUILD_VARIANT:=default}
99

10+
EXTRA_BAORD_CONFIG=$2
11+
1012
export BUILD_FLAVOR=""
1113
# Disable flavor system
1214
#BUILD_FLAVOR="$1"
@@ -86,6 +88,13 @@ MODULES_LIST="${TMP//)/,}"
8688
# [ -n "$BASE_CHROOT_SCRIPT_PATH" ] || BASE_CHROOT_SCRIPT_PATH=$BASE_SCRIPT_PATH/chroot_script
8789
[ -n "$BASE_MOUNT_PATH" ] || BASE_MOUNT_PATH=$BASE_WORKSPACE/mount
8890

91+
# Import remote and submodules config
92+
if [ -f "${EXTRA_BAORD_CONFIG}" ]; then
93+
source "${EXTRA_BAORD_CONFIG}"
94+
else
95+
echo "Note: Not sourceing board config"
96+
fi
97+
8998
export REMOTE_AND_META_CONFIG="$BASE_WORKSPACE"/remote_and_meta_config
9099
# Remote modules and meta modulese go in first if they want to change standard behaviour
91100
if [ -f "${REMOTE_AND_META_CONFIG}" ]; then

src/custompios

+5
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ fi
108108
mkdir -p $BASE_WORKSPACE
109109
mkdir -p $BASE_MOUNT_PATH
110110

111+
# This is already genrated at "build" sourced in "config", but copying here mostly for debug
112+
if [ -f "${EXTRA_BAORD_CONFIG}" ]; then
113+
mv -v "${EXTRA_BAORD_CONFIG}" "${BASE_WORKSPACE}"/extra_board_config
114+
fi
115+
111116
# Clean exported artifacts from other builds
112117
rm -rf "${BASE_WORKSPACE}"/*.tar.gz
113118

src/requirements-devel.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
types-PyYAML

0 commit comments

Comments
 (0)