-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpublish-ci.py
65 lines (60 loc) · 2.43 KB
/
publish-ci.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
import requests
import json
import hashlib
import os
uri = 'https://zenodo.org/api/deposit/depositions'
access_token = os.environ['ZENODO_API_KEY']
headers = {"Content-Type": "application/json"}
# login
response = requests.get(uri, params={'access_token': access_token})
# data will be sent in the headers with the request
data = {'filename': os.environ['TARBALL']}
# files is an array of files to be sent as parameters to the request
files = {'file': open(os.environ['TARBALL'], 'rb')}
with open('metadata.json') as metadata_file:
metadata = json.load(metadata_file)
# If the project has already been published, we should check if anything has
# changed and if so, update the project
# If the project not yet been published, then create a deposition for it.
if os.path.isfile('zenodo.json'):
print("This build has been published before")
with open('zenodo.json') as deposition:
zenodo = json.load(deposition)
id = zenodo['id']
print 'id is ', id
# get the files path
files_url = zenodo['links']['files']
print files_url
published_checksum = requests.get(
files_url,
params={'access_token': access_token})\
.json()[0]['checksum']
checksum = hashlib.md5(open(os.environ['TARBALL'], 'rb').read())\
.hexdigest()
# assert checksum == published_checksum
if (checksum == published_checksum):
print "no need to publish this, the file is the same"
else:
print "we need to update the file"
else:
# The project has not yet been published
# deposit the file
print("no deposition yet")
# create deposition and write the publication
create = requests.post(uri,
params={'access_token': access_token},
json={},
headers=headers)
id = create.json()['id']
deposit = requests.post(uri + '/%s/files' % id,
params={'access_token': access_token},
data=data,
files=files)
print(deposit.json())
# update with metadata
meta = requests.put(uri + '/%s' % id,
params={'access_token': access_token},
data=json.dumps(metadata),
headers=headers)
with open('zenodo.json', 'w') as deposition:
json.dump(create.json(), deposition)