release.py 1.9 KB
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', '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()')