Repository navigation
Expand file tree
/
Copy pathstub.py
More file actions
155 lines (122 loc) · 4.83 KB
/
Copy pathstub.py
File metadata and controls
155 lines (122 loc) · 4.83 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
# This is the stub program which is downloaded to the uPython device. It receives commands from the host via stdin (REPL UART link).
import os, machine
buffer = b''
batch = 1024
runFlag = True
while runFlag:
cmdText = input('\nready~')
cmd, *params = cmdText.strip().split()
print('~~recvd:')
if cmd == 'cd':
if len(params) < 1:
print('~~error:missing params'); continue
try: os.chdir(params[0])
except Exception as E: print('~~error:{}'.format(repr(E))); continue
print('~~complete:')
elif cmd == 'pwd':
try: buffer = os.getcwd().encode()
except Exception as E: print('~~error:{}'.format(repr(E))); continue
print('~~complete:')
elif cmd == 'list':
if len(params) < 1:
print('~~error:missing params'); continue
try: buffer = '\n'.join(os.listdir(params[0])).encode()
except Exception as E: print('~~error:{}'.format(repr(E))); continue
print('~~complete:')
elif cmd == 'ilist':
if len(params) < 1:
print('~~error:missing params'); continue
try: listing = os.ilistdir(params[0])
except Exception as E: print('~~error:{}'.format(repr(E))); continue
entries = []
# No MicroPython device will likely ever be able to surpass a few GB files, but just to be safe...
suffixes = ('B', 'kB', 'MB', 'GB', 'TB', 'PB')
sizeDiv = 1024
itemNameLen = 0
for entry in listing:
if entry[1] == 0x8000: itemType = 'f'
elif entry[1] == 0x4000: itemType = 'd'
else: itemType = '?'
itemName = entry[0]
itemNameLen = max(itemNameLen, len(itemName))
if len(entry) >= 4 and itemType == 'f':
itemSize = entry[3]
s = 0
while itemSize >= sizeDiv:
itemSize //= sizeDiv
s += 1
itemSize = str(itemSize) + suffixes[s]
else:
itemSize = '---'
entries.append((itemType, itemName, itemSize))
for e, entry in enumerate(entries):
itemType, itemName, itemSize = entry
entries[e] = '{} : {}{} : {}'.format(itemType, itemName, ' ' * (itemNameLen - len(itemName)), itemSize)
buffer = '\n'.join(entries).encode()
print('~~complete:')
elif cmd == 'mkdir':
if len(params) < 1:
print('~~error:missing params'); continue
try: os.mkdir(params[0])
except Exception as E: print('~~error:{}'.format(repr(E))); continue
print('~~complete:')
elif cmd == 'rmdir':
if len(params) < 1:
print('~~error:missing params'); continue
try: os.rmdir(params[0])
except Exception as E: print('~~error:{}'.format(repr(E))); continue
print('~~complete:')
elif cmd == 'read':
if len(params) < 1:
print('~~error:missing params'); continue
try: file = open(params[0], 'rb'); buffer = file.read(); file.close()
except Exception as E: print('~~error:{}'.format(repr(E))); continue
print('~~complete:')
elif cmd == 'write':
if len(params) < 1:
print('~~error:missing params'); continue
try: file = open(params[0], 'wb'); null = file.write(buffer); file.close(); buffer = b''
except Exception as E: print('~~error:{}'.format(repr(E))); continue
print('~~complete:')
elif cmd == 'ren':
if len(params) < 2:
print('~~error:missing params'); continue
try: os.rename(params[0], params[1])
except Exception as E: print('~~error:{}'.format(repr(E))); continue
print('~~complete:')
elif cmd == 'rm':
if len(params) < 1:
print('~~error:missing params'); continue
try: os.remove(params[0])
except Exception as E: print('~~error:{}'.format(repr(E))); continue
print('~~complete:')
elif cmd == 'readbuf':
if len(buffer):
data = buffer[0:batch]
buffer = buffer[batch:]
print(''.join(['{:02X}'.format(d) for d in data]))
print('~~complete:')
else:
print('~~error:no data'); continue
elif cmd == 'writebuf':
if len(params) < 1:
print('~~error:missing params'); continue
dataHex = params[0]
while len(dataHex):
buffer += int('0x' + dataHex[0:2]).to_bytes(1, 'big')
dataHex = dataHex[2:]
print('~~complete:')
elif cmd == 'clearbuf':
buffer = b''
print('~~complete:')
elif cmd == 'batch':
batch = int(params[0])
print('~~complete:')
elif cmd == 'quit':
runFlag = False
print('~~complete:')
elif cmd == 'reboot':
print('~~complete:')
machine.reset()
else:
print('~~error:bad command')