-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-upload.py
More file actions
77 lines (61 loc) · 1.96 KB
/
test-upload.py
File metadata and controls
77 lines (61 loc) · 1.96 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
import os
import json
from hashlib import sha256
import http.client
from http import HTTPStatus
import time
import struct
import random
import glob
FILE_READ_BUFFER = 4096
path = os.path.join(os.getcwd(), os.environ['ARTIFACT_PATTERN'])
files = glob.glob(path, recursive=False)
uploading_files = []
for archive_file in files:
file = open(archive_file, 'rb')
sha256_hash = sha256()
file.seek(0, 0)
for byte_block in iter(lambda: file.read(FILE_READ_BUFFER), b""):
sha256_hash.update(byte_block)
checksum = sha256_hash.hexdigest()
uploading_files.append({
"filename": os.path.basename(archive_file),
"sha256_checksum": checksum,
"file_length": file.tell()
})
file.close()
print("BuildFileHashes: " + json.dumps(uploading_files))
file_contents = []
file_sizes = []
for archiveFile in files:
file = open(archiveFile, 'rb')
file_data = file.read()
file_sizes.append(len(file_data))
file_contents.append(file_data)
file.close()
conn = http.client.HTTPSConnection("build-uploader.vircadia.com")
context = json.loads(os.environ['GITHUB_CONTEXT'])
owner_and_repository = context["repository"].split("/")
owner = owner_and_repository[0]
repository = owner_and_repository[1]
headers = {
"owner": owner,
"repo": repository,
"commit_hash": context["event"]["pull_request"]["head"]["sha"],
"pull_number": context["event"]["number"],
"job_name": os.environ["JOB_NAME"],
"run_id": context["run_id"],
"file_sizes": ','.join(str(e) for e in file_sizes)
}
concat_file_body = b''.join(file_contents)
print("Total files size: " + str(len(concat_file_body)))
conn.request("PUT", "/", body=concat_file_body, headers=headers)
response = conn.getresponse()
EXIT_CODE_OK = 0
EXIT_CODE_ERROR = 1
if (response.status == HTTPStatus.OK):
print("response: ", json.loads(response.read()))
exit(EXIT_CODE_OK)
else:
print(response.status, response.reason, response.read())
exit(EXIT_CODE_ERROR)