transfer.py 1.6 KB
import psutil
import urllib.request
import os
import shutil
import platform

import subprocess


def get_windows_drive_name(letter):
    return subprocess.check_output(["cmd", "/c vol " + letter]).decode().split("\r\n")[0].split(" ").pop()


if platform.system().lower() == 'windows':
    disks = [(x.mountpoint, get_windows_drive_name(x.mountpoint.strip('\\')))
             for x in psutil.disk_partitions()]
    disks = [(x, name)
             for x, name in disks if 'TRINKEY' in name or 'CIRCUIT' in name]
else:
    disks = [(x.mountpoint, x.mountpoint) for x in psutil.disk_partitions(
    ) if 'TRINKEY' in x.mountpoint.upper() or 'CIRCUIT' in x.mountpoint.upper()]

if len(disks) != 1:
    print("Could not find the trinkey.  Did you click the reset button twice and connect it?")
    exit(1)

disk, name = disks[0]

if 'TRINKEY' in name:
    urllib.request.urlretrieve(
        "https://downloads.circuitpython.org/bin/adafruit_proxlight_trinkey_m0/en_US/adafruit-circuitpython-adafruit_proxlight_trinkey_m0-en_US-9.1.4.uf2", os.path.join(
            disk, 'trinkey.uf2'))
    print("The drive will now eject. You can quit any keyboard popups that appear. Run the transfer.py file again for it to be bootable.")
else:
    shutil.rmtree(os.path.join(disk, 'lib'), True)
    shutil.rmtree(os.path.join(disk, 'src'), True)
    shutil.rmtree(os.path.join(disk, 'boot_out.txt'), True)
    shutil.rmtree(os.path.join('src', '__pycache__'), True)
    shutil.copytree('src', os.path.join(disk, 'src'), dirs_exist_ok=True)
    shutil.copy('code.py', os.path.join(disk, 'code.py'))
    print("Done! Switch back to the Serial Monitor!")