Skip to content

Instantly share code, notes, and snippets.

@KokoseiJ
Created February 21, 2023 19:35
Show Gist options
  • Save KokoseiJ/cbacf48bdc24e060386251a29aae4914 to your computer and use it in GitHub Desktop.
Save KokoseiJ/cbacf48bdc24e060386251a29aae4914 to your computer and use it in GitHub Desktop.
Stepmania USB Profiile Setup Script
#!/bin/env python3
import os
import re
def get_usbdevices(exclude=[]):
usbs = [x for x in os.listdir("/dev/disk/by-path") if "usb" in x]
usbs = sanitize_parts(usbs)
return [x for x in usbs if x not in exclude]
def sanitize_parts(usbs):
return tuple(set([re.sub(r"-part[0-9]+", "", x) for x in usbs]))
def detect_device(prompt, default_devices):
devices = []
while not devices:
input(prompt)
devices = get_usbdevices(default_devices)
if not devices:
print("No device has been detected. Please try again.")
elif len(devices) > 1:
print("Multiple devices detected. Are you sure you've only plugged in a single device?")
print("Detected devices:\n" + "\n".join(devices))
devices = []
return devices[0]
def gen_line(device, point):
return f"/dev/disk/by-path/{device} \t/mnt/{point} auto rw,user,noauto,noatime 0 0\n"
def gen_fstab(p1, p2):
return f"""\
# Added by Stepmania USB Profile Config Utility
{gen_line(p1, "p1")}
{gen_line(p1+"-part1", "p1")}
{gen_line(p2, "p2")}
{gen_line(p2+"-part1", "p2")}
"""
def main():
print("""\
****************************
* USB Profile Setup Script *
*
* Written by Kokosei J *
****************************
Please ensure that you have NO USB DRIVE PLUGGED IN prior to setting up the USB.
This script requires a working USB drive, in order to determine the port that will be used.
""")
input("When you are ready, press Enter to continue.")
default_devices = get_usbdevices()
print("\nDetected devices (Blacklisted):\n" + "\n".join(default_devices))
p1 = detect_device("\nPlug in your USB drive to P1 port, and press enter.", default_devices)
print(f"\nDetected device: {p1}\nPlease unplug the device now.")
default_devices.append(p1)
p2 = detect_device("\nPlug in your USB drive to P2 port, and press enter.", default_devices)
print(f"\nDetected device: {p2}\nPlease unplug the device now.")
fstab = gen_fstab(p1, p2)
print(f"\nP1: {p1}\nP2:{p2}")
print(f"fstab entry:\n{fstab}")
input("Press enter to write the result to fstab. Press Ctrl+C to cancel and exit.")
with open("/etc/fstab", "a") as f:
f.write(fstab)
print("Done.")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment