-
Notifications
You must be signed in to change notification settings - Fork 2
/
upload-artifacts.py
340 lines (296 loc) · 11.4 KB
/
upload-artifacts.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import datetime
import json
import os
import pprint
import subprocess
import sys
import time as ttime
from itertools import count
import requests
# BASE_URL = "https://sandbox.zenodo.org/api"
BASE_URL = "https://zenodo.org/api"
def print_now(*args):
print(*args, file=sys.stdout, flush=True)
def upload_files(bucket_url, files, token):
if files is None:
raise ValueError(
"Files cannot be None, specify a dict with file names "
"as keys and access mode as values"
)
ret_declare = requests.post(
bucket_url,
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {token}",
},
data=json.dumps([{"key": os.path.basename(file)} for file in files]),
)
print_now(ret_declare.status_code, ret_declare.text)
for file, mode in files.items():
print_now(f"Uploading {file}...")
basename = os.path.basename(file)
ret_content = requests.put(
f"{bucket_url}/{basename}/content",
headers={
"Accept": "application/json",
"Content-Type": "application/octet-stream",
"Authorization": f"Bearer {token}",
},
data=open(file, mode),
)
print_now(ret_content.status_code, ret_content.text)
ret_commit = requests.post(
f"{bucket_url}/{basename}/commit",
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {token}",
},
)
print_now(ret_commit.status_code, ret_commit.text)
def create_new_version(
conceptrecid=None, version=None, extra_files=None, token=None
):
rec = requests.get(
f"{BASE_URL}/records/{conceptrecid}/versions/latest",
headers={"Authorization": f"Bearer {token}"},
)
ret_newver = requests.post(
f"{BASE_URL}/records/{rec.json()['id']}/versions",
params={"access_token": token},
)
print_now(ret_newver.url, ret_newver.status_code, ret_newver.json())
newver_draft = ret_newver.json()["links"]["self"]
notes_urls = [
# # non-tiled
# "https://github.com/nsls2-conda-envs/nsls2-collection/pull/28",
# "https://github.com/nsls2-conda-envs/nsls2-collection/actions/runs/7603753363",
# # need this empty line to enforce line break on Zenodo:
# "",
# tiled
"https://github.com/nsls2-conda-envs/nsls2-collection-tiled/pull/45",
"https://github.com/nsls2-conda-envs/nsls2-collection-tiled/actions/runs/11617248480",
]
notes_urls_strs = "<br>\n".join([f'<a href="{url}">{url}</a>'
if url else ""
for url in notes_urls])
unpack_instructions = """
Unpacking instructions:
<br>
<pre>
mkdir -p ~/conda_envs/<env-name>
cd ~/conda_envs/<env-name>
wget <url-to>/<env-name>.tar.gz
tar -xvf <env-name>.tar.gz
conda activate $PWD
conda-unpack
</pre>
"""
data = {
"metadata": {
"version": version,
"title": f"NSLS-II collection conda environment {version} with Python 3.10, 3.11, and 3.12",
"description": f"NSLS-II collection environment deployed to the experimental floor.<br><br>{notes_urls_strs}",
"resource_type": {"id": "software"},
"publication_date": datetime.datetime.now().strftime("%Y-%m-%d"),
"publisher": "NSLS-II, Brookhaven National Laboratory",
"prereserve_doi": True,
"keywords": [
"conda",
"NSLS-II",
"bluesky",
"data acquisition",
"conda-forge",
"conda-pack",
],
"additional_descriptions": [
{
"description": unpack_instructions,
"type": {
"id": "notes",
"title": {
"en": "Unpacking instructions"
}
},
},
],
"creators": [
{
"person_or_org": {
"name": "Rakitin, Max",
"given_name": "Max",
"family_name": "Rakitin",
"type": "personal",
"identifiers": [{
"scheme": "orcid",
"identifier": "0000-0003-3685-852X",
}],
},
"affiliations": [{
"id": "01q47ea17",
"name": "NSLS-II, Brookhaven National Laboratory",
}],
},
{
"person_or_org": {
"name": "Bischof, Garrett",
"given_name": "Bischof",
"family_name": "Garrett",
"type": "personal",
"identifiers": [{
"scheme": "orcid",
"identifier": "0000-0001-9351-274X",
}],
},
"affiliations": [{
"id": "01q47ea17",
"name": "NSLS-II, Brookhaven National Laboratory",
}],
},
{
"person_or_org": {
"name": "Aishima, Jun",
"given_name": "Jun",
"family_name": "Aishima",
"type": "personal",
"identifiers": [{
"scheme": "orcid",
"identifier": "0000-0003-4710-2461",
}],
},
"affiliations": [{
"id": "01q47ea17",
"name": "NSLS-II, Brookhaven National Laboratory",
}],
},
],
}
}
resp_update = requests.put(
newver_draft,
params={"access_token": token},
headers={"Content-Type": "application/json"},
data=json.dumps(data),
)
print_now(newver_draft, resp_update.status_code, resp_update.text)
for file in resp_update.json()["files"]:
self_file = file["links"]["self"]
r = requests.delete(self_file, params={"access_token": token})
print_now(r.status_code, r.text)
bucket_url = resp_update.json()["links"]["files"]
all_files = {}
if extra_files is not None:
all_files.update(**extra_files)
upload_files(bucket_url, files=all_files, token=token)
# ret = requests.post(
# resp_update.json()["links"]["publish"], params={"access_token": token}
# )
# print_now(ret.status_code, ret.text)
# return ret.json()
def update_deposition_with_files(conceptrecid=None, files=None, token=None):
headers = {"Authorization": f"Bearer {token}"}
rec = requests.get(
f"{BASE_URL}/records/{conceptrecid}",
headers=headers,
)
latest_published_id = rec.json()["id"]
deposition = requests.get(
f"{BASE_URL}/deposit/depositions/{latest_published_id}",
headers=headers,
)
latest_draft_url = deposition.json()["links"]["latest_draft"]
latest_draft = requests.get(
latest_draft_url,
headers=headers,
)
bucket_url = latest_draft.json()["links"]["bucket"]
upload_files(bucket_url, files=files, token=token)
if __name__ == "__main__":
conceptrecid = "4057062" # never changes, it's for the initial version.
version = "2024-3.0"
token = os.environ["ZENODO_TOKEN"]
resp = create_new_version(
conceptrecid=conceptrecid,
# version=f"{version}-tiled",
version=f"{version}",
token=token,
# extra_files={"README.md": "r", "LICENSE": "r"} # used for testing purposes
extra_files={
# # Python 3.8 (non-tiled)
# f"{version}-py38-md5sum.txt": "r",
# f"{version}-py38-sha256sum.txt": "r",
# f"{version}-py38.yml": "r",
# f"Dockerfile-{version}-py38": "r",
# f"runner-{version}-py38.sh": "r",
# f"{version}-py38.tar.gz": "rb",
# # Python 3.9 (non-tiled)
# f"{version}-py39-md5sum.txt": "r",
# f"{version}-py39-sha256sum.txt": "r",
# f"{version}-py39.yml": "r",
# f"Dockerfile-{version}-py39": "r",
# f"runner-{version}-py39.sh": "r",
# f"{version}-py39.tar.gz": "rb",
# # Python 3.10 (non-tiled)
# f"{version}-py310-md5sum.txt": "r",
# f"{version}-py310-sha256sum.txt": "r",
# f"{version}-py310.yml": "r",
# f"Dockerfile-{version}-py310": "r",
# f"runner-{version}-py310.sh": "r",
# f"{version}-py310.tar.gz": "rb",
# # Python 3.11 (non-tiled)
# f"{version}-py311-md5sum.txt": "r",
# f"{version}-py311-sha256sum.txt": "r",
# f"{version}-py311.yml": "r",
# f"Dockerfile-{version}-py311": "r",
# f"runner-{version}-py311.sh": "r",
# f"{version}-py311.tar.gz": "rb",
# # Python 3.8 (tiled)
# f"{version}-py38-tiled-md5sum.txt": "r",
# f"{version}-py38-tiled-sha256sum.txt": "r",
# f"{version}-py38-tiled.yml": "r",
# f"Dockerfile-{version}-py38-tiled": "r",
# f"runner-{version}-py38-tiled.sh": "r",
# f"{version}-py38-tiled.tar.gz": "rb",
# # Python 3.9 (tiled)
# f"{version}-py39-tiled-md5sum.txt": "r",
# f"{version}-py39-tiled-sha256sum.txt": "r",
# f"{version}-py39-tiled.yml": "r",
# f"Dockerfile-{version}-py39-tiled": "r",
# f"runner-{version}-py39-tiled.sh": "r",
# f"{version}-py39-tiled.tar.gz": "rb",
# Python 3.10 (tiled)
f"{version}-py310-tiled-md5sum.txt": "r",
f"{version}-py310-tiled-sha256sum.txt": "r",
f"{version}-py310-tiled.yml.txt": "r",
f"{version}-py310-tiled.tar.gz": "rb",
# Python 3.11 (tiled)
f"{version}-py311-tiled-md5sum.txt": "r",
f"{version}-py311-tiled-sha256sum.txt": "r",
f"{version}-py311-tiled.yml.txt": "r",
f"{version}-py311-tiled.tar.gz": "rb",
# Python 3.12 (tiled)
f"{version}-py312-tiled-md5sum.txt": "r",
f"{version}-py312-tiled-sha256sum.txt": "r",
f"{version}-py312-tiled.yml.txt": "r",
f"{version}-py312-tiled.tar.gz": "rb",
},
)
pprint.pprint(resp)
# files = {
# # Python 3.8
# "2022-2.2-py38-md5sum.txt": "r",
# "2022-2.2-py38-sha256sum.txt": "r",
# "2022-2.2-py38.yml": "r",
# "Dockerfile-2022-2.2-py38": "r",
# "runner-2022-2.2-py38.sh": "r",
# "2022-2.2-py38.tar.gz": "rb",
# # Python 3.9
# "2022-2.2-py39-md5sum.txt": "r",
# "2022-2.2-py39-sha256sum.txt": "r",
# "2022-2.2-py39.yml": "r",
# "Dockerfile-2022-2.2-py39": "r",
# "runner-2022-2.2-py39.sh": "r",
# "2022-2.2-py39.tar.gz": "rb",
# }
# update_deposition_with_files(
# conceptrecid=conceptrecid, files=files, token=token
# )