Skip to content

Commit fe4cc0d

Browse files
Use built in extraction of freezefs to simplify build script
1 parent fa2d97a commit fe4cc0d

File tree

1 file changed

+17
-45
lines changed

1 file changed

+17
-45
lines changed

build.sh

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ function create_frozen_fs {
2727

2828
cp -r $DIR_TO_FREEZE $DIR_NAME_ON_BOARD
2929

30-
python -m freezefs $DIR_NAME_ON_BOARD $OUTPUT_FILE
30+
# Use on-import=extract so our frozen filesystem is unpacked to '/' in flash on import
31+
# Use --compress to compress the frozen filesystem archive
32+
# Use --overwrite always to ensure that the frozen filesystem is returned to factory state if the persistent file is deleted
33+
34+
python -m freezefs $DIR_NAME_ON_BOARD $OUTPUT_FILE --on-import=extract --compress --overwrite always
3135
}
3236

37+
# Adds the provided directory to the manifest file for the specified port and board.
3338
# Options:
3439
# $1: The directory to add to the manifest
3540
# $2: The port (e.g. rp2)
@@ -77,17 +82,11 @@ function add_to_manifest {
7782
# Options:
7883
# $1: Port name
7984
# $2: Frozen data file path
80-
# $3: Copy Source: If copying imported frozen data to a mutable location, this is the directory name of the source (optional)
81-
# $4: Copy Destination: If copying imported frozen data to a mutable location, this is the directory name of the destination (optional)
82-
# $5: Add destination to sys.path? If true, the destination directory will be added to sys.path in _boot.py (optional)
83-
# NOTE: By providing the source and destination, the frozen data filesystem will be copied to a mutable location on the board
84-
# If they are not provided, the frozen data filesystem will still be accessible, but will be read-only.
85+
# $3: Unpacked directory name on the board (optional). If provided, the modules in this directory will be made importable
8586
function add_frozen_data_to_boot_for_port {
8687
local TARGET_PORT_NAME=$1
8788
local FROZEN_DATA_FILE=$2
88-
local SOURCE_DIR=$3
89-
local DESTINATION_DIR=$4
90-
local ADD_TO_SYSPATH=${5:-false}
89+
local UNPACKED_DIR=$3
9190

9291
# Remove the ".py" extension from the frozen data file
9392
local FROZEN_DATA_BASENAME=$(basename $FROZEN_DATA_FILE .py)
@@ -110,41 +109,13 @@ function add_frozen_data_to_boot_for_port {
110109
echo "except OSError:" >> ${BOOT_FILE}
111110
echo " import ${FROZEN_DATA_BASENAME}" >> ${BOOT_FILE}
112111
echo " with open('${PERSISTENT_FILE_FOR_UNPACK}', 'w') as f:" >> ${BOOT_FILE}
113-
echo " f.write('Hi! Delete this file to restore the ${FROZEN_EXAMPLES_UNPACKED_DIR} to its default state. WARNING: This will override ALL of your changes to that directory.')" >> ${BOOT_FILE}
114-
115-
# Now, copy the unpacked frozen data filesystem to a mutable location if the source and destination are provided
116-
# Simple recursive function to copy the directory tree (since i.e. shutil.copytree is not available on MicroPython)
117-
if [ -n "$SOURCE_DIR" ] && [ -n "$DESTINATION_DIR" ]; then
118-
echo "Copying frozen data from ${SOURCE_DIR} to ${DESTINATION_DIR} in _boot.py"
119-
local BOOT_FILE="micropython/ports/${TARGET_PORT_NAME}/modules/_boot.py"
120-
echo " def copytree(src, dst):" >> ${BOOT_FILE}
121-
echo " try:" >> ${BOOT_FILE}
122-
echo " os.mkdir(dst)" >> ${BOOT_FILE}
123-
echo " except OSError:" >> ${BOOT_FILE}
124-
echo " pass" >> ${BOOT_FILE}
125-
echo " for entry in os.ilistdir(src):" >> ${BOOT_FILE}
126-
echo " fname, typecode, _, _ = entry" >> ${BOOT_FILE}
127-
echo " src_path = src + '/' + fname" >> ${BOOT_FILE}
128-
echo " dst_path = dst + '/' + fname" >> ${BOOT_FILE}
129-
echo " if typecode == 0x4000:" >> ${BOOT_FILE} # typecode == 0x4000 means directory
130-
echo " copytree(src_path, dst_path)" >> ${BOOT_FILE}
131-
echo " else:" >> ${BOOT_FILE}
132-
echo " with open(src_path, 'rb') as fsrc:" >> ${BOOT_FILE}
133-
echo " with open(dst_path, 'wb') as fdst:" >> ${BOOT_FILE}
134-
echo " fdst.write(fsrc.read())" >> ${BOOT_FILE}
135-
echo " copytree('${SOURCE_DIR}', '${DESTINATION_DIR}')" >> ${BOOT_FILE}
136-
# Finally, unmount the source directory if it is mounted
137-
echo " try:" >> ${BOOT_FILE}
138-
echo " os.umount('/${SOURCE_DIR}')" >> ${BOOT_FILE}
139-
echo " except Exception as e:" >> ${BOOT_FILE}
140-
echo " print('umount failed:', e)" >> ${BOOT_FILE}
141-
fi
112+
echo " f.write('Hi! Delete this file and reset your board to restore the ${FROZEN_EXAMPLES_UNPACKED_DIR} directory to its default state. WARNING: This will override ALL of your changes to that directory.')" >> ${BOOT_FILE}
142113

143-
# If the ADD_TO_SYSPATH flag is true, add the destination directory to sys.path
144-
if [ "$ADD_TO_SYSPATH" = true ]; then
145-
echo "Adding ${DESTINATION_DIR} to sys.path in _boot.py"
114+
# If a destination directory is provided, we will add it to the sys.path so that the modules in the unpacked directory can be imported
115+
if [ -n "$UNPACKED_DIR" ]; then
116+
echo "Adding ${UNPACKED_DIR} to sys.path in _boot.py"
146117
echo "import sys" >> ${BOOT_FILE}
147-
echo "sys.path.append('/${DESTINATION_DIR}')" >> ${BOOT_FILE}
118+
echo "sys.path.append('/${UNPACKED_DIR}')" >> ${BOOT_FILE}
148119
fi
149120

150121
# Helpful for debugging during the build process, but can be removed if we'd rather not see this output...
@@ -156,7 +127,7 @@ function add_frozen_data_to_boot_for_port {
156127
# Also freezes the examples directory in a filesystem archive on the board
157128
function build_micropython_opencv {
158129
# Install necessary packages (Could move into an install_dependencies.sh if we want this to be more explicit/modular)
159-
sudo apt-get update
130+
sudo apt update
160131
sudo apt install cmake python3 build-essential gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib
161132
# Install necessary python packages (could also move this to a requirements.txt file)
162133
pip install freezefs
@@ -167,15 +138,16 @@ function build_micropython_opencv {
167138

168139
# Create our frozen filesystem archive for the examples directory
169140
# Note the "." to make the read-only version of the examples directory hidden in IDEs like Thonny
170-
create_frozen_fs "examples" ".$FROZEN_EXAMPLES_UNPACKED_DIR" "$FROZEN_MODULES_DIR/$FROZEN_EXAMPLES_ARCHIVE_SCRIPT"
141+
create_frozen_fs "examples" "$FROZEN_EXAMPLES_UNPACKED_DIR" "$FROZEN_MODULES_DIR/$FROZEN_EXAMPLES_ARCHIVE_SCRIPT"
171142

172143
# Add necessary content to the manifest file to freeze the modules in the provided directory
173144
add_to_manifest "$FROZEN_MODULES_DIR" "rp2" "SPARKFUN_XRP_CONTROLLER" "mpconfigvariant_LARGE_BINARY.cmake"
174145

175146
# Add necessary content to the boot.py file to unpack the frozen data filesystem on boot
176147
# Provide the source and destination directories to copy the frozen data filesystem to a mutable (and non-hidden) location
177148
# Provide "true" as the last argument to add the destination directory to sys.path (since our examples directory contains modules that we want to be importable...)
178-
add_frozen_data_to_boot_for_port "rp2" "$FROZEN_EXAMPLES_ARCHIVE_SCRIPT" ".$FROZEN_EXAMPLES_UNPACKED_DIR" "$FROZEN_EXAMPLES_UNPACKED_DIR" true
149+
# add_frozen_data_to_boot_for_port "rp2" "$FROZEN_EXAMPLES_ARCHIVE_SCRIPT" ".$FROZEN_EXAMPLES_UNPACKED_DIR" "$FROZEN_EXAMPLES_UNPACKED_DIR" true
150+
add_frozen_data_to_boot_for_port "rp2" "$FROZEN_EXAMPLES_ARCHIVE_SCRIPT" "$FROZEN_EXAMPLES_UNPACKED_DIR" true
179151

180152
# Set Pico SDK path to $GITHUB_WORKSPACE/micropython/lib/pico-sdk if $GITHUB_WORKSPACE is set, otherwise use the current directory
181153
if [ -n "$GITHUB_WORKSPACE" ]; then

0 commit comments

Comments
 (0)