-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupload.py
71 lines (53 loc) · 2.52 KB
/
upload.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
from pathlib import Path
import json
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
import getpass
from paramiko import AutoAddPolicy, SSHClient
from scp import SCPClient
RM_TEMPLATES_DIR = Path("/usr/share/remarkable/templates/")
def main(args):
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(AutoAddPolicy())
password = args.password or getpass.getpass("reMarkable password: ")
ssh.connect(args.host, username=args.user, password=password)
scp = SCPClient(ssh.get_transport())
# Get templates.json from remarkable
scp.get(str(RM_TEMPLATES_DIR / "templates.json"))
with Path("./templates.json").open("r") as f:
templates = json.load(f)
# Load new/updated templates
with Path("./files/templates.json").open("r") as f:
new_templates = json.load(f)
# Updated templates.json
new_template_names = [template["name"] for template in new_templates["templates"]]
for i, template in enumerate(templates["templates"]):
for new_template in new_templates["templates"]:
if template["name"] == new_template["name"]:
templates["templates"][i] = new_template
new_template_names.remove(template["name"])
for template in new_templates["templates"]:
if template["name"] in new_template_names:
templates["templates"].append(template)
with Path("./templates.json").open("w") as f:
json.dump(templates, f, indent=2)
# Copy files to remarkable
files = ["templates.json"]
for ext in ["png", "svg"]:
files += [f"./files/{filename}.{ext}" for filename in [template["filename"] for template in new_templates["templates"]]]
scp.put(files, remote_path=str(RM_TEMPLATES_DIR))
# Restart xochitl
stdin, stdout, stderr = ssh.exec_command('systemctl restart xochitl')
if errors := stderr.readlines():
raise Exception("\n".joins(errors))
if out := stdout.readlines():
for line in out:
print(line)
ssh.close()
if __name__ == "__main__":
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument("--host", type=str, default="10.11.99.1", help="Hostname for connecting with remarkable")
parser.add_argument("-u", "--user", type=str, default="root", help="Username for connecting with remarkable")
parser.add_argument("-p", "--password", action="store_true", help="Use ssh password for connection with remarkable")
args = parser.parse_args()
main(args)