-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinstall.sh
executable file
·149 lines (123 loc) · 4.52 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/bash
#
# MicroPython Package Installer
# Created by: Ubi de Feo and Sebastian Romero
#
# Installs a MicroPython Package to a board using mpremote.
#
# This script accepts an optional argument to compile .py files to .mpy.
# Simply run the script with the optional argument:
#
# ./install.sh mpy
# Name to display during installation
PKGNAME="MicroPython Button Library"
# Destination directory for the package on the board
PKGDIR="mp_button"
# Source directory for the package on the host
SRCDIR=$PKGDIR
# Board library directory
LIBDIR="lib"
# File system operations such as "mpremote mkdir" or "mpremote rm"
# will generate an error if the folder exists or if the file does not exist.
# These errors can be ignored.
#
# Traceback (most recent call last):
# File "<stdin>", line 2, in <module>
# OSError: [Errno 17] EEXIST
# Check if a directory exists
# Returns 0 if directory exists, 1 if it does not
function directory_exists {
# Run mpremote and capture the error message
error=$(mpremote fs ls $1)
# Return error if error message contains "OSError: [Errno 2] ENOENT"
if [[ $error == *"OSError: [Errno 2] ENOENT"* ]]; then
return 1
else
return 0
fi
}
# Copies a file to the board using mpremote
# Only produces output if an error occurs
function copy_file {
echo "Copying $1 to $2"
# Run mpremote and capture the error message
error=$(mpremote cp $1 $2)
# Print error message if return code is not 0
if [ $? -ne 0 ]; then
echo "Error: $error"
fi
}
# Deletes a file from the board using mpremote
# Only produces output if an error occurs
function delete_file {
echo "Deleting $1"
# Run mpremote and capture the error message
error=$(mpremote rm $1)
# Print error message if return code is not 0
if [ $? -ne 0 ]; then
echo "Error: $error"
fi
}
echo "Installing $PKGNAME"
# If directories do not exist, create them
if ! directory_exists "/${LIBDIR}"; then
echo "Creating $LIBDIR on board"
mpremote mkdir "${LIBDIR}"
fi
if ! directory_exists "/${LIBDIR}/${PKGDIR}"; then
echo "Creating $LIBDIR/$PKGDIR on board"
mpremote mkdir "/${LIBDIR}/${PKGDIR}"
fi
ext="py"
if [ "$1" = "mpy" ]; then
ext=$1
echo ".py files will be compiled to .mpy"
fi
existing_files=$(mpremote fs ls ":/${LIBDIR}/${PKGDIR}")
for filename in $SRCDIR/*; do
f_name=`basename $filename`
source_extension="${f_name##*.}"
destination_extension=$source_extension
# If examples are distributed within the package
# ensures they are copied but not compiled to .mpy
if [[ -d $filename && "$f_name" == "examples" ]]; then
if ! directory_exists "/${LIBDIR}/${PKGDIR}/examples"; then
echo "Creating $LIBDIR/$PKGDIR/examples on board"
mpremote mkdir "/${LIBDIR}/${PKGDIR}/examples"
fi
for example_file in $filename/*; do
example_f_name=`basename $example_file`
example_source_extension="${example_f_name##*.}"
example_destination_extension=$example_source_extension
if [[ $existing_files == *"${example_f_name%.*}.$example_source_extension"* ]]; then
delete_file ":/${LIBDIR}/$PKGDIR/examples/${example_f_name%.*}.$example_source_extension"
fi
if [ "$example_source_extension" = "py" ] && [[ $existing_files == *"${example_f_name%.*}.mpy"* ]]; then
delete_file ":/${LIBDIR}/$PKGDIR/examples/${example_f_name%.*}.mpy"
fi
copy_file $filename/${example_f_name%.*}.$example_destination_extension ":/${LIBDIR}/$PKGDIR/examples/${example_f_name%.*}.$example_destination_extension"
done
continue
fi
if [[ "$ext" == "mpy" && "$source_extension" == "py" ]]; then
echo "Compiling $SRCDIR/$f_name to $SRCDIR/${f_name%.*}.$ext"
mpy-cross "$SRCDIR/$f_name"
destination_extension=$ext
fi
# Make sure previous versions of the given file are deleted.
if [[ $existing_files == *"${f_name%.*}.$source_extension"* ]]; then
delete_file ":/${LIBDIR}/$PKGDIR/${f_name%.*}.$source_extension"
fi
# Check if source file has a .py extension and if a .mpy file exists on the board
if [ "$source_extension" = "py" ] && [[ $existing_files == *"${f_name%.*}.mpy"* ]]; then
delete_file ":/${LIBDIR}/$PKGDIR/${f_name%.*}.mpy"
fi
# Copy either the .py or .mpy file to the board depending on the chosen option
copy_file $SRCDIR/${f_name%.*}.$destination_extension ":/${LIBDIR}/$PKGDIR/${f_name%.*}.$destination_extension"
done
if [ "$ext" == "mpy" ]; then
echo "cleaning up mpy files"
rm $SRCDIR/*.mpy
fi
echo "Done. Resetting target board ..."
mpremote reset