Repository navigation
Expand file tree
/
Copy pathupyfile.py
More file actions
527 lines (438 loc) · 13.8 KB
/
Copy pathupyfile.py
File metadata and controls
527 lines (438 loc) · 13.8 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
import argparse, logging, os, serial, sys, time
_version = '3.3.0.dev'
bufferSize = 1024 # Buffer size for serial data
batchSize = 1024 # Batch size for command data
debug = True
verbose = True
portOpen = False
flowControl = True # Some devices' USB/serial interfaces don't implement flow control
flowDelay = -1.0
pcLog = logging.getLogger('PC')
devLog = logging.getLogger('DEV')
port = None
# Serial port management
def openPort(portName, baud = 115200, timeout = 0.2, stopBits = 1):
global portOpen, port
pcLog.debug('Opening serial port')
port = serial.Serial(
port = portName,
baudrate = baud,
bytesize = 8,
timeout = timeout,
stopbits = stopBits
)
portOpen = True
def closePort():
if portOpen:
pcLog.debug('Closing serial port')
port.close()
else:
pcLog.debug('Serial port already closed')
def readPort(wait=True, shush=False):
if wait:
while port.in_waiting == 0:
time.sleep(0.05)
data = port.readline()
# Clutters debug when the stub is sent, thus the shush flag
if not shush: devLog.debug(data)
return data
def writePort(data):
pcLog.debug(data)
# Write the data in batches to ensure the buffer never overflows
remaining = data
while len(remaining) > bufferSize:
port.write(remaining[0:bufferSize])
remaining = remaining[bufferSize:]
if flowControl: port.flush()
else: time.sleep(flowDelay)
if len(remaining):
port.write(remaining)
if flowControl: port.flush()
else: time.sleep(flowDelay)
# Communication management
def waitFor(line, shush=False, dumpOnTraceback=True):
pcLog.info('Waiting for {}'.format(line))
devOutput = b''
tracebackSequence = b'Traceback (most recent call last):\r\n'
tracebackDoneSequence = b'>>> '
while devOutput != line:
devOutput = readPort(shush=shush)
if dumpOnTraceback and devOutput == tracebackSequence:
break
else: # If loop didn't break, no traceback was caught
pcLog.info('Done waiting')
return
devLog.error('Device error')
traceback = devOutput
for i in range(10):
if devOutput == tracebackDoneSequence: break
devOutput = readPort(shush=shush)
traceback = traceback + devOutput
devLog.error('Device traceback:\n{}'.format(traceback.decode('utf-8', errors='replace')))
sys.exit(1)
def verifyResponse():
respRaw = readPort().strip()
resp = respRaw.split(b':')
if resp[0] == b'~~error':
devLog.error(resp[1]); sys.exit(1)
elif resp[0] == b'~~complete':
pcLog.debug('Response ok')
else:
pcLog.error('Unexpected response: {}'.format(respRaw))
time.sleep(0.1) # Give the device 100ms to get the rest of the response out
badResp = b''
while port.in_waiting > 0:
badResp = badResp + port.readline()
time.sleep(0.05) # Continue waiting each batch for 50ms to get any that's left.
pcLog.error(badResp)
sys.exit(1)
# Command implementation
def _readFile(file):
# Stub commands:
# read file
# readbuf
# ...
# Send read command
cmd = b'read ' + file.encode() + b'\r\n'
writePort(cmd)
waitFor(b'~~recvd:\r\n')
verifyResponse()
dataRaw = b''
while True:
# Send readbuf command
cmd = b'readbuf' + b'\r\n'
writePort(cmd)
waitFor(b'~~recvd:\r\n')
# Process response
respRaw = readPort()
if respRaw == b'~~error:no data\r\n': pcLog.debug('No data, breaking loop'); break
else: dataRaw += respRaw.strip()
respRaw = readPort()
if respRaw != b'~~complete:\r\n':
devLog.error('Unexpected response: {}'.format(respRaw)); sys.exit(1)
# Decode data
data = b''
while len(dataRaw):
data += int('0x' + dataRaw[0:2].decode(), base=16).to_bytes(1, 'big')
dataRaw = dataRaw[2:]
return data
def cmd_init(args):
pcLog.debug('Loading stub')
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'stub.py'), 'rb') as stubFile: stub = stubFile.read().replace(b'\n', b'\r\n')
print('Initializing device')
writePort(b'\x03') # Ctrl+C
waitFor(b'>>> ')
writePort(b'\x05') # Ctrl+E
writePort(stub)
writePort(b'\x04') # Ctrl+D
waitFor(b'ready~')
pcLog.info('Sent stub')
cmd = ('batch {}\r\n'.format(batchSize)).encode()
writePort(cmd)
waitFor(b'~~recvd:\r\n')
verifyResponse()
def cmd_cd(args):
# Stub commands:
# cd
# Send cd command
cmd = b'cd ' + args.dir.encode() + b'\r\n'
writePort(cmd)
waitFor(b'~~recvd:\r\n')
verifyResponse()
def cmd_pwd(args):
# Stub commands:
# pwd
# Send pwd command
cmd = b'pwd\r\n'
writePort(cmd)
waitFor(b'~~recvd:\r\n')
verifyResponse()
dataRaw = b''
while True:
# Send readbuf command
cmd = b'readbuf' + b'\r\n'
writePort(cmd)
waitFor(b'~~recvd:\r\n')
# Process response
respRaw = readPort()
if respRaw == b'~~error:no data\r\n': pcLog.debug('No data, breaking loop'); break
else: dataRaw += respRaw.strip()
respRaw = readPort()
if respRaw != b'~~complete:\r\n':
devLog.error('Unexpected response: {}'.format(respRaw)); sys.exit(1)
# Decode data
data = b''
while len(dataRaw):
data += int('0x' + dataRaw[0:2].decode(), base=16).to_bytes(1, 'big')
dataRaw = dataRaw[2:]
print(data.decode())
def cmd_list(args):
# Stub commands:
# list
# Send list command
cmd = b'list ' + args.dir.encode() + b'\r\n'
writePort(cmd)
waitFor(b'~~recvd:\r\n')
verifyResponse()
dataRaw = b''
while True:
# Send readbuf command
cmd = b'readbuf' + b'\r\n'
writePort(cmd)
waitFor(b'~~recvd:\r\n')
# Process response
respRaw = readPort()
if respRaw == b'~~error:no data\r\n': pcLog.debug('No data, breaking loop'); break
else: dataRaw += respRaw.strip()
respRaw = readPort()
if respRaw != b'~~complete:\r\n':
devLog.error('Unexpected response: {}'.format(respRaw)); sys.exit(1)
# Decode data
data = b''
while len(dataRaw):
data += int('0x' + dataRaw[0:2].decode(), base=16).to_bytes(1, 'big')
dataRaw = dataRaw[2:]
listing = data.decode()
print('Contents of directory {} are:'.format(args.dir))
print(listing)
def cmd_ilist(args):
# Stub commands:
# ilist
# Send ilist command
cmd = b'ilist ' + args.dir.encode() + b'\r\n'
writePort(cmd)
waitFor(b'~~recvd:\r\n')
verifyResponse()
dataRaw = b''
while True:
# Send readbuf command
cmd = b'readbuf' + b'\r\n'
writePort(cmd)
waitFor(b'~~recvd:\r\n')
# Process response
respRaw = readPort()
if respRaw == b'~~error:no data\r\n': pcLog.debug('No data, breaking loop'); break
else: dataRaw += respRaw.strip()
respRaw = readPort()
if respRaw != b'~~complete:\r\n':
devLog.error('Unexpected response: {}'.format(respRaw)); sys.exit(1)
# Decode data
data = b''
while len(dataRaw):
data += int('0x' + dataRaw[0:2].decode(), base=16).to_bytes(1, 'big')
dataRaw = dataRaw[2:]
listing = data.decode()
print('Contents of directory {} are:'.format(args.dir))
print(listing)
def cmd_mkdir(args):
# Stub commands:
# mkdir
# Send mkdir command
cmd = b'mkdir ' + args.path.encode() + b'\r\n'
writePort(cmd)
waitFor(b'~~recvd:\r\n')
verifyResponse()
def cmd_rmdir(args):
# Stub commands:
# mkdir
# Send rmdir command
cmd = b'rmdir ' + args.path.encode() + b'\r\n'
writePort(cmd)
waitFor(b'~~recvd:\r\n')
verifyResponse()
def cmd_read(args):
data = _readFile(args.file)
print(data.decode('UTF-8', errors = 'ignore'))
def cmd_push(args):
with open(args.infile, 'rb') as infile: data = infile.read()
# Stub commands:
# writebuf
# ...
# write file
while len(data):
# Encode data
dataHex = ''
dataBatch = data[0:batchSize]; data = data[batchSize:]
for i in dataBatch:
dataHex += '{:02X}'.format(i)
dataHex = dataHex.encode()
# Send writebuf command
cmd = b'writebuf ' + dataHex + b'\r\n'
writePort(cmd)
waitFor(b'~~recvd:\r\n')
verifyResponse()
# Send write command
cmd = b'write ' + args.outfile.encode() + b'\r\n'
writePort(cmd)
waitFor(b'~~recvd:\r\n')
verifyResponse()
def cmd_pull(args):
data = _readFile(args.infile)
with open(args.outfile, 'wb') as filePC:
filePC.write(data)
def cmd_ren(args):
# Stub commands:
# ren
# Send ren command
cmd = b'ren ' + args.old.encode() + b' ' + args.new.encode() + b'\r\n'
writePort(cmd)
waitFor(b'~~recvd:\r\n')
verifyResponse()
def cmd_rm(args):
# Stub commands:
# rm
# Send rm command
cmd = b'rm ' + args.file.encode() + b'\r\n'
writePort(cmd)
waitFor(b'~~recvd:\r\n')
verifyResponse()
# Main section
if __name__ == '__main__':
rootParser = argparse.ArgumentParser(prog='uPyFile', description='uPyfile {}: A file transfer utility for MicroPython devices'.format(_version))
rootParser.add_argument(
'-p',
'--port',
help='Port the device is on',
type=str
)
rootParser.add_argument(
'-b',
'--baud',
help='Baudrate to use',
type=int,
default=115200
)
rootParser.add_argument(
'-t',
'--timeout',
help='Timeout (in seconds) for serial comms',
type=float,
default=0.2
)
rootParser.add_argument(
'-f',
'--flow-delay',
help='Flow control delay (in seconds) - omit to use hardware flow control (default)',
type=float,
default=None
)
rootParser.add_argument(
'-B',
'--buffer',
help='Buffer size',
type=int,
default=1024
)
rootParser.add_argument(
'-v',
'--verbose',
help='Enable verbose output',
action='store_true'
)
subParsers = rootParser.add_subparsers(dest='command')
subParsers.required = True
initParser = subParsers.add_parser('init', help='Initialize a device')
initParser.set_defaults(function=cmd_init)
cdParser = subParsers.add_parser('cd', help='Change the working directory')
cdParser.set_defaults(function=cmd_cd)
cdParser.add_argument(
'dir',
help='What to change the working directory to',
type=str
)
pwdParser = subParsers.add_parser('pwd', help='Print the working directory')
pwdParser.set_defaults(function=cmd_pwd)
listParser = subParsers.add_parser('list', help='List a directory on a device (names only)')
listParser.set_defaults(function=cmd_list)
listParser.add_argument(
'dir',
help='Directory to list',
type=str
)
listParser = subParsers.add_parser('ilist', help='List a directory on a device (names and attributes)')
listParser.set_defaults(function=cmd_ilist)
listParser.add_argument(
'dir',
help='Directory to list',
type=str
)
mkdirParser = subParsers.add_parser('mkdir', help='Make a directory on a device')
mkdirParser.set_defaults(function=cmd_mkdir)
mkdirParser.add_argument(
'path',
help='Path at which to create a directory',
type=str
)
rmdirParser = subParsers.add_parser('rmdir', help='Remove a directory on a device')
rmdirParser.set_defaults(function=cmd_rmdir)
rmdirParser.add_argument(
'path',
help='Path at which to create a directory',
type=str
)
readParser = subParsers.add_parser('read', help='Read a file from a device')
readParser.set_defaults(function=cmd_read)
readParser.add_argument(
'file',
help='File to read',
type=str
)
pushParser = subParsers.add_parser('push', help='Push a file to a device')
pushParser.set_defaults(function=cmd_push)
pushParser.add_argument(
'infile',
help='File on PC to push',
type=str
)
pushParser.add_argument(
'outfile',
help='File on device to push to',
type=str
)
pullParser = subParsers.add_parser('pull', help='Pull a file from a device')
pullParser.set_defaults(function=cmd_pull)
pullParser.add_argument(
'infile',
help='File on device to pull',
type=str
)
pullParser.add_argument(
'outfile',
help='File on PC to pull to',
type=str
)
renParser = subParsers.add_parser('ren', help='Rename a file or directory')
renParser.set_defaults(function=cmd_ren)
renParser.add_argument(
'old',
help='File to delete',
type=str
)
renParser.add_argument(
'new',
help='File to delete',
type=str
)
rmParser = subParsers.add_parser('rm', help='Remove a file')
rmParser.set_defaults(function=cmd_rm)
rmParser.add_argument(
'file',
help='File to delete',
type=str
)
args = rootParser.parse_args()
if args.verbose: logging.basicConfig(level=logging.DEBUG)
else: logging.basicConfig(level=logging.WARNING)
if args.flow_delay is None:
flowControl = True
pcLog.debug('Using hardware flow control')
else:
flowControl = False
flowDelay = args.flow_delay
pcLog.debug('Using constant delay flow control ({}s)'.format(flowDelay))
bufferSize = args.buffer
try:
openPort(args.port, baud=args.baud, timeout=args.timeout) # Make the serial connection
args.function(args) # Run the command
finally:
closePort()