-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncthing_rescan.py
executable file
·138 lines (110 loc) · 4.13 KB
/
syncthing_rescan.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/env python3
# vim: set fileencoding=utf-8 :
""" syncthing_rescan.py
Manually triggers a rescan of the local Syncthing instance"""
# The MIT License (MIT)
#
# Copyright (c) 2017-2024 Georg Lutz
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Standard library imports:
import argparse
import os
import http.client
import xml.parsers.expat
def get_args():
'''Configures command line parser and returns parsed parameters'''
parser = argparse.ArgumentParser(
description="Manually triggers a rescan of the local Syncthing instance")
return parser.parse_args()
def get_config():
'''Returns a dict with the syncthing config
{ "apikey": "here is the api, key",
"address": "the address of the GUI server"
}
'''
filepath_candidates = [
os.path.expanduser("~/.local/state/syncthing/config.xml"),
os.path.expanduser("~/.config/syncthing/config.xml")
]
filepath = ""
for file_ in filepath_candidates:
if os.path.exists(file_):
filepath = file_
break
if not filepath:
print("Could not find syncthing config file")
sys.exit(1)
result = {
"apikey": "",
"address": ""
}
# Workaround to allow write access to variables from inner functions
in_gui = [False]
in_address = [False]
in_apikey = [False]
def cb_start_element(name, attrs):
'''expat callback'''
if name == "gui":
in_gui[0] = True
if in_gui[0] and name == "address":
in_address[0] = True
if in_gui[0] and name == "apikey":
in_apikey[0] = True
def cb_end_element(name):
'''expat callback'''
if name == "gui" and in_gui[0]:
in_gui[0] = False
if name == "address" and in_address[0]:
in_address[0] = False
if name == "apikey" and in_apikey[0]:
in_apikey[0] = False
def cb_character_data_handler(data):
'''expat callback'''
if in_address[0]:
result["address"] = data
if in_apikey[0]:
result["apikey"] = data
with open(filepath, "rb") as filehandle:
parser = xml.parsers.expat.ParserCreate()
parser.StartElementHandler = cb_start_element
parser.EndElementHandler = cb_end_element
parser.CharacterDataHandler = cb_character_data_handler
parser.ParseFile(filehandle)
return result
def main():
'''main function, called when script file is executed directly'''
get_args()
config = get_config()
url = "http://" + config["address"] + "/rest/db/scan"
print("Calling " + url + ":")
# can't use urllib2. because it capitalizes headers, i.e. transforms
# request.add_header("X-API-Key", config["apikey"])
# to
# request.add_header("X-Api-Key", config["apikey"])
conn = http.client.HTTPConnection("127.0.0.1:8384")
headers = {"X-API-Key": config["apikey"]}
conn.request("POST", "/rest/db/scan", headers=headers)
response = conn.getresponse()
print(response.status, response.reason)
data = response.read()
print(data)
conn.close()
if __name__ == "__main__":
main()