Created
April 21, 2020 10:58
-
-
Save mwtoews/4f7ae41f455da6d6ec25f342bb69082a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
Download pest++ binaries, and install them in ./bin | |
""" | |
import os | |
import requests | |
import sys | |
import urllib | |
# See https://developer.github.com/v3/repos/releases/ | |
owner = 'mwtoews' | |
repo = 'pestpp-usgs' | |
api_url = 'https://api.github.com/repos/{}/{}'.format(owner, repo) | |
def main(release_id='latest', force=False): | |
req_url = api_url + '/releases/' + release_id | |
resp = requests.get(req_url) | |
if not resp.ok: | |
raise RuntimeError('{}: {}'.format(req_url, str(resp))) | |
release = resp.json() | |
print('fetched release {}'.format(release['tag_name'])) | |
assets = release.get('assets', []) | |
if sys.platform.startswith('linux'): | |
ostag = 'linux' | |
elif sys.platform.startswith('win'): | |
ostag = 'win' | |
elif sys.platform.startswith('darwin'): | |
ostag = 'mac' | |
found_ostag = False | |
for asset in assets: | |
if ostag in asset['name']: | |
found_ostag = True | |
break | |
if not found_ostag: | |
raise RuntimeError( | |
'could not find {} from {}; see available assets here: {}'.format( | |
ostag, release['tag_name'], release['html_url'])) | |
basedir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) | |
download_dir = os.path.join(basedir, '.downloads') | |
if not os.path.isdir(download_dir): | |
os.mkdir(download_dir) | |
download_pth = os.path.join(download_dir, asset['name']) | |
if os.path.isfile(download_pth) and not force: | |
print('using previous download (--force was not specified)') | |
else: | |
print('downloading {}'.format(asset['name'])) | |
urllib.request.urlretrieve(asset['browser_download_url'], download_pth) | |
print('extracting files to {}'.format(os.path.abspath(basedir))) | |
if download_pth.endswith('.zip'): | |
import zipfile | |
with zipfile.ZipFile(download_pth, 'r') as f: | |
f.extractall(basedir) | |
elif download_pth.endswith('.tar.gz') or download_pth.endswith('.tgz'): | |
import tarfile | |
with tarfile.open(download_pth, "r:gz") as f: | |
f.extractall(basedir) | |
if __name__ == '__main__': | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
'--release_id', default='latest', | |
help='release_id (default: %(default)s)') | |
parser.add_argument( | |
'--force', action='store_true', help='force re-download') | |
args = parser.parse_args() | |
main(**vars(args)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment