-
Notifications
You must be signed in to change notification settings - Fork 0
/
crondump.py
37 lines (28 loc) · 1.15 KB
/
crondump.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
"Fetch and save gzipped tar files from an instance."
import json
from pathlib import Path
import sys
import requests
import utils
def fetch_and_save(url, apikey, dirpath):
"Fetch the gzipped tar file and save to local disk."
print(f"fetching mdbook.tgz from {url}")
response = requests.get(url, headers=dict(apikey=apikey))
if response.status_code != 200:
raise ValueError(f"invalid status code for response: {response.status_code}")
try:
content_disposition = response.headers["content-disposition"]
except KeyError:
raise ValueError("no content-disposition in response")
parts = content_disposition.split('"')
if len(parts) != 3:
raise ValueError("no filename in content-disposition in response")
filepath = dirpath.joinpath(parts[1])
with filepath.open("wb") as outfile:
outfile.write(response.content)
print(f"wrote mdbook.tgz to {filepath}")
if __name__ == "__main__":
dirpath = Path(sys.argv[1])
with dirpath.joinpath("config.json").open() as infile:
config = json.load(infile)
fetch_and_save(config["site"].rstrip("/") + "/tgz", config["apikey"], dirpath)