-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.py
executable file
·309 lines (257 loc) · 10.9 KB
/
backup.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import argparse
import json
import multiprocessing as mp
import os
import re
import shutil
import sys
import tarfile
import unicodedata
from dataclasses import dataclass
from datetime import date, timedelta, datetime
from os import path, makedirs, listdir
from typing import List, Dict, Any
import requests
def parse_args(print_args: bool = True) -> argparse.Namespace:
"""create the argument parser
Args:
print_args (bool, optional): If true, the arguments will be printed to stdout after parsing. Defaults to True.
Returns:
argparse.Namespace: the parsed arguments as a Namespace object
"""
default_url = "http://localhost:8080/fhir"
default_resource_types = ["CodeSystem", "ValueSet", "ConceptMap", "StructureDefinition"]
parser = argparse.ArgumentParser(
"Terminology Server Backup")
parser.add_argument("--endpoint",
"-e",
default=default_url,
type=str,
dest="endpoint",
help="The FHIR endpoint of the server (Default: '%(default)s')")
parser.add_argument("--resource-types",
"-r",
nargs="+",
default=default_resource_types,
type=str,
dest='resource_types',
help="the resource types to back-up. Separate with Space (Default: '%(default)s')")
parser.add_argument("--header",
help="headers to pass to the HTTP method. Use for authentication if required! "
"If multiple required, repeat argument. (Default: None)",
dest="headers",
action="append")
parser.add_argument("--out-dir", "-o",
default="./output",
help="destination directory (Default %(default)s)",
dest="out_dir",
type=str)
parser.add_argument("--delete-days", "-d",
default=0,
type=int,
dest='delete_days',
help="remove folders from at least (>=) these many days ago "
"(Default %(default)s, for no removal)")
parser.add_argument("--tarball", "-t",
action='store_true',
help='create a tarball from the downloaded file')
parser.add_argument("--parallel", "-l",
default=1,
type=int,
help="number of parallel GETs to carry out (Default %(default)s for no parallel execution)")
parsed_args = parser.parse_args()
parsed_args.endpoint = parsed_args.endpoint.strip("/")
parsed_args.parallel = max(parsed_args.parallel, 1)
parsed_args.delete_days = max(parsed_args.delete_days, 0)
if print_args:
for arg in vars(parsed_args):
print(f" - {arg}: {getattr(parsed_args, arg)}")
return parsed_args
@dataclass
class BundleResponse:
"""store the entry in a bundle GET operation
"""
resource_id: str
title: str
canonical_url: str
url: str
version: str
def perform_request_as_json(url: str, headers: List[str] = None) -> Dict[str, Any]:
"""perform a GET request to url with headers and return the JSON representation of the response
Args:
url (str): the endpoint to query
headers (List[str], optional): the headers to pass to requests.get. Defaults to None.
Raises:
ValueError: if the status code is not 200
Returns:
Dict[str, Any]: The JSON body as a Dict
"""
rx = requests.get(url, headers=headers)
if rx.status_code != 200:
raise ValueError(
f"HTTP Error {rx.status_code} getting from {url}", rx)
return rx.json()
def get_resource_urls_from_server(fhir_endpoint: str, resource_type: str, headers: List[str] = None) \
-> List[BundleResponse]:
"""get the urls of all the resources of the given type from the server. Walks through bundles!
Args:
fhir_endpoint (str): the endpoint
resource_type (str): the resource type (gets included in the request url)
headers (List[str], optional): the headers to pass to requests.get. Defaults to None.
Returns:
List[BundleResponse]: The parsed entries of the original bundle
"""
request_url = f"{fhir_endpoint}/{resource_type}"
bundle_responses: List[BundleResponse] = []
next_link = request_url
while next_link:
print("Requesting from: ", next_link)
bundle_json = perform_request_as_json(next_link, headers)
bundle_responses += bundle_json_to_bundle_response_list(bundle_json)
next_link = bundle_json_get_next_link(bundle_json)
return bundle_responses
def bundle_json_get_next_link(bundle_json: Dict[str, Any]) -> str:
"""get the 'next' link from a bundle, if it exists
Args:
bundle_json (Dict[str, Any]): the bundle to examine
Returns:
str: the url of the 'next' bundle or None
"""
filtered_link = [link for link in bundle_json["link"] if link["relation"] == "next"]
if len(filtered_link) > 0:
return filtered_link[0]["url"]
return ""
def bundle_json_to_bundle_response_list(bundle_json: Dict[str, Any]) -> List[BundleResponse]:
"""parse every entry in the bundle as a BundleResponse
Args:
bundle_json (Dict[str, Any]): the bundle to parse
Returns:
List[BundleResponse]: the parsed entries
"""
if 'entry' not in bundle_json.keys():
return []
responses: List[BundleResponse] = []
for entry in bundle_json['entry']:
url = entry["fullUrl"]
title = entry["resource"].get("name", None)
canonical_url = entry["resource"]["url"]
version = entry["resource"].get("version", None)
resource_id = entry["resource"]["id"]
responses.append(BundleResponse(
resource_id,
title,
canonical_url,
url,
version))
return responses
def download_resource(resource_type: str, r: BundleResponse, out_dir: str) -> str:
"""download a resource from the fully-qualified url in r to out_dir
Args:
resource_type (str): the resource type, used in the output filename
r (BundleResponse): the parsed entry from the search bundle
out_dir (str): the output directory
Returns:
str: the fully-qualified output path
"""
rx = perform_request_as_json(r.url)
target_filename = f"{resource_type}-{r.resource_id}_{r.title}_{today}"
target_abspath = path.join(out_dir, slugify(target_filename)) + ".json"
with open(target_abspath, "w") as fs:
json.dump(rx, fs, indent=2)
sys.stdout.flush()
return target_abspath
def slugify(value, allow_unicode=False):
"""
Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated
dashes to single dashes. Remove characters that aren't alphanumerics,
underscores, or hyphens. Convert to lowercase. Also strip leading and
trailing whitespace, dashes, and underscores.
via https://stackoverflow.com/a/295466
"""
value = str(value)
if allow_unicode:
value = unicodedata.normalize('NFKC', value)
else:
value = unicodedata.normalize('NFKD', value).encode(
'ascii', 'ignore').decode('ascii')
value = re.sub(r'[^\w\s-]', '', value.lower())
return re.sub(r'[-\s]+', '-', value).strip('-_')
def remove_old_directories():
print("\n\n########\n\n")
print("REMOVING")
if args.delete_days <= 0:
print("No directories were removed")
return
print(f"Removing from {args.out_dir}, >= {args.delete_days} ago")
folder_names = sorted(listdir(args.out_dir))
today_date = date.fromisoformat(today)
cutoff_date = today_date - timedelta(days=args.delete_days)
print("Cutoff Date:", cutoff_date)
to_delete = [
fn for fn in folder_names if date.fromisoformat(fn) <= cutoff_date]
print("These folders will be deleted:", to_delete)
for fn in to_delete:
full_path = path.abspath(path.join(args.out_dir, fn))
print(f" - {fn}: ", end='')
try:
print(f"{len(listdir(full_path))} sub-directories", end='')
shutil.rmtree(full_path)
print(" -- deleted")
except PermissionError:
print()
permissions = os.stat(full_path)
error_print(f"***Permission Error for '{full_path}': {permissions}")
def download_all_resource_types():
"""main entry point into the app"""
for resource_type in args.resource_types:
print("\n\n########\n\n")
resource_list = get_resource_urls_from_server(
args.endpoint, resource_type, args.headers)
print(f"got {len(resource_list)} resources of type {resource_type}")
if len(resource_list) == 0:
continue
out_dir = path.join(path.abspath(args.out_dir), today, resource_type)
if not (path.isdir(out_dir)):
makedirs(out_dir)
print(f"downloading with {args.parallel} parallel execution(s)")
if args.parallel == 1:
for r in resource_list:
download_resource_to_file(resource_type, r, out_dir)
sys.stdout.flush()
else:
pool = mp.Pool(args.parallel)
[pool.apply(download_resource_to_file, args=(resource_type, r, out_dir)) for r in resource_list]
pool.close()
sys.stdout.flush()
def download_resource_to_file(resource_type: str, r: BundleResponse, out_dir: str):
fn = download_resource(resource_type, r, out_dir)
print(f" - {r.url} (canonical {r.canonical_url}) -> {fn}")
sys.stdout.flush()
return fn
def error_print(*the_args, **the_kwargs):
"""https://stackoverflow.com/a/14981125/2333678"""
print(*the_args, file=sys.stderr, **the_kwargs)
def create_tarball():
"""create a tarball for the downloaded files"""
if not args.tarball:
return
print("\n\n########\n\n")
output_path = path.join(path.abspath(args.out_dir), today)
tar_filename = f"{today}.tar.gz"
tar_path = path.join(output_path, tar_filename)
print(f"creating tarball at {tar_path}")
file_list = sorted([path.join(output_path, f) for f in listdir(output_path) if f != tar_filename])
with tarfile.open(tar_path, "w:gz") as tar:
for f in file_list:
tar.add(f, arcname=f"{today}/{os.path.basename(f)}")
print(f" - added {f} to tarball")
if __name__ == "__main__":
today = date.today().isoformat()
print("##########################################")
print(f"executing at {datetime.now().isoformat()} UTC")
print("------------------------------------------")
args = parse_args()
download_all_resource_types()
create_tarball()
remove_old_directories()
print("##########################################\n\n")