Skip to content

Commit 6605337

Browse files
committed
bin/download-latest-release.py script
1 parent e7121f2 commit 6605337

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

bin/download-latest-release.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python
2+
import os
3+
import subprocess
4+
from urllib.parse import urlparse
5+
6+
import click
7+
import requests
8+
from decouple import config
9+
10+
11+
REPO_URL = config(
12+
"REPO_URL", default="https://api.github.com/repos/peterbe/whatsdeployed"
13+
)
14+
URL = REPO_URL + "/releases"
15+
16+
17+
def _check_output(*args, **kwargs):
18+
return subprocess.check_output(*args, **kwargs).decode("utf-8").strip()
19+
20+
21+
def _download(url):
22+
r = requests.get(url)
23+
r.raise_for_status()
24+
if "application/json" in r.headers["content-type"]:
25+
return r.json()
26+
return r
27+
28+
29+
@click.command()
30+
@click.option("-v", "--verbose", is_flag=True)
31+
@click.option("-t", "--tag-name", help="tag name if not the current")
32+
@click.option("-d", "--destination", help="place to download the zip file (default ./)")
33+
def cli(tag_name=None, verbose=False, destination=None):
34+
destination = destination or "."
35+
if not tag_name:
36+
tag_name = _check_output("git tag".split())
37+
assert tag_name
38+
39+
for release in _download(URL):
40+
if release["tag_name"] == tag_name:
41+
for asset in release["assets"]:
42+
if asset["content_type"] == "application/zip":
43+
url = asset["browser_download_url"]
44+
print("Downloading", url)
45+
fn = os.path.basename(urlparse(url).path)
46+
fp = os.path.join(destination, fn)
47+
with open(fp, "wb") as f:
48+
f.write(_download(url).content)
49+
print("Downloaded", fp, os.stat(fp).st_size, "bytes")
50+
break
51+
else:
52+
error_out("No application/zip asset found")
53+
break
54+
else:
55+
error_out("No tag name called {!r}".format(tag_name))
56+
57+
58+
def error_out(msg, raise_abort=True):
59+
click.echo(click.style(msg, fg="red"))
60+
if raise_abort:
61+
raise click.Abort
62+
63+
64+
if __name__ == "__main__":
65+
cli()

0 commit comments

Comments
 (0)