Skip to content

Commit 609a7e1

Browse files
committed
Improved command line tools.
Signed-off-by: ubi de feo <me@ubidefeo.com>
1 parent 0431ab1 commit 609a7e1

File tree

8 files changed

+360
-312
lines changed

8 files changed

+360
-312
lines changed

shell_tools/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# MicroPython Apps Framework developer helpers [WIP]
2+
3+
These `bash` shell scripts simplify working with the framework from the host machine and allows creating, deploying and backing up applications to and fromt he board.
4+
5+
## Requirements
6+
7+
These scripts require `arduino-tools-mpy` to be installed on the board and `mpremote` to be installed on the host computer:
8+
9+
```shell
10+
pip install mpremote
11+
```
12+
13+
## How to use
14+
```shell
15+
./chmod +x app_util
16+
./app_util
17+
mpremote: no device found
18+
Usage: ./app_util.sh <command>:
19+
help
20+
• create
21+
• install
22+
• backup
23+
• remove
24+
• delete
25+
• list
26+
• run
27+
```
28+
29+
## How it works
30+
31+
These scripts will leverage the board's installed `arduino-tools-mpy` to create, backup and delete MicroPython apps on the board.
32+
For example, creating an app with `./app_util create {APP_NAME} {App Friendly Name}` will run code on the board, create the app (will ask for confirmation to overwrite if already present), back it up to a `.tar` archive and transfer it to the local machine, expand it and make it available locally.
33+
34+
Running
35+
36+
```shell
37+
./app_util create demo_app Demo Application
38+
```
39+
40+
Will generate the following output:
41+
42+
```shell
43+
☑️ Querying MicroPython board...
44+
☑️ Checking if "/app_weather" exists on board
45+
📦 App "Weather Widget" does not exist on board. Creating...
46+
☑️ Creating app "weather" with friendly name "Weather Widget"
47+
☑️ Archiving "weather" on board
48+
☑️ Copying app "weather" archive to local machine
49+
☑️ 🗜️ Extracting "weather_23987.tar" to app_weather
50+
51+
✅ App "Weather Widget" created and available locally
52+
```
53+

shell_tools/_backup.sh

100755100644
Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
source _common.sh
2-
function backup_app {
1+
function transfer_app {
32
app_safe_name=$(echo "$1" | tr -d '\r\n')
43
output_msg="Archiving \"$app_safe_name\" on board"
54
echo -n "$output_msg"
@@ -9,7 +8,6 @@ function backup_app {
98
cmd+="res = export_app('$app_safe_name');print(res)"
109

1110
error=$(mpremote exec "$cmd")
12-
# Print error message if return code is not 0
1311
if [ $? -ne 0 ]; then
1412
echo -ne "\r\033[2K"
1513
echo "$output_msg"
@@ -25,7 +23,6 @@ function backup_app {
2523
echo -n "$output_msg"
2624

2725
error=$(mpremote cp :$remote_archive_path ./)
28-
# Print error message if return code is not 0
2926
if [ $? -ne 0 ]; then
3027
echo -ne "\r\033[2K"
3128
echo "$output_msg"
@@ -37,21 +34,21 @@ function backup_app {
3734

3835
local_folder_name="$APPS_PREFIX$app_safe_name"
3936
if [ -d "$local_folder_name" ]; then
40-
input_msg="Delete local folder $local_folder_name?"
37+
input_msg="Delete local folder $local_folder_name?"
4138
read -p "$input_msg [Y/n]: " confirm
4239
confirm=${confirm:-n}
4340

4441
if [ $confirm == "Y" ]; then
4542
echo -ne "\033[F\r\033[2K"
46-
echo "👍🏼 $input_msg"
43+
echo "☑️ $input_msg"
4744
output_msg="Deleting local folder $local_folder_name"
4845
echo -n "$output_msg"
4946
rm -rf "$local_folder_name"
5047
echo -ne "\r\033[2K"
5148
echo "☑️ $output_msg"
5249
else
5350
echo -ne "\r\033[2K"
54-
echo "👎🏼 $input_msg"
51+
echo " $input_msg"
5552
timestamp=$(date +%s)
5653

5754
local_folder_backup_name="$local_folder_name""_backup_$timestamp"
@@ -66,7 +63,7 @@ function backup_app {
6663

6764

6865
archive_name=`basename $remote_archive_path`
69-
output_msg="Extracting \"$archive_name\" to $APPS_PREFIX$app_safe_name"
66+
output_msg="🗜️ Extracting \"$archive_name\" to $APPS_PREFIX$app_safe_name"
7067
tar --strip-components=1 -xf $archive_name
7168
rm -f $archive_name
7269
# Print error message if return code is not 0
@@ -80,16 +77,3 @@ function backup_app {
8077
echo "☑️ $output_msg"
8178

8279
}
83-
84-
85-
app_safe_name=$1
86-
remote_app_path="$APPS_ROOT""$APPS_PREFIX""$app_safe_name"
87-
echo $remote_app_path
88-
if directory_exists $remote_app_path; then
89-
echo "App \"$app_safe_name\" exists on board. Backing up locally."
90-
backup_app $app_safe_name
91-
else
92-
echo "App \"$app_safe_name\" does not exist on board. Backup canceled."
93-
fi
94-
95-
echo -e "\n✅ App \"$app_safe_name\" backed up and available locally"

shell_tools/_common.sh

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#!/bin/bash
2-
APPS_ROOT="/"
2+
33
APPS_PREFIX="app_"
44
ARDUINO_TOOLS_MIP_URL="github:arduino/arduino-tools-mpy"
55

6-
76
PYTHON_HELPERS='''
87
import os
98
import sys
@@ -73,6 +72,22 @@ except ImportError as e:
7372
7473
'''
7574

75+
function get_apps_root {
76+
output_msg="Extracting board's apps root"
77+
# echo -n "⏳ $output_msg" >&2
78+
get_root="${PYTHON_HELPERS}print(get_root())"
79+
result=$(mpremote exec "$get_root" 2>&1)
80+
# Print result message if return code is not 0
81+
if [ $? -ne 0 ]; then
82+
echo >&2
83+
echo "Error: $error" >&2
84+
exit 1
85+
fi
86+
echo -ne "\r\033[2K" >&2
87+
# echo -e "\r☑️ $output_msg: $result" >&2
88+
echo "$result"
89+
}
90+
7691
function check_arduino_tools {
7792
error=$(mpremote exec "$ARDUINO_TOOLS_CHECK")
7893
if [[ $error == *"Error"* ]]; then
@@ -86,26 +101,31 @@ function check_arduino_tools {
86101
# returns 0 if device is present, 1 if it is not
87102
function device_present {
88103
# Run mpremote and capture the error message
89-
echo "⏳ Querying MicroPython board..."
104+
output="Querying MicroPython board..." >&2
105+
echo -ne "$output" >&2
106+
90107
sys_info="${PYTHON_HELPERS}sys_info()"
91108
error=$(mpremote exec "$sys_info")
109+
echo -ne "\r\033[2K" >&2
110+
echo -e "\r☑️ $output" >&2
92111
# Return error if error message contains "OSError: [Errno 2] ENOENT"
93112
if [[ $error == *"no device found"* ]]; then
94-
return 0
95-
else
96113
return 1
114+
else
115+
return 0
97116
fi
98117
}
99118

100119
function directory_exists {
101120
# Run mpremote and capture the error message
102121
output="Checking if \"$1\" exists on board"
103-
echo -ne "$output"
122+
echo -ne "$output" >&2
104123

105124
error=$(mpremote fs ls $1 2>&1)
106-
echo -ne "\r\033[2K"
107-
echo -e "\r☑️ $output"
125+
echo -ne "\r\033[2K" >&2
126+
echo -e "\r☑️ $output" >&2
108127
# Return error if error message contains "OSError: [Errno 2] ENOENT"
128+
# echo -ne "--- $error" >&2
109129
if [[ $error == *"OSError: [Errno 2] ENOENT"* || $error == *"No such"* ]]; then
110130
return 1
111131
else
@@ -118,15 +138,15 @@ function directory_exists {
118138
# Only produces output if an error occurs
119139
function copy_file {
120140
output="Copying $1 to $2"
121-
echo -n "$output"
141+
echo -n "$output" >&2
122142
# Run mpremote and capture the error message
123143
error=$(mpremote cp $1 $2)
124144
# Print error message if return code is not 0
125145
if [ $? -ne 0 ]; then
126-
echo "Error: $error"
146+
echo "Error: $error" >&2
127147
fi
128-
echo -ne "\r\033[2K"
129-
echo -e "\r☑️ $output"
148+
echo -ne "\r\033[2K" >&2
149+
echo -e "\r☑️ $output" >&2
130150
}
131151

132152
# Deletes a file from the board using mpremote
@@ -144,25 +164,25 @@ function delete_file {
144164

145165
function create_folder {
146166
output_msg="Creating $1 on board"
147-
echo -n "$output_msg"
167+
echo -n "$output_msg" >&2
148168
error=$(mpremote mkdir "$1")
149169
# Print error message if return code is not 0
150170
if [ $? -ne 0 ]; then
151-
echo "Error: $error"
171+
echo "Error: $error" >&2
152172
fi
153-
echo -ne "\r\033[2K"
154-
echo -e "\r☑️ $output_msg"
173+
echo -ne "\r\033[2K" >&2
174+
echo -e "\r☑️ $output_msg" >&2
155175
}
156176

157177
function delete_folder {
158178
output_msg="Deleting $1 on board"
159-
echo -n "$output_msg"
179+
echo -n "$output_msg" >&2
160180
delete_folder="${PYTHON_HELPERS}delete_folder(\"/$1\")"
161181
error=$(mpremote exec "$delete_folder")
162182
# Print error message if return code is not 0
163183
if [ $? -ne 0 ]; then
164-
echo "Error: $error"
184+
echo "Error: $error" >&2
165185
fi
166-
echo -ne "\r\033[2K"
167-
echo -e "\r☑️ $output_msg"
186+
echo -ne "\r\033[2K" >&2
187+
echo -e "\r☑️ $output_msg" >&2
168188
}

shell_tools/_create.sh

100755100644
Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
1-
#!/bin/bash
2-
#
3-
# Create an Arduino MicroPython App on the target board.
4-
#
5-
# ./_create.sh <app_name> <friendly_name>
6-
7-
#########################
8-
# VERY WORK IN PROGRESS #
9-
#########################
10-
source _common.sh
11-
app_name=$1
12-
app_safe_name=$(echo $app_name | tr ' [:punct:]' '_')
13-
shift
14-
app_friendly_name=$*
15-
16-
171
function create_app {
182
app_safe_name=$1
193
shift
20-
app_friendly_name=$*
4+
app_friendly_name=${*:-$app_safe_name}
215

22-
if [ "$app_friendly_name" = "" ]; then
23-
app_friendly_name=$app_safe_name
24-
fi
6+
# if [ "$app_friendly_name" = "" ]; then
7+
# app_friendly_name=$app_safe_name
8+
# fi
259

2610
output_msg="Creating app \"$app_safe_name\" with friendly name \"$app_friendly_name\""
2711
echo -n "$output_msg"
@@ -46,17 +30,3 @@ function create_app {
4630
echo -ne "\r\033[2K"
4731
echo "☑️ $output_msg"
4832
}
49-
50-
remote_app_path="$APPS_ROOT""$APPS_PREFIX""$app_safe_name"
51-
echo $remote_app_path
52-
if directory_exists $remote_app_path; then
53-
echo "App \"$app_friendly_name\" already exists on board. Delete first."
54-
exit 1
55-
else
56-
echo "App \"$app_friendly_name\" does not exist on board. Creating..."
57-
create_app $app_safe_name $app_friendly_name
58-
fi
59-
60-
transfer_app $app_safe_name
61-
62-
echo -e "\n✅ App \"$app_friendly_name\" created and available locally"

0 commit comments

Comments
 (0)