1
+ #! /bin/bash
2
+ APPS_ROOT=" /"
3
+ APPS_PREFIX=" app_"
4
+ ARDUINO_TOOLS_MIP_URL=" github:arduino/arduino-tools-mpy"
5
+
6
+
7
+ PYTHON_HELPERS=' ' '
8
+ import os
9
+
10
+ os.chdir(get_root())
11
+ from arduino_tools.apps_manager import create_app, export_app
12
+
13
+ def is_directory(path):
14
+ return True if os.stat(path)[0] == 0x4000 else False
15
+
16
+ def get_all_files(path, array_of_files = []):
17
+ files = os.ilistdir(path)
18
+ for file in files:
19
+ is_folder = file[1] == 16384
20
+ p = path + "/" + file[0]
21
+ array_of_files.append({
22
+ "path": p,
23
+ "type": "folder" if is_folder else "file"
24
+ })
25
+ if is_folder:
26
+ array_of_files = get_all_files(p, array_of_files)
27
+ return array_of_files
28
+
29
+ def delete_folder(path):
30
+ files = get_all_files(path)
31
+ for file in files:
32
+ if file["type"] == "file":
33
+ os.remove(file["path"])
34
+ for file in reversed(files):
35
+ if file["type"] == "folder":
36
+ os.rmdir(file["path"])
37
+ os.rmdir(path)
38
+
39
+ def sys_info():
40
+ import sys
41
+ print(sys.platform, sys.implementation.version)
42
+
43
+ def list_apps():
44
+ import os
45
+ from arduino_tools.apps_manager import get_apps_list
46
+ apps = get_apps_list()
47
+ apps_names = ""
48
+ for i, app in enumerate(apps):
49
+ if i != 0:
50
+ apps_names += " "
51
+ apps_names += app["name"]
52
+ print(apps_names)
53
+
54
+ def get_root(has_flash_mount = True):
55
+ if "/flash" in sys.path:
56
+ return "/flash/"
57
+ else:
58
+ return "/"
59
+
60
+ os.chdir(get_root())
61
+ ' ' '
62
+
63
+ ARDUINO_TOOLS_CHECK=' ' '
64
+ import sys
65
+ sys.path.insert(0, "/lib")
66
+ try:
67
+ from arduino_tools.apps_manager import create_app
68
+ from tarfile import write
69
+ except ImportError as e:
70
+ print("Error: ")
71
+
72
+ ' ' '
73
+
74
+ function check_arduino_tools {
75
+ error=$( mpremote exec " $ARDUINO_TOOLS_CHECK " )
76
+ if [[ $error == * " Error" * ]]; then
77
+ return 1
78
+ else
79
+ return 0
80
+ fi
81
+ }
82
+
83
+ # Check if device is present/connectable
84
+ # returns 0 if device is present, 1 if it is not
85
+ function device_present {
86
+ # Run mpremote and capture the error message
87
+ echo " ⏳ Querying MicroPython board..."
88
+ sys_info=" ${PYTHON_HELPERS} sys_info()"
89
+ error=$( mpremote exec " $sys_info " )
90
+ # Return error if error message contains "OSError: [Errno 2] ENOENT"
91
+ if [[ $error == * " no device found" * ]]; then
92
+ return 0
93
+ else
94
+ return 1
95
+ fi
96
+ }
97
+
98
+ function directory_exists {
99
+ # Run mpremote and capture the error message
100
+ output=" Checking if \" $1 \" exists on board"
101
+ echo -ne " ❔ $output "
102
+
103
+ error=$( mpremote fs ls $1 2>&1 )
104
+ echo -ne " \r\033[2K"
105
+ echo -e " \r☑️ $output "
106
+ # Return error if error message contains "OSError: [Errno 2] ENOENT"
107
+ if [[ $error == * " OSError: [Errno 2] ENOENT" * || $error == * " No such" * ]]; then
108
+ return 1
109
+ else
110
+ return 0
111
+ fi
112
+
113
+ }
114
+
115
+ # Copies a file to the board using mpremote
116
+ # Only produces output if an error occurs
117
+ function copy_file {
118
+ output=" Copying $1 to $2 "
119
+ echo -n " ⏳ $output "
120
+ # Run mpremote and capture the error message
121
+ error=$( mpremote cp $1 $2 )
122
+ # Print error message if return code is not 0
123
+ if [ $? -ne 0 ]; then
124
+ echo " Error: $error "
125
+ fi
126
+ echo -ne " \r\033[2K"
127
+ echo -e " \r☑️ $output "
128
+ }
129
+
130
+ # Deletes a file from the board using mpremote
131
+ # Only produces output if an error occurs
132
+ function delete_file {
133
+ echo " Deleting $1 "
134
+ # Run mpremote and capture the error message
135
+ error=$( mpremote rm $1 )
136
+
137
+ # Print error message if return code is not 0
138
+ if [ $? -ne 0 ]; then
139
+ echo " Error: $error "
140
+ fi
141
+ }
142
+
143
+ function create_folder {
144
+ output_msg=" Creating $1 on board"
145
+ echo -n " ⏳ $output_msg "
146
+ error=$( mpremote mkdir " $1 " )
147
+ # Print error message if return code is not 0
148
+ if [ $? -ne 0 ]; then
149
+ echo " Error: $error "
150
+ fi
151
+ echo -ne " \r\033[2K"
152
+ echo -e " \r☑️ $output_msg "
153
+ }
154
+
155
+ function delete_folder {
156
+ output_msg=" Deleting $1 on board"
157
+ echo -n " ⏳ $output_msg "
158
+ delete_folder=" ${PYTHON_HELPERS} delete_folder(\" /$1 \" )"
159
+ error=$( mpremote exec " $delete_folder " )
160
+ # Print error message if return code is not 0
161
+ if [ $? -ne 0 ]; then
162
+ echo " Error: $error "
163
+ fi
164
+ echo -ne " \r\033[2K"
165
+ echo -e " \r☑️ $output_msg "
166
+ }
0 commit comments