-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathresign.py
executable file
·111 lines (82 loc) · 3.94 KB
/
resign.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
#!/usr/bin/env python3
# set this to your resync-executable (if it's in path, its name should suffice)
resync_cmd = 'resync.py'
import sys, os, subprocess, pathlib, tempfile, shutil, argparse, json
parser = argparse.ArgumentParser(description='Relay documents over your reMarkable for signing')
parser.add_argument('-r', '--remote-address', action='store', default='10.11.99.1', dest='ssh_destination', metavar='<IP or hostname>', help='remote address of the reMarkable')
parser.add_argument('documents', metavar='documents', type=str, nargs='*', help='Documents and folders to be signed')
args = parser.parse_args()
prepdir = pathlib.Path(tempfile.mkdtemp())
ssh_socketfile = '/tmp/remarkable-push.socket'
ssh_connection = subprocess.Popen(f'ssh -o ConnectTimeout=1 -M -N -q -S {ssh_socketfile} root@{args.ssh_destination}', shell=True)
# quickly check if we actually have a functional ssh connection (might not be the case right after an update)
checkmsg = subprocess.getoutput(f'ssh -S {ssh_socketfile} root@{args.ssh_destination} "/bin/true"')
if checkmsg != "":
print("ssh connection does not work, verify that you can manually ssh into your reMarkable. ssh itself commented the situation with:")
print(checkmsg)
ssh_connection.terminate()
sys.exit(255)
docs = [pathlib.Path(p) for p in args.documents]
targetfiles = []
for doc in docs:
destname = 'sign_' + doc.name
shutil.copy(doc, prepdir/destname)
targetfiles.append(destname)
pushdocs = [prepdir/doc for doc in os.listdir(prepdir)]
try:
subprocess.call([resync_cmd, '--remote-address', args.ssh_destination, 'push'] + pushdocs)
except FileNotFoundError:
print("Could not locate {resync_cmd}, maybe it's not in your path?", file=sys.stderr)
sys.exit(1)
for doc in pushdocs:
os.remove(doc)
input("Now sign all documents and press enter once you're done.")
subprocess.call([resync_cmd, '--remote-address', args.ssh_destination, '-o', prepdir, 'pull'] + targetfiles)
for f in targetfiles:
destname = os.path.splitext(f[5:])[0] + '_signed.pdf'
shutil.move(prepdir/f, destname)
shutil.rmtree(prepdir)
#######################################################################
#
# now let's clean up after ourselves on the remarkable
# this is a bit more elaborate as we need to fumble on the remarkable
#
#######################################################################
def get_uuid_by_visibleName(name):
"""
retrieves uuid for all given documents that have the given name set as visibleName
"""
#pattern = f'"visibleName": "{name}"'
cmd = f'ssh -S {ssh_socketfile} root@{args.ssh_destination} "grep -lF \'\\"visibleName\\": \\"{name}\\"\' .local/share/remarkable/xochitl/*.metadata"'
res = subprocess.getoutput(cmd)
uuid_candidates = []
if res != '':
for result in res.split('\n'):
try:
# hard pattern matching to provoke a mismatch-exception on the first number mismatch
_, _, _, _, filename = result.split('/')
except ValueError:
continue
u, _ = filename.split('.')
raw_metadata = subprocess.getoutput(f'ssh -S {ssh_socketfile} root@{args.ssh_destination} "cat .local/share/remarkable/xochitl/{u}.metadata"')
try:
metadata = json.loads(raw_metadata)
except json.decoder.JSONDecodeError:
continue
if metadata is not None and metadata['parent'] == '':
uuid_candidates.append(u)
if len(uuid_candidates) > 1:
print("Document {name} was found multiple times, not cleaning it up, delete manually")
elif len(uuid_candidates) < 1:
print("Document {name} was not found, unable to clean it up")
else:
return uuid_candidates[0]
return None
for tf in targetfiles:
u = get_uuid_by_visibleName(tf)
if u is not None:
cmd = f'ssh -S {ssh_socketfile} root@{args.ssh_destination} "rm -r ~/.local/share/remarkable/xochitl/{u}*"'
subprocess.call(cmd, shell=True)
subprocess.call(f'ssh -S {ssh_socketfile} root@{args.ssh_destination} systemctl restart xochitl', shell=True)
ssh_connection.terminate()
print("All documents processed, have fun with your remaining paperwork. :)")