-
Notifications
You must be signed in to change notification settings - Fork 5
/
syncdir.py
executable file
·233 lines (211 loc) · 8.58 KB
/
syncdir.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env python3
"""
KMFDDM sync tool. This tool synchronizes declarations and sets from
a directory and uploads them to a KMFDDM server. It tries to be smart
about only notifying the changed items (declarations, sets) and only
performing one set of notifications for all changed items.
The specified directory is walked to find files that can be synced. Any
file with a ".json" extension is assumed to be declaration and is
uploaded as such. Files matching "set.$SET.txt" are assumed to contain
one line for each declaration identifier wanting to be associated to
"$SET" set name. Include a minus ("-") sign in front of a declaration
to dissociate it, or an octothorp/hash ("#") for a comment.
For example a directory might look like this:
./a/com.example.test.json
./b/com.example.act.json
./c/set.default.txt
Here each of the two JSON files will be treated as declarations and the
txt file will apply each declaration (one per line) to the "default"
set.
"""
import os
import json
import ssl
import urllib.request
from urllib.parse import urlencode
import argparse
import base64
import re
ssl._create_default_https_context = ssl._create_stdlib_context
set_pattern = r"set\.(.*?)\.txt"
nonotify_params = urlencode(
{
"nonotify": "1",
}
)
def make_declaration_req(
base_url: str, auth_header: str, declaraion_data: bytes
) -> urllib.request.Request:
url = base_url + "/v1/declarations?" + nonotify_params
req = urllib.request.Request(url=url, method="PUT")
req.add_header("Content-Type", "application/json")
req.add_header("Authorization", f"Basic {auth_header}")
req.data = declaraion_data
return req
def make_set_req(
base_url: str, auth_header: str, method: str, set_name: str, declaration_id: str
) -> urllib.request.Request:
url = (
base_url
+ "/v1/set-declarations/"
+ set_name
+ "?"
+ urlencode(
{
"declaration": declaration_id,
"nonotify": "1",
}
)
)
req = urllib.request.Request(url=url, method=method)
req.add_header("Authorization", f"Basic {auth_header}")
return req
def sync_dir(dir, base_url, key):
# collectors for later notifying/reporting
changed_decls = []
unchanged_decls = []
changed_sets = []
unchanged_sets = []
auth_header = base64.b64encode(f"kmfddm:{key}".encode("utf-8")).decode("utf-8")
set_files = []
for root, dirs, files in os.walk(dir):
for file in files:
file_path = os.path.join(root, file)
if file.endswith(".json"):
with open(file_path, "rb") as f:
try:
data = f.read()
decl = json.loads(data)
req = make_declaration_req(base_url, auth_header, data)
response = urllib.request.urlopen(req)
status_code = response.getcode()
if status_code == 204:
id = decl["Identifier"]
print(f"changed declaration {id}")
changed_decls.append(id)
else:
print(
f"WARNING: unknown status code declaration: {status_code}"
)
except json.JSONDecodeError as e:
print(f"ERROR parsing {file}: {str(e)}")
except urllib.error.HTTPError as e:
if e.code == 304:
unchanged_decls.append(decl["Identifier"])
else:
json_error = ""
try:
json_error = json.loads(e.read())["error"]
except:
pass
print(f"ERROR uploading {file}: {str(e)}: {json_error}")
except urllib.error.URLError as e:
print(f"ERROR uploading {file}: {str(e)}")
else:
match = re.search(set_pattern, file)
if not match:
continue
# just collect them for now as we want to process
# sets after all of the declarations have been uploaded
set_files.append((file_path, file, match.group(1)))
for file_path, file, set_name in set_files:
with open(file_path, "r") as f:
decls = [line.strip() for line in f]
changed_set = False
for decl_id in decls:
if decl_id == "" or decl_id[0] == "#":
# comment
continue
method = "PUT"
if decl_id[0] == "-":
# a declaration in a set file preceded with a minus (-)
# indicates removal of the association
decl_id = decl_id[1:].strip()
method = "DELETE"
req = make_set_req(base_url, auth_header, method, set_name, decl_id)
try:
response = urllib.request.urlopen(req)
status_code = response.getcode()
if status_code == 204:
if method == "PUT":
print(
f"associated declaration {decl_id} with set {set_name}"
)
elif method == "DELETE":
print(
f"dissociated declaration {decl_id} from set {set_name}"
)
changed_set = True
else:
print(f"WARNING: unknown status code sets: {status_code}")
except urllib.error.HTTPError as e:
if e.code != 304:
json_error = ""
try:
json_error = json.loads(e.read())["error"]
except:
pass
print(
f"ERROR associating {decl_id} with {set_name}: {str(e)}: {json_error}"
)
except urllib.error.URLError as e:
print(f"ERROR associating {decl_id} with {set_name}: {str(e)}")
if changed_set:
changed_sets.append(set_name)
else:
unchanged_sets.append(set_name)
if len(unchanged_decls) > 0:
print(f"unchanged declarations: {len(unchanged_decls)}")
if len(unchanged_sets) > 0:
print(f"unchanged sets: {len(unchanged_sets)}")
if len(changed_decls) > 0 or len(changed_sets) > 0:
params = {}
if len(changed_decls) > 0:
params["declaration"] = changed_decls
print(
f"changed declarations ({len(changed_decls)}): "
+ ", ".join(changed_decls)
)
if len(changed_sets) > 0:
params["set"] = changed_sets
print(f"changed sets ({len(changed_sets)}): " + ", ".join(changed_sets))
encoded_params = urlencode(params, doseq=True)
notify_url = base_url + "/v1/notify?" + encoded_params
req = urllib.request.Request(url=notify_url, method="POST")
req.add_header("Authorization", f"Basic {auth_header}")
try:
response = urllib.request.urlopen(req)
status_code = response.getcode()
if status_code == 204:
print(f"sent notify")
else:
print(f"WARNING: unknown status code notify: {status_code}")
except (urllib.error.HTTPError, urllib.error.URLError) as e:
print(f"ERROR notifying to {notify_url}: {str(e)}")
else:
print("no changed declarations or sets")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="KMFDDM syncer",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.epilog = __doc__
parser.add_argument(
"dir",
type=str,
help="path to the directory containing declarations and set files",
)
parser.add_argument(
"--baseurl",
type=str,
default=os.environ.get("BASE_URL", "http://[::1]:9002"),
help="URL for uploading the JSON files (default: http://[::1]:9002)",
)
parser.add_argument(
"--key",
type=str,
default=os.environ.get("API_KEY", "kmfddm"),
help="Password for HTTP Basic authentication",
)
args = parser.parse_args()
sync_dir(args.dir, args.baseurl, args.key)