This repository has been archived by the owner on Jan 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathmain.py
352 lines (286 loc) · 10.4 KB
/
main.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# -*- coding: utf-8 -*-
# Copyright (C) 2015-2019 Peter Magnusson <[email protected]>
"""This module is the cli for the Uploader class"""
from __future__ import print_function
import argparse
import logging
import os
import sys
import glob
import serial
from .uploader import Uploader
from .term import terminal
from serial import VERSION as serialversion
from .version import __version__
log = logging.getLogger(__name__) # pylint: disable=C0103
def destination_from_source(sources, use_glob=True):
"""
Split each of the sources in the array on ':'
First part will be source, second will be destination.
Modifies the the original array to contain only sources
and returns an array of destinations.
"""
destinations = []
newsources = []
for i in range(0, len(sources)):
srcdst = sources[i].split(':')
if len(srcdst) == 2:
destinations.append(srcdst[1])
newsources.append(srcdst[0]) # proper list assignment
else:
if use_glob:
listing = glob.glob(srcdst[0])
for filename in listing:
newsources.append(filename)
# always use forward slash at destination
destinations.append(filename.replace('\\', '/'))
else:
newsources.append(srcdst[0])
destinations.append(srcdst[0])
return [newsources, destinations]
def operation_upload(uploader, sources, verify, do_compile, do_file, do_restart):
"""The upload operation"""
if not isinstance(sources, list):
sources = [sources]
sources, destinations = destination_from_source(sources)
if len(destinations) == len(sources):
if uploader.prepare():
for filename, dst in zip(sources, destinations):
if do_compile:
uploader.file_remove(os.path.splitext(dst)[0]+'.lc')
if not os.path.exists(filename) and not os.path.isfile(filename):
raise Exception("File does not exist. {filename}".format(filename=filename))
uploader.write_file(filename, dst, verify)
# init.lua is not allowed to be compiled
if do_compile and dst != 'init.lua':
uploader.file_compile(dst)
uploader.file_remove(dst)
if do_file:
uploader.file_do(os.path.splitext(dst)[0]+'.lc')
elif do_file:
uploader.file_do(dst)
else:
raise Exception('Error preparing nodemcu for reception')
else:
raise Exception('You must specify a destination filename for each file you want to upload.')
if do_restart:
uploader.node_restart()
log.info('All done!')
return destinations
def operation_download(uploader, sources, *args, **kwargs):
"""The download operation"""
sources, destinations = destination_from_source(sources, False)
# print('sources', sources)
# print('destinations', destinations)
dest = kwargs.pop('dest', '')
if len(destinations) == len(sources):
if uploader.prepare():
for filename, dst in zip(sources, destinations):
dst = os.path.join(dest, dst)
uploader.read_file(filename, dst)
else:
raise Exception('You must specify a destination filename for each file you want to download.')
log.info('All done!')
def operation_list_files(uploader):
"""List file on target"""
files = uploader.file_list()
for f in files:
log.info("{file:30s} {size}".format(file=f[0], size=f[1]))
def operation_file(uploader, cmd, filename=''):
"""File operations"""
if cmd == 'list':
operation_list_files(uploader)
if cmd == 'do':
for path in filename:
uploader.file_do(path)
elif cmd == 'format':
uploader.file_format()
elif cmd == 'remove':
for path in filename:
uploader.file_remove(path)
elif cmd == 'print':
for path in filename:
uploader.file_print(path)
elif cmd == 'remove_all':
uploader.file_remove_all()
def operation_port(args):
if args.cmd == 'list':
ports = serial.tools.list_ports.comports(include_links=False)
print('device', 'vid', 'pid')
for p in ports:
print(p.device, p.vid, p.pid)
def arg_auto_int(value):
"""parsing function for integer arguments"""
return int(value, 0)
def main_func():
"""Main function for cli"""
parser = argparse.ArgumentParser(
description='NodeMCU Lua file uploader',
prog='nodemcu-uploader'
)
parser.add_argument(
'--verbose',
help='verbose output',
action='store_true',
default=False)
parser.add_argument(
'--silent',
help='silent output. Errors and worse',
action='store_true',
default=False)
parser.add_argument(
'--version',
help='prints the version and exists',
action='version',
version='%(prog)s {version} (serial {serialversion}, python {pv})'.format(
version=__version__,
serialversion=serialversion,
pv=sys.version)
)
parser.add_argument(
'--port', '-p',
help='Serial port device',
default=Uploader.PORT)
parser.add_argument(
'--baud', '-b',
help='Serial port baudrate',
type=arg_auto_int,
default=Uploader.BAUD)
parser.add_argument(
'--start_baud', '-B',
help='Initial Serial port baudrate',
type=arg_auto_int,
default=Uploader.START_BAUD)
parser.add_argument(
'--timeout', '-t',
help='Timeout for operations',
type=arg_auto_int,
default=Uploader.TIMEOUT)
parser.add_argument(
'--autobaud_time', '-a',
help='Duration of the autobaud timer',
type=float,
default=Uploader.AUTOBAUD_TIME,
)
subparsers = parser.add_subparsers(
dest='operation',
help='Run nodemcu-uploader {command} -h for additional help')
backup_parser = subparsers.add_parser(
'backup',
help='Backup all the files on the nodemcu board')
backup_parser.add_argument('path', help='Folder where to store the backup')
upload_parser = subparsers.add_parser(
'upload',
help='Path to one or more files to be uploaded. Destination name will be the same as the file name.')
upload_parser.add_argument(
'filename',
nargs='+',
help='Lua file to upload. Use colon to give alternate destination.'
)
upload_parser.add_argument(
'--compile', '-c',
help='If file should be uploaded as compiled',
action='store_true',
default=False
)
upload_parser.add_argument(
'--verify', '-v',
help='To verify the uploaded data.',
action='store',
nargs='?',
choices=['none', 'raw', 'sha1'],
default='none'
)
upload_parser.add_argument(
'--dofile', '-e',
help='If file should be run after upload.',
action='store_true',
default=False
)
upload_parser.add_argument(
'--restart', '-r',
help='If esp should be restarted',
action='store_true',
default=False
)
exec_parser = subparsers.add_parser(
'exec',
help='Path to one or more files to be executed line by line.')
exec_parser.add_argument('filename', nargs='+', help='Lua file to execute.')
download_parser = subparsers.add_parser(
'download',
help='Path to one or more files to be downloaded. Destination name will be the same as the file name.')
download_parser.add_argument(
'filename',
nargs='+',
help='Lua file to download. Use colon to give alternate destination.')
file_parser = subparsers.add_parser(
'file',
help='File functions')
file_parser.add_argument(
'cmd',
choices=('list', 'do', 'format', 'remove', 'print', 'remove_all'),
help="""list=list files, do=dofile given path, format=formate file area,
remove=remove given path, remove_all=delete all files""")
file_parser.add_argument('filename', nargs='*', help='path for cmd')
node_parse = subparsers.add_parser(
'node',
help='Node functions')
node_parse.add_argument(
'ncmd',
choices=('heap', 'restart', 'info'),
help="heap=print heap memory, restart=restart nodemcu, info=show node info")
subparsers.add_parser(
'terminal',
help='Run pySerials miniterm'
)
port_parser = subparsers.add_parser(
'port',
help='serial port stuff'
)
port_parser.add_argument(
'cmd',
choices=('list',)
)
args = parser.parse_args()
default_level = logging.INFO
if args.silent:
default_level = logging.ERROR
if args.verbose:
default_level = logging.DEBUG
# formatter = logging.Formatter('%(message)s')
logging.basicConfig(level=default_level, format='%(message)s')
if args.operation == 'terminal':
# uploader can not claim the port
terminal(args.port, str(args.start_baud))
return
elif args.operation == 'port':
operation_port(args)
return
# let uploader user the default (short) timeout for establishing connection
uploader = Uploader(args.port, args.baud, start_baud=args.start_baud, autobaud_time=args.autobaud_time)
# and reset the timeout (if we have the uploader&timeout)
if args.timeout:
uploader.set_timeout(args.timeout)
if args.operation == 'upload':
operation_upload(uploader, args.filename, args.verify, args.compile, args.dofile,
args.restart)
elif args.operation == 'download':
operation_download(uploader, args.filename)
elif args.operation == 'exec':
sources = args.filename
for path in sources:
uploader.exec_file(path)
elif args.operation == 'file':
operation_file(uploader, args.cmd, args.filename)
elif args.operation == 'node':
if args.ncmd == 'heap':
uploader.node_heap()
elif args.ncmd == 'restart':
uploader.node_restart()
elif args.ncmd == 'info':
uploader.node_info()
elif args.operation == 'backup':
uploader.backup(args.path)
# no uploader related commands after this point
uploader.close()