Skip to content

Commit 5de809f

Browse files
Add unmount of immutable directory and persistent tracking so expanding examples only happens once
1 parent 5728bc4 commit 5de809f

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

build.sh

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ fi
88
FROZEN_MODULES_DIR="$(dirname "$0")/frozen_modules"
99
FROZEN_EXAMPLES_ARCHIVE_SCRIPT="frozen_examples.py"
1010
FROZEN_EXAMPLES_UNPACKED_DIR="micropython-opencv-examples"
11+
PERSISTENT_FILE_FOR_UNPACK="/keep_opencv_example_changes"
1112

1213
# Uses freezefs to create a frozen filesystem archive for the provided directory.
1314
# See https://github.com/bixb922/freezefs for more details on freezefs
@@ -100,31 +101,43 @@ function add_frozen_data_to_boot_for_port {
100101
# Add the frozen data filesystem to the _boot.py file
101102
local BOOT_FILE="micropython/ports/${TARGET_PORT_NAME}/modules/_boot.py"
102103

104+
# Create our "persistent file for unpack" that will be used to check if the frozen data filesystem has already been unpacked
105+
# If it has not been unpacked, we will import the frozen data filesystem
103106
echo "Adding frozen data filesystem to ${BOOT_FILE}"
104-
echo "import ${FROZEN_DATA_BASENAME}" >> ${BOOT_FILE}
107+
echo "import os" >> ${BOOT_FILE}
108+
echo "try:" >> ${BOOT_FILE}
109+
echo " os.stat('${PERSISTENT_FILE_FOR_UNPACK}')" >> ${BOOT_FILE}
110+
echo "except OSError:" >> ${BOOT_FILE}
111+
echo " import ${FROZEN_DATA_BASENAME}" >> ${BOOT_FILE}
112+
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}
105114

106115
# 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)
107117
if [ -n "$SOURCE_DIR" ] && [ -n "$DESTINATION_DIR" ]; then
108118
echo "Copying frozen data from ${SOURCE_DIR} to ${DESTINATION_DIR} in _boot.py"
109119
local BOOT_FILE="micropython/ports/${TARGET_PORT_NAME}/modules/_boot.py"
110-
echo "import os" >> ${BOOT_FILE}
111-
# Simple recursive function to copy the directory tree (since i.e. shutil.copytree is not available on MicroPython)
112-
echo "def copytree(src, dst):" >> ${BOOT_FILE}
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
113137
echo " try:" >> ${BOOT_FILE}
114-
echo " os.mkdir(dst)" >> ${BOOT_FILE}
115-
echo " except OSError:" >> ${BOOT_FILE}
116-
echo " pass" >> ${BOOT_FILE}
117-
echo " for entry in os.ilistdir(src):" >> ${BOOT_FILE}
118-
echo " fname, typecode, _, _ = entry" >> ${BOOT_FILE}
119-
echo " src_path = src + '/' + fname" >> ${BOOT_FILE}
120-
echo " dst_path = dst + '/' + fname" >> ${BOOT_FILE}
121-
echo " if typecode == 0x4000:" >> ${BOOT_FILE} # typecode == 0x4000 means directory
122-
echo " copytree(src_path, dst_path)" >> ${BOOT_FILE}
123-
echo " else:" >> ${BOOT_FILE}
124-
echo " with open(src_path, 'rb') as fsrc:" >> ${BOOT_FILE}
125-
echo " with open(dst_path, 'wb') as fdst:" >> ${BOOT_FILE}
126-
echo " fdst.write(fsrc.read())" >> ${BOOT_FILE}
127-
echo "copytree('${SOURCE_DIR}', '${DESTINATION_DIR}')" >> ${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}
128141
fi
129142

130143
# If the ADD_TO_SYSPATH flag is true, add the destination directory to sys.path

0 commit comments

Comments
 (0)