-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathop1go.py
140 lines (115 loc) · 3.73 KB
/
op1go.py
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
#! /usr/bin/python
import os
import sys
import re
import time
import usb.core
import usb.util
import shutil
from datetime import datetime
VENDOR = 0x2367
PRODUCT = 0x0002
USBID_OP1 = "*Teenage_OP-1*"
MOUNT_DIR = "/media/op1"
BACKUP_DIR_FORMAT = "%Y-%m-%d (%H%M%S)"
HOME = "/op1go"
BACKUPS_DIR = os.path.join(HOME, "backups")
# OP-1 connection
def ensure_connection():
if not is_connected():
print("Connect your OP-1 and put it in DISK mode (Shift+COM -> 3)...")
wait_for_connection()
def is_connected():
return usb.core.find(idVendor=VENDOR, idProduct=PRODUCT) is not None
def wait_for_connection():
try:
while True:
time.sleep(1)
if is_connected():
break
except KeyboardInterrupt:
sys.exit(0)
# mounting
def mountdevice(source, target, fs, options=''):
ret = os.system('mount {} {}'.format(source, target))
if ret not in (0, 8192):
raise RuntimeError("Error mounting {} on {}: {}".format(source, target, ret))
def unmountdevice(target):
ret = os.system('umount {}'.format(target))
if ret != 0:
raise RuntimeError("Error unmounting {}: {}".format(target, ret))
def getmountpath():
o = os.popen('readlink -f /dev/disk/by-id/' + USBID_OP1).read()
if USBID_OP1 in o:
raise RuntimeError("Error getting OP-1 mount path: {}".format(o))
else:
return o.rstrip()
# copying
def forcedir(path):
if not os.path.isdir(path):
os.makedirs(path)
def get_visible_folders(d):
return list(filter(lambda x: os.path.isdir(os.path.join(d, x)), get_visible_children(d)))
def get_visible_children(d):
return list(filter(lambda x: x[0] != '.', os.listdir(d)))
def copytree(src, dst, symlinks=False, ignore=None):
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
shutil.copytree(s, d, symlinks, ignore)
else:
shutil.copy2(s, d)
def backup_files(source, destination):
dstroot = os.path.join(destination, datetime.now().strftime(BACKUP_DIR_FORMAT))
forcedir(dstroot)
for node in get_visible_children(source):
src = os.path.join(source, node)
dst = os.path.join(dstroot, node)
print(" . from: {} to {}".format(src, dst))
forcedir(dst)
copytree(src, dst)
blink(1)
# misc
def blink(count):
os.system("echo none | sudo tee /sys/class/leds/led0/trigger >/dev/null 2>&1")
for i in range(0,count):
os.system("echo 0 | sudo tee /sys/class/leds/led0/brightness >/dev/null 2>&1")
time.sleep(0.15)
os.system("echo 1 | sudo tee /sys/class/leds/led0/brightness >/dev/null 2>&1")
time.sleep(0.05)
def blinklong():
os.system("echo none | sudo tee /sys/class/leds/led0/trigger >/dev/null 2>&1")
os.system("echo 0 | sudo tee /sys/class/leds/led0/brightness >/dev/null 2>&1")
time.sleep(1)
os.system("echo 1 | sudo tee /sys/class/leds/led0/brightness >/dev/null 2>&1")
def blinkyay():
os.system("echo none | sudo tee /sys/class/leds/led0/trigger >/dev/null 2>&1")
for i in range(0,1000000):
os.system("echo 0 | sudo tee /sys/class/leds/led0/brightness >/dev/null 2>&1")
time.sleep(0.01)
os.system("echo 1 | sudo tee /sys/class/leds/led0/brightness >/dev/null 2>&1")
time.sleep(0.01)
## Main ##
# create mount point and local backup folders
blinklong()
forcedir(BACKUPS_DIR)
forcedir(MOUNT_DIR)
# wait until OP-1 is connected
print(" > Starting - waiting for OP-1 to connect")
ensure_connection()
time.sleep(5)
# mount OP-1
mountpath = getmountpath()
print(" > OP-1 device path: %s" % mountpath)
mountdevice(mountpath, MOUNT_DIR, 'ext4', 'rw')
print(" > Device mounted at %s" % MOUNT_DIR)
# copy files to local storage
blink(5)
print(" > Copying files...")
backup_files(MOUNT_DIR, BACKUPS_DIR)
# unmount OP-1
print(" > Unmounting OP-1")
unmountdevice(MOUNT_DIR)
print(" > Done.")
blinkyay()