Skip to content

Commit d5ab998

Browse files
committed
Updated installer.
Signed-off-by: ubi de feo <me@ubidefeo.com>
1 parent 609a7e1 commit d5ab998

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

mpinstall.sh

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,11 @@ function device_present {
7373

7474
# Check if a directory exists
7575
# Returns 0 if directory exists, 1 if it does not
76-
function directory_exists {
76+
function folder_exists {
7777
# Run mpremote and capture the error message
78-
error=$(mpremote fs ls $1)
79-
80-
# Return error if error message contains "OSError: [Errno 2] ENOENT"
81-
if [[ $error == *"OSError: [Errno 2] ENOENT"* ]]; then
78+
error=$(mpremote fs ls "$1" 2>&1)
79+
# Return error if error message contains "ENOENT" or "No such file or directory" (>= 1.26.0)
80+
if [[ $error == *"ENOENT"* ]] || [[ $error == *"No such file or directory"* ]]; then
8281
return 1
8382
else
8483
return 0
@@ -88,7 +87,7 @@ function directory_exists {
8887
# Copies a file to the board using mpremote
8988
# Only produces output if an error occurs
9089
function copy_file {
91-
output="Copying $1 to $2"
90+
output="Copying file to board: $1 >> $2"
9291
echo -n "$output"
9392
# Run mpremote and capture the error message
9493
error=$(mpremote cp $1 $2)
@@ -103,7 +102,7 @@ function copy_file {
103102
# Deletes a file from the board using mpremote
104103
# Only produces output if an error occurs
105104
function delete_file {
106-
output="Deleting File $1"
105+
output="Deleting file on board: $1"
107106
echo -n "$output"
108107
# Run mpremote and capture the error message
109108
error=$(mpremote rm $1)
@@ -116,7 +115,7 @@ function delete_file {
116115
}
117116

118117
function create_folder {
119-
output_msg="Creating $1 on board"
118+
output_msg="Creating folder on board: $1"
120119
echo -n "$output_msg"
121120
error=$(mpremote mkdir "$1")
122121
# Print error message if return code is not 0
@@ -128,7 +127,7 @@ function create_folder {
128127
}
129128

130129
function delete_folder {
131-
output_msg="Deleting Folder $1 on board"
130+
output_msg="Deleting Folder on board: $1"
132131
echo -n "$output_msg"
133132
delete_folder="${PYTHON_HELPERS}delete_folder(\"/$1\")"
134133
error=$(mpremote exec "$delete_folder")
@@ -140,6 +139,16 @@ function delete_folder {
140139
echo -e "\r√ $output_msg"
141140
}
142141

142+
function get_board_lib_path {
143+
device_root="${PYTHON_HELPERS}get_root()"
144+
output=$(mpremote exec "$device_root")
145+
output=$(echo "$output" | tr -d '[:space:]')
146+
if [[ -n "$output" ]]; then
147+
echo "$output/lib"
148+
else
149+
echo "lib"
150+
fi
151+
}
143152

144153
function install_package {
145154
if [[ $1 == "" ]]; then
@@ -152,17 +161,10 @@ function install_package {
152161
PKGDIR=`basename $1`
153162
# Source directory for the package on the host
154163
SRCDIR=`realpath .`
155-
# Board's library directory
156-
device_root="${PYTHON_HELPERS}print(get_root())"
157-
output=$(mpremote exec "$device_root")
158-
output=$(echo "$output" | tr -d '[:space:]')
159-
# echo "Device Root: $output"
160-
if [ "$output" == "/flash" ]; then
161-
echo "Board has root in /flash"
162-
# output=""
163-
fi
164-
LIBDIR="$output""lib"
165-
create_folder "$LIBDIR"
164+
165+
LIBDIR="$(get_board_lib_path)"
166+
echo "Installing package $PKGNAME from $SRCDIR to $LIBDIR/$PKGDIR"
167+
166168
IFS=$'\n' read -rd '' -a package_files < <(find . -mindepth 1)
167169
items_count=${#package_files[@]}
168170
current_item=0
@@ -176,8 +178,8 @@ function install_package {
176178
# only delete and create package directory if it is the first item
177179
# if the script never made it here, it means no files were found
178180
if [ $current_item == 1 ]; then
179-
output_msg="Deleting Package $LIBDIR/$PKGDIR on board"
180-
if directory_exists "${LIBDIR}/${PKGDIR}"; then
181+
output_msg="Deleting Package folder on board: $LIBDIR/$PKGDIR"
182+
if folder_exists "${LIBDIR}/${PKGDIR}"; then
181183
echo -n "$output_msg"
182184
delete_folder="${PYTHON_HELPERS}delete_folder(\"${LIBDIR}/${PKGDIR}\")"
183185
mpremote exec "$delete_folder"
@@ -196,7 +198,6 @@ function install_package {
196198
destination_extension=$source_extension
197199
clean_item_path="${item_path//.\//}"
198200
if [[ "$ext" == "mpy" && "$source_extension" == "py" ]]; then
199-
echo -n "Compiling $clean_item_path to mpy"
200201
mpy-cross "$item_path"
201202
destination_extension=$ext
202203
copy_file ${clean_item_path%.*}.$destination_extension :$LIBDIR/${destination_subpath%.*}.$destination_extension
@@ -244,7 +245,6 @@ for arg in "$@"; do
244245
continue
245246
fi
246247
if [ "$arg" == "--mpy" ]; then
247-
echo "Compiling .py files to .mpy"
248248
ext="mpy"
249249
continue
250250
fi
@@ -266,6 +266,16 @@ if device_present == 0; then
266266
exit 1
267267
fi
268268

269+
LIBDIR="$(get_board_lib_path)"
270+
output_msg=""
271+
if folder_exists "${LIBDIR}"; then
272+
output_msg="Library folder ($LIBDIR) exists on board"
273+
else
274+
create_folder "$LIBDIR"
275+
output_msg="Library folder ($LIBDIR) did not exist on board, it was created."
276+
fi
277+
echo -ne "\r\033[2K"
278+
echo -e "\r√ $output_msg"
269279
package_number=0
270280
start_dir=`pwd`
271281
for package in "${packages[@]}"; do

0 commit comments

Comments
 (0)