Skip to content

Commit

Permalink
Add utility to upload support artefacts to S3.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Jul 27, 2017
1 parent b35b133 commit bfbfa9d
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ tests/temp
.pydevproject
tests/tmp*
.cache/
.env
7 changes: 7 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@

<target name="all" depends="java, android" />

<target name="upload" depends="java, android" description="Upload assets to S3">
<exec executable="python">
<arg value="tools/upload.py"/>
<arg value="b${release}"/>
</exec>
</target>

<target name="clean" description="Clean up">
<delete dir="${build}"/>
<delete dir="${dist}"/>
Expand Down
91 changes: 91 additions & 0 deletions tools/upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()

0 comments on commit bfbfa9d

Please sign in to comment.