forked from beeware/voc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.py
91 lines (76 loc) · 2.97 KB
/
upload.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
import os
import sys
import threading
from botocore.exceptions import ClientError
import boto3
from argparse import ArgumentParser
class Progress:
def __init__(self, index, count, filename):
self.index = index
self.count = count
self.filename = os.path.basename(filename)
self.size = float(os.path.getsize(filename))
self.seen_so_far = 0
self.lock = threading.Lock()
def __call__(self, bytes_amount):
# To simplify we'll assume this is hooked up
# to a single filename.
with self.lock:
self.seen_so_far += bytes_amount
percentage = (self.seen_so_far / self.size) * 100
print("[%s/%s] %s %s/%s (%.2f%%)" % (
self.index, self.count, self.filename,
self.seen_so_far, int(self.size),
percentage))
def upload(build, directory, s3_client):
filenames = []
for filename in os.listdir(directory):
full_filename = os.path.join(directory, filename)
if filename.endswith('-support.%s.tar.gz' % build):
filenames.append((filename, full_filename))
for i, (filename, full_filename) in enumerate(filenames):
py, version, platform, remainder = filename.split('-')
print("[%s/%s] %s..." % (i + 1, len(filenames), filename))
with open(full_filename, 'rb') as data:
s3_client.upload_fileobj(
data,
'pybee-briefcase-support',
'Python-Android-support/%s/%s/%s' % (
version,
platform,
filename
),
Callback=Progress(i+1, len(filenames), full_filename)
)
print()
def main():
parser = ArgumentParser()
parser.add_argument(
'--directory', '-d', default='dist', dest='directory',
help='Specify the directory containing artefacts.',
)
parser.add_argument('tag', metavar='tag', help='Build tag to upload.')
options = parser.parse_args()
# Load sensitive environment variables from a .env file
try:
with open('.env') as envfile:
for line in envfile:
if line.strip() and not line.startswith('#'):
key, value = line.strip().split('=', 1)
os.environ.setdefault(key.strip(), value.strip())
except FileNotFoundError:
pass
try:
aws_session = boto3.session.Session(
region_name=os.environ['AWS_REGION'],
aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'],
aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'],
)
s3_client = aws_session.client('s3')
print("Uploading %s support files..." % options.tag)
upload(options.tag, options.directory, s3_client)
except KeyError as e:
print("AWS environment variable %s not found" % e)
sys.exit(1)
if __name__ == '__main__':
main()