|
| 1 | +#!/bin/python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import json |
| 7 | +import hashlib |
| 8 | +import base64 |
| 9 | +import requests |
| 10 | + |
| 11 | +def sha256_file(path): |
| 12 | + h = hashlib.sha256() |
| 13 | + with open(path, 'rb') as f: |
| 14 | + while True: |
| 15 | + data = f.read(1024 * 64) |
| 16 | + if not data: |
| 17 | + break |
| 18 | + h.update(data) |
| 19 | + return h.hexdigest() |
| 20 | + |
| 21 | +def init_build(host, token, files): |
| 22 | + url = host.rstrip('/') + '/upload/init' |
| 23 | + headers = {"Authorization": "Bearer " + token} |
| 24 | + payload = {"files": files} |
| 25 | + r = requests.post(url, json=payload, headers=headers, timeout=30) |
| 26 | + r.raise_for_status() |
| 27 | + return r.json() |
| 28 | + |
| 29 | +def upload_file(path, upload_info): |
| 30 | + url = upload_info.get('url') |
| 31 | + headers = dict(upload_info.get('headers', {})) |
| 32 | + |
| 33 | + size = os.path.getsize(path) |
| 34 | + headers['Content-Length'] = str(size) |
| 35 | + |
| 36 | + print('Uploading', path) |
| 37 | + with open(path, 'rb') as f: |
| 38 | + r = requests.put(url, data=f, headers=headers, timeout=900) |
| 39 | + if r.status_code != 200: |
| 40 | + print('Upload failed', r.status_code) |
| 41 | + print(r.text[:500]) |
| 42 | + r.raise_for_status() |
| 43 | + |
| 44 | +def commit_build(host, token, build_id): |
| 45 | + url = host.rstrip('/') + '/upload/commit' |
| 46 | + headers = {"Authorization": "Bearer " + token} |
| 47 | + r = requests.post(url, json={"buildId": build_id}, headers=headers, timeout=900) |
| 48 | + if r.status_code != 200: |
| 49 | + print('Commit failed', r.status_code) |
| 50 | + print(r.text[:500]) |
| 51 | + r.raise_for_status() |
| 52 | + return r.json() |
| 53 | + |
| 54 | +if __name__ == '__main__': |
| 55 | + parser = argparse.ArgumentParser(prog='deploy-build') |
| 56 | + parser.add_argument('--ipa', required=True, help='Path to IPA') |
| 57 | + parser.add_argument('--dsyms', help='Path to dSYMs.zip') |
| 58 | + parser.add_argument('--configuration', required=True, help='Path to JSON config') |
| 59 | + args = parser.parse_args() |
| 60 | + |
| 61 | + if not os.path.exists(args.configuration): |
| 62 | + print('{} does not exist'.format(args.configuration)) |
| 63 | + sys.exit(1) |
| 64 | + if not os.path.exists(args.ipa): |
| 65 | + print('{} does not exist'.format(args.ipa)) |
| 66 | + sys.exit(1) |
| 67 | + if args.dsyms is not None and not os.path.exists(args.dsyms): |
| 68 | + print('{} does not exist'.format(args.dsyms)) |
| 69 | + sys.exit(1) |
| 70 | + |
| 71 | + try: |
| 72 | + with open(args.configuration, 'r') as f: |
| 73 | + config = json.load(f) |
| 74 | + except Exception as e: |
| 75 | + print('Failed to read configuration:', e) |
| 76 | + sys.exit(1) |
| 77 | + |
| 78 | + host = config.get('host') |
| 79 | + token = config.get('auth_token') |
| 80 | + if not host or not token: |
| 81 | + print('Invalid configuration') |
| 82 | + sys.exit(1) |
| 83 | + ipa_path = args.ipa |
| 84 | + dsym_path = args.dsyms |
| 85 | + |
| 86 | + ipa_sha = sha256_file(ipa_path) |
| 87 | + files = { |
| 88 | + 'ipa': { |
| 89 | + 'filename': os.path.basename(ipa_path), |
| 90 | + 'size': os.path.getsize(ipa_path), |
| 91 | + 'sha256': ipa_sha, |
| 92 | + } |
| 93 | + } |
| 94 | + if dsym_path: |
| 95 | + dsym_sha = sha256_file(dsym_path) |
| 96 | + files['dsym'] = { |
| 97 | + 'filename': os.path.basename(dsym_path), |
| 98 | + 'size': os.path.getsize(dsym_path), |
| 99 | + 'sha256': dsym_sha, |
| 100 | + } |
| 101 | + |
| 102 | + print('Init build') |
| 103 | + init = init_build(host, token, files) |
| 104 | + build_id = init.get('build_id') |
| 105 | + urls = init.get('upload_urls', {}) |
| 106 | + if not build_id: |
| 107 | + print('No build_id') |
| 108 | + sys.exit(1) |
| 109 | + |
| 110 | + upload_file(ipa_path, urls.get('ipa', {})) |
| 111 | + if dsym_path and 'dsym' in urls: |
| 112 | + upload_file(dsym_path, urls.get('dsym', {})) |
| 113 | + |
| 114 | + print('Commit build') |
| 115 | + result = commit_build(host, token, build_id) |
| 116 | + |
| 117 | + print('Done! Install page:', result.get('install_page_url')) |
0 commit comments