-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathcommon.py
295 lines (251 loc) · 9.48 KB
/
common.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
import click
import json
import random
import requests
import string
import sys
from bs4 import BeautifulSoup
from click import echo, style
from datetime import datetime
from dateutil import tz
from http import HTTPStatus
from pathlib import Path
from tqdm import tqdm
from evalai.utils.auth import get_request_header, get_host_url
from evalai.utils.config import EVALAI_ERROR_CODES
from evalai.utils.urls import URLS
class Date(click.ParamType):
"""
Date object parsed using datetime.
"""
name = "date"
def __init__(self, format):
self.format = format
def convert(self, value, param, ctx):
try:
date = datetime.strptime(value, self.format)
return date
except ValueError:
raise self.fail(
"Incorrect date format, please use {} format. Example: 8/23/17.".format(
self.format
)
)
def upload_file_to_s3(file, presigned_urls, max_chunk_size):
"""
Function to upload a file, given the target presigned s3 url
Arguments:
file_name (str) -- the path of the file to be uploaded
presigned_url (str) -- the presigned url to upload the file on s3
"""
echo(
style(
"Uploading the file...",
fg="green",
bold=False,
)
)
try:
parts = []
index = 0
file_size = Path(file.name).stat().st_size
for chunk_size in tqdm(range(0, file_size, max_chunk_size)):
presigned_url_object = presigned_urls[index]
part = presigned_url_object["partNumber"]
url = presigned_url_object["url"]
file_data = file.read(max_chunk_size)
response = requests.put(url, data=file_data)
if response.status_code != HTTPStatus.OK:
response.raise_for_status()
etag = response.headers["ETag"]
parts.append({"ETag": etag, "PartNumber": part})
index += 1
response = {"success": True, "parts": parts}
except Exception as err:
echo(style("\nThere was an error while uploading the file: {}".format(err), fg="red", bold=True))
response = {
"success": False,
"parts": []
}
return response
def validate_token(response):
"""
Function to check if the authentication token provided by user is valid or not.
"""
if "detail" in response:
if response["detail"] == "Invalid token":
echo(
style(
"\nThe authentication token you are using isn't valid."
" Please generate it again.\n",
bold=True,
fg="red",
)
)
sys.exit(1)
if response["detail"] == "Token has expired":
echo(
style(
"\nSorry, the token has expired. Please generate it again.\n",
bold=True,
fg="red",
)
)
sys.exit(1)
def validate_date_format(date):
for date_format in ("%Y-%m-%dT%H:%M:%S.%fZ", "%Y-%m-%dT%H:%M:%SZ"):
try:
return datetime.strptime(date, date_format)
except ValueError:
pass
raise ValueError("Invalid date format. Please check again.")
def convert_UTC_date_to_local(date):
# Format date
date = validate_date_format(date)
from_zone = tz.tzutc()
to_zone = tz.tzlocal()
# Convert to local timezone from UTC.
date = date.replace(tzinfo=from_zone)
converted_date = date.astimezone(to_zone)
date = converted_date.strftime("%D %r")
return date
def clean_data(data):
"""
Strip HTML and clean spaces
"""
data = BeautifulSoup(data, "lxml").text.strip()
data = " ".join(data.split()).encode("utf-8")
return data
def notify_user(message, color="green", bold=False):
echo(style(message, fg=color, bold=bold))
def generate_random_string(length):
letter_set = string.ascii_lowercase + string.digits
return "".join(random.choice(letter_set) for _ in range(length))
def publish_submission_message(challenge_phase_pk, submission_pk, headers):
url = "{}{}".format(get_host_url(), URLS.send_submission_message.value)
url = url.format(challenge_phase_pk, submission_pk)
response = requests.post(
url,
headers=headers,
)
return response
def upload_file_using_presigned_url(challenge_phase_pk, file, file_type, submission_metadata={}):
if file_type == "submission":
url = "{}{}".format(
get_host_url(), URLS.get_presigned_url_for_submission_file.value
)
finish_upload_url = "{}{}".format(
get_host_url(), URLS.finish_upload_for_submission_file.value
)
elif file_type == "annotation":
url = "{}{}".format(
get_host_url(), URLS.get_presigned_url_for_annotation_file.value
)
finish_upload_url = "{}{}".format(
get_host_url(), URLS.finish_upload_for_annotation_file.value
)
url = url.format(challenge_phase_pk)
headers = get_request_header()
# Limit to max 100 MB chunk for multipart upload
max_chunk_size = 20 * 1024 * 1024
try:
# Fetching the presigned url
if file_type == "submission":
file_size = Path(file.name).stat().st_size
num_file_chunks = int(file_size / max_chunk_size) + 1
data = {
"status": "submitting",
"file_name": file.name,
"num_file_chunks": num_file_chunks,
}
data = dict(data, **submission_metadata)
response = requests.post(url, headers=headers, data=data)
if response.status_code is not HTTPStatus.CREATED:
response.raise_for_status()
# Update url params for multipart upload on S3
finish_upload_url = finish_upload_url.format(
challenge_phase_pk, response.json().get("submission_pk")
)
elif file_type == "annotation":
file_size = Path(file.name).stat().st_size
num_file_chunks = int(file_size / max_chunk_size) + 1
data = {"file_name": file.name, "num_file_chunks": num_file_chunks}
response = requests.post(url, headers=headers, data=data)
if response.status_code is not HTTPStatus.OK:
response.raise_for_status()
# Update url params for multipart upload on S3
finish_upload_url = finish_upload_url.format(challenge_phase_pk)
response = response.json()
presigned_urls = response.get("presigned_urls")
upload_id = response.get("upload_id")
if file_type == "submission":
submission_pk = response.get("submission_pk")
# Uploading the file to S3
response = upload_file_to_s3(file, presigned_urls, max_chunk_size)
if not response["success"] and file_type == "submission":
# Publishing submission message to the message queue for processing
response = publish_submission_message(challenge_phase_pk, submission_pk, headers)
response.raise_for_status()
data = {
"parts": json.dumps(response.get("parts")),
"upload_id": upload_id,
}
if file_type == "annotation":
data["annotations_uploaded_using_cli"] = True
# Complete multipart S3 upload
upload_response = requests.post(
finish_upload_url, headers=headers, data=data
)
if file_type == "submission":
# Publishing submission message to the message queue for processing
response = publish_submission_message(challenge_phase_pk, submission_pk, headers)
response.raise_for_status()
# Publish submission before throwing submission upload error
if upload_response.status_code is not HTTPStatus.OK:
upload_response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code in EVALAI_ERROR_CODES:
validate_token(response.json())
if file_type == "submission":
error_message = "\nThere was an error while making the submission: {}\n".format(
response.json()["error"]
)
elif file_type == "annotation":
error_message = "\nThere was an error while uploading the annotation file: {}".format(
response.json()["error"]
)
echo(
style(
error_message,
fg="red",
bold=True,
)
)
else:
echo(style("{}".format(err), fg="red"))
sys.exit(1)
except requests.exceptions.RequestException:
echo(
style(
"\nCould not establish a connection to EvalAI."
" Please check the Host URL.\n",
bold=True,
fg="red",
)
)
sys.exit(1)
if file_type == "submission":
success_message = "\nYour submission {} with the id {} is successfully submitted for evaluation.\n".format(
file.name, submission_pk
)
elif file_type == "annotation":
success_message = "\nThe annotation file {} for challenge phase {} is successfully uploaded.\n".format(
file.name, challenge_phase_pk
)
echo(
style(
success_message,
fg="green",
bold=True,
)
)