From cfd6ae88b194d69d2a2c365d4a8a78644ba9079b Mon Sep 17 00:00:00 2001 From: Aleks Kissinger Date: Sun, 13 Jan 2019 18:13:29 +0100 Subject: added github AUTO deploy scripts --- scripts/gh-list.py | 11 +++++++++++ scripts/gh-push.py | 30 ++++++++++++++++++++++++++++++ scripts/gh.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 scripts/gh-list.py create mode 100644 scripts/gh-push.py create mode 100644 scripts/gh.py (limited to 'scripts') diff --git a/scripts/gh-list.py b/scripts/gh-list.py new file mode 100644 index 0000000..1966db4 --- /dev/null +++ b/scripts/gh-list.py @@ -0,0 +1,11 @@ +from gh import gh, pr +import os + +tok = os.getenv('GITHUB_TOKEN') + +draft = [r for r in gh('releases') + if r['draft'] and r['name'] == 'AUTO'][0] + +for a in draft['assets']: + print(a['browser_download_url'] + + '?access_token=' + tok) diff --git a/scripts/gh-push.py b/scripts/gh-push.py new file mode 100644 index 0000000..3aedb15 --- /dev/null +++ b/scripts/gh-push.py @@ -0,0 +1,30 @@ +import sys, re +from gh import gh, get_release + +if len(sys.argv) != 2: + print("Usage: python gh-push.py FILENAME") + sys.exit(1) + +f = sys.argv[1] + +print('Pulling info on release AUTO.') +draft = get_release('AUTO') + +existing = [a for a in draft['assets'] if a['name'] == f] +if (len(existing) > 0): + print('Asset exists, deleting.') + gh('releases/assets/' + str(existing[0]['id']), + ['-X', 'DELETE']) + +print('Uploading new asset...') + +upload_url = re.sub( + '\\{.*\\}', '?name=' + f, + draft['upload_url']) + +resp = gh(upload_url, [ + '-H', 'Content-type: application/octet-stream', + '--data-binary', f + ]) + +print('Done.') diff --git a/scripts/gh.py b/scripts/gh.py new file mode 100644 index 0000000..13a8880 --- /dev/null +++ b/scripts/gh.py @@ -0,0 +1,29 @@ +import sys, os, json +from subprocess import Popen, PIPE + +api_url = 'https://api.github.com/repos/tikzit/tikzit' + +tok = os.getenv('GITHUB_TOKEN', '') + +if tok == '': + print("Must set GITHUB_TOKEN environment variable.") + sys.exit(1) + +# pretty-print JSON responses +def pr(j): print(json.dumps(j, indent=2)) + +# call GitHub API with curl +def gh(s, args=[]): + cmd = (["curl", "-s"] + args + + ["-H", "Authorization: token " + tok] + + [s if 'https://' in s else api_url + '/' + s]) + # ONLY UN-COMMENT FOR TESTING: + # print(' '.join(cmd)) + p = Popen(cmd, stdout=PIPE) + resp = p.stdout.read() + return json.loads(resp if resp else '{}') + +def get_release(n): + rs = [r for r in gh('releases') if r['name'] == n] + if len(rs) > 0: return rs[0] + else: return None -- cgit v1.2.3