release.py
1.91 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
import serial
import io
import os
import sys
######################################
# EDIT THIS TO MATCH YOUR SETTINGS
PORT = 'COM17'
IPADDRESS = '192.168.1.189'
######################################
BAUDRATE = 115200
folders = ['help', 'rooms', 'rooms/town', 'rooms/wilderness', 'inventory', 'commands', 'mobs']
files = [
"welcome.txt",
"defaultplayer.json",
"main.py"
]
def run_command(sio, command, expected='>>>'):
sio.write("{}\n".format(command))
sio.flush() # it is buffering. required to get the data out *now*
res = ''
while expected not in res:
try:
res = sio.readline()
except UnicodeDecodeError:
return ''
return res
with serial.Serial(PORT, BAUDRATE, timeout=1) as ser:
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))
run_command(sio, '\x03')
run_command(sio, 'import os')
root = run_command(sio, 'os.listdir()', expected=']')
if 'EEXIST' in root:
print('Folders already created.')
else:
print('Creating folders.')
# we are missing folders so they need to be created.
tmp_folders = folders
tmp_folders.append('players')
for folder in tmp_folders:
if folder in root:
continue
print('Creating folder: {}'.format(folder))
run_command(sio, 'os.mkdir("{}")'.format(folder))
for folder in folders:
for f in os.listdir(folder):
if not os.path.isdir(f):
files.append('{}/{}'.format(folder, f))
with open('releasepw.conf', 'r', encoding='utf-8') as f:
password = f.read()
for file in files:
# print("python webrepl\\webrepl_cli.py -p {} {} {}:/{}".format(password, file, IPADDRESS, file))
os.system("python webrepl\\webrepl_cli.py -p {} {} {}:/{}".format(password, file, IPADDRESS, file))
print("Rebooting via machine reset")
run_command(sio, 'import machine;machine.reset()')