-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
178 lines (147 loc) · 4.4 KB
/
main.py
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import requests
import platform
import json
import sys
import os
def to_error_message(errs):
"""
Formats an array of error dicts into an error message string. Returns an error message.
"""
return ', '.join(map(lambda e: f"{e['title']}: {e['detail']}", errs))
def create_release(version, channel, name=None, tag=None):
"""
Creates a new release for the configured product. Returns a release object.
"""
res = requests.post(
f"https://api.keygen.sh/v1/accounts/{os.environ['KEYGEN_ACCOUNT_ID']}/releases",
headers={
'Authorization': f"Bearer {os.environ['KEYGEN_PRODUCT_TOKEN']}",
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json',
'Keygen-Version': '1.3'
},
data=json.dumps({
'data': {
'type': 'releases',
'attributes': {
'version': version,
'channel': channel,
'name': name,
'tag': tag
},
'relationships': {
'product': {
'data': { 'type': 'product', 'id': os.environ['KEYGEN_PRODUCT_ID'] }
}
}
}
})
)
release = res.json()
if 'errors' in release:
errs = release['errors']
print(f'[error] Release failed: errors={to_error_message(errs)}',
file=sys.stderr)
sys.exit(1)
else:
print(f"[info] Created: release={release['data']['id']} link={release['data']['links']['self']}",
file=sys.stdout)
return release['data']
def publish_release(release):
"""
Publishes a release. Returns a release object.
"""
res = requests.post(
f"https://api.keygen.sh/v1/accounts/{os.environ['KEYGEN_ACCOUNT_ID']}/releases/{release['id']}/actions/publish",
headers={
'Authorization': f"Bearer {os.environ['KEYGEN_PRODUCT_TOKEN']}",
'Accept': 'application/vnd.api+json',
'Keygen-Version': '1.3'
}
)
release = res.json()
if 'errors' in release:
errs = release['errors']
print(f'[error] Publish failed: errors={to_error_message(errs)}',
file=sys.stderr)
sys.exit(1)
else:
print(f"[info] Published: release={release['data']['id']} link={release['data']['links']['self']}",
file=sys.stdout)
return release['data']
def upload_artifact_for_release(release, filename, filetype, filesize, platform, arch, data=None):
"""
Uploads an artifact for the given release. Returns an artifact object.
"""
res = requests.post(
f"https://api.keygen.sh/v1/accounts/{os.environ['KEYGEN_ACCOUNT_ID']}/artifacts",
allow_redirects=False,
headers={
'Authorization': f"Bearer {os.environ['KEYGEN_PRODUCT_TOKEN']}",
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json',
'Keygen-Version': '1.3'
},
data=json.dumps({
'data': {
'type': 'artifacts',
'attributes': {
'filename': filename,
'filesize': filesize,
'filetype': filetype,
'platform': platform,
'arch': arch
},
'relationships': {
'release': {
'data': { 'type': 'release', 'id': release['id'] }
}
}
}
})
)
artifact = res.json()
if 'errors' in artifact:
errs = artifact['errors']
print(f'[error] Upload failed: errors={to_error_message(errs)}',
file=sys.stderr)
sys.exit(1)
else:
print(f"[info] Uploaded: artifact={artifact['data']['id']} link={artifact['data']['links']['self']}",
file=sys.stdout)
# Follow redirect and upload file to storage provider
upload_url = res.headers['location']
if data:
requests.put(upload_url,
headers={ 'Content-Type': 'text/plain' },
data=data
)
return artifact['data']
release = create_release(
name='Python Release v1',
version='1.0.0',
channel='stable'
)
with open('examples/hello-world.txt', mode='r') as f:
stat = os.stat(f.name)
artifact = upload_artifact_for_release(
filename=f.name,
filesize=stat.st_size,
filetype='txt',
platform=f"{platform.system()} {platform.release()}",
arch=platform.processor(),
release=release,
data=f
)
with open('examples/hello-mars.txt', mode='r') as f:
stat = os.stat(f.name)
artifact = upload_artifact_for_release(
filename=f.name,
filesize=stat.st_size,
filetype='txt',
platform=f"{platform.system()} {platform.release()}",
arch=platform.processor(),
release=release,
data=f
)
publish_release(release)