Repository navigation
Expand file tree
/
Copy pathDeployBuild.py
More file actions
158 lines (137 loc) · 4.95 KB
/
Copy pathDeployBuild.py
File metadata and controls
158 lines (137 loc) · 4.95 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/python3
import argparse
import os
import sys
import json
import hashlib
import base64
import time
import requests
def sha256_file(path):
h = hashlib.sha256()
with open(path, 'rb') as f:
while True:
data = f.read(1024 * 64)
if not data:
break
h.update(data)
return h.hexdigest()
def init_build(host, token, files, channel):
url = host.rstrip('/') + '/upload/init'
headers = {"Authorization": "Bearer " + token}
payload = {"files": files, "channel": channel}
r = requests.post(url, json=payload, headers=headers, timeout=30)
r.raise_for_status()
return r.json()
class ProgressFileReader:
def __init__(self, path, size):
self._file = open(path, 'rb')
self._size = size
self._sent = 0
self._start_time = time.time()
self._last_print = self._start_time
def __len__(self):
return self._size
def read(self, chunk_size=-1):
if chunk_size == -1:
chunk_size = self._size
data = self._file.read(chunk_size)
if not data:
elapsed = time.time() - self._start_time
speed = self._size / elapsed / 1024 / 1024 if elapsed > 0 else 0
print(' 100% - all bytes sent in {:.1f}s ({:.2f} MB/s), waiting for server response...'.format(elapsed, speed))
return data
self._sent += len(data)
now = time.time()
if now - self._last_print >= 5:
elapsed = now - self._start_time
speed = self._sent / elapsed / 1024 / 1024 if elapsed > 0 else 0
print(' {:.1f}% ({:.1f} / {:.1f} MB) {:.2f} MB/s'.format(
self._sent * 100 / self._size, self._sent / 1024 / 1024, self._size / 1024 / 1024, speed))
self._last_print = now
return data
def close(self):
self._file.close()
def upload_file(path, upload_info):
url = upload_info.get('url')
headers = dict(upload_info.get('headers', {}))
size = os.path.getsize(path)
headers['Content-Length'] = str(size)
print('Uploading {} ({:.1f} MB)'.format(path, size / 1024 / 1024))
start_time = time.time()
body = ProgressFileReader(path, size)
try:
r = requests.put(url, data=body, headers=headers, timeout=900)
finally:
body.close()
print(' Server responded: {} ({:.1f}s total)'.format(r.status_code, time.time() - start_time))
if r.status_code != 200:
print('Upload failed', r.status_code)
print(r.text[:500])
r.raise_for_status()
def commit_build(host, token, build_id):
url = host.rstrip('/') + '/upload/commit'
headers = {"Authorization": "Bearer " + token}
r = requests.post(url, json={"buildId": build_id}, headers=headers, timeout=900)
if r.status_code != 200:
print('Commit failed', r.status_code)
print(r.text[:500])
r.raise_for_status()
return r.json()
if __name__ == '__main__':
parser = argparse.ArgumentParser(prog='deploy-build')
parser.add_argument('--ipa', required=True, help='Path to IPA')
parser.add_argument('--dsyms', help='Path to dSYMs.zip')
parser.add_argument('--configuration', required=True, help='Path to JSON config')
args = parser.parse_args()
if not os.path.exists(args.configuration):
print('{} does not exist'.format(args.configuration))
sys.exit(1)
if not os.path.exists(args.ipa):
print('{} does not exist'.format(args.ipa))
sys.exit(1)
if args.dsyms is not None and not os.path.exists(args.dsyms):
print('{} does not exist'.format(args.dsyms))
sys.exit(1)
try:
with open(args.configuration, 'r') as f:
config = json.load(f)
except Exception as e:
print('Failed to read configuration:', e)
sys.exit(1)
host = config.get('host')
token = config.get('auth_token')
channel = config.get('channel')
if not host or not token or not channel:
print('Invalid configuration')
sys.exit(1)
ipa_path = args.ipa
dsym_path = args.dsyms
ipa_sha = sha256_file(ipa_path)
files = {
'ipa': {
'filename': os.path.basename(ipa_path),
'size': os.path.getsize(ipa_path),
'sha256': ipa_sha,
}
}
if dsym_path:
dsym_sha = sha256_file(dsym_path)
files['dsym'] = {
'filename': os.path.basename(dsym_path),
'size': os.path.getsize(dsym_path),
'sha256': dsym_sha,
}
print('Init build')
init = init_build(host, token, files, channel)
build_id = init.get('build_id')
urls = init.get('upload_urls', {})
if not build_id:
print('No build_id')
sys.exit(1)
upload_file(ipa_path, urls.get('ipa', {}))
if dsym_path and 'dsym' in urls:
upload_file(dsym_path, urls.get('dsym', {}))
print('Commit build')
result = commit_build(host, token, build_id)
print('Done! Install page:', result.get('install_page_url'))