-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud-exporter.py
More file actions
executable file
·94 lines (83 loc) · 3.55 KB
/
Copy pathcloud-exporter.py
File metadata and controls
executable file
·94 lines (83 loc) · 3.55 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
#!/usr/bin/env python
import sys
import os
import requests
import yaml
import json
import shutil
def checkProperties(configs):
if configs['title'] == None or configs['title'] == "":
print("'title' is a required property")
sys.exit(2)
if configs['title'].strip() == None or configs['title'].strip() == "":
print("'title' property missing or badly formatted")
sys.exit(2)
if configs['path_to_data'] == None or configs['path_to_data'] == "":
print("'path_to_data' is a required property")
sys.exit(2)
if configs['access_token'] == None or configs['access_token'] == "":
print("'access_token' is a required property")
sys.exit(2)
def init_checks():
global configs
global base_url
global filename
global access_token
if os.path.exists("configs.yaml") == False:
print("Configuration file not found")
sys.exit(2)
with open("configs.yaml", 'r') as stream:
try:
configs = yaml.load(stream)
except yaml.YAMLError as exc:
print("Error loading configs.yaml file")
sys.exit(2)
checkProperties(configs)
if configs['path_to_data'][-1:] == '/': configs['path_to_data'] = configs['path_to_data'][:-1]
if os.path.exists(configs['path_to_data']) == False:
print("No such file or directory "+configs['path_to_data'])
sys.exit(2)
base_url = "https://sandbox.zenodo.org" if configs['sandbox'] == True else "https://zenodo.org"
filename = configs['path_to_data'].split('/')[::-1][0]
access_token = configs['access_token']
if filename == "":
print("property 'path_to_data' is wrong!")
sys.exit(2)
def checkResponse(r):
succeed = [200,201,202,204]
if r.status_code not in succeed:
print(json.dumps(r.json()))
sys.exit(2)
init_checks()
print("===========================================================================")
print(" Starting to upload '"+filename+"' to "+base_url+"...")
print("===========================================================================")
if os.path.isdir(configs['path_to_data']) == True:
print("Compressing directory to upload...")
shutil.make_archive(filename, 'zip', configs['path_to_data'])
filename = filename+".zip"
print("Creating upload...")
headers = {"Content-Type": "application/json"}
r = requests.post(base_url+"/api/deposit/depositions", params={'access_token': access_token}, json={}, headers=headers)
checkResponse(r)
print("Uploading file...")
deposition_id = r.json()['id']
data = {'filename': filename}
if os.path.isdir(configs['path_to_data']) == True:
r = requests.post(base_url+'/api/deposit/depositions/%s/files' % deposition_id, params={'access_token': access_token}, data=data, files={'file': open(filename, 'rb')})
else:
r = requests.post(base_url+'/api/deposit/depositions/%s/files' % deposition_id, params={'access_token': access_token}, data=data, files={'file': open(configs['path_to_data'], 'rb')})
checkResponse(r)
print("Adding metadata...")
metadata = {
'metadata': {
'title':configs['title'],
'upload_type':'other',
'description':'Uploaded using cloud-exporter'
}
}
r = requests.put(base_url+'/api/deposit/depositions/%s' % deposition_id, params={'access_token': access_token}, data=json.dumps(metadata), headers=headers)
checkResponse(r)
print("===========================================================================")
print(" Successfully uploaded '"+filename+"' to "+base_url)
print("===========================================================================")