#!/usr/bin/python3
import os
import sys
import socket
CURRENT_DIR  = os.path.dirname(__file__)
NEWSBLUR_DIR = ''.join([CURRENT_DIR, '/../../'])
sys.path.insert(0, NEWSBLUR_DIR)
os.environ['DJANGO_SETTINGS_MODULE'] = 'newsblur_web.settings'

import threading

class ProgressPercentage(object):

    def __init__(self, filename):
        self._filename = filename
        self._size = float(os.path.getsize(filename))
        self._seen_so_far = 0
        self._lock = threading.Lock()

    def __call__(self, bytes_amount):
        # To simplify, 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
            sys.stdout.write(
                "\r%s  %s / %s  (%.2f%%)" % (
                    self._filename, self._seen_so_far, self._size,
                    percentage))
            sys.stdout.flush()

import time
import boto3
from django.conf import settings

BACKUP_DIR = '/srv/newsblur/backup/'

s3 = boto3.client('s3', aws_access_key_id=settings.S3_ACCESS_KEY, aws_secret_access_key=settings.S3_SECRET) 

hostname = socket.gethostname().replace('-','_')
s3_object_name = f'backup_{hostname}/backup_{hostname}_{time.strftime("%Y-%m-%d-%H-%M")}.sql'
path = os.listdir(BACKUP_DIR)[0]
full_path = os.path.join(BACKUP_DIR, path)
print('Uploading %s to %s on S3 bucket %s' % (full_path, s3_object_name, settings.S3_BACKUP_BUCKET))
s3.upload_file(full_path, settings.S3_BACKUP_BUCKET, s3_object_name, Callback=ProgressPercentage(full_path))
os.remove(full_path)

[((5, 15, 5, 40), 'os.path.dirname', 'os.path.dirname', ({(5, 31, 5, 39): '__file__'}, {}), '(__file__)', False, 'import os\n'), ((7, 0, 7, 32), 'sys.path.insert', 'sys.path.insert', ({(7, 16, 7, 17): '(0)', (7, 19, 7, 31): 'NEWSBLUR_DIR'}, {}), '(0, NEWSBLUR_DIR)', False, 'import sys\n'), ((37, 5, 37, 107), 'boto3.client', 'boto3.client', (), '', False, 'import boto3\n'), ((42, 12, 42, 42), 'os.path.join', 'os.path.join', ({(42, 25, 42, 35): 'BACKUP_DIR', (42, 37, 42, 41): 'path'}, {}), '(BACKUP_DIR, path)', False, 'import os\n'), ((45, 0, 45, 20), 'os.remove', 'os.remove', ({(45, 10, 45, 19): 'full_path'}, {}), '(full_path)', False, 'import os\n'), ((41, 7, 41, 29), 'os.listdir', 'os.listdir', ({(41, 18, 41, 28): 'BACKUP_DIR'}, {}), '(BACKUP_DIR)', False, 'import os\n'), ((18, 21, 18, 37), 'threading.Lock', 'threading.Lock', ({}, {}), '()', False, 'import threading\n'), ((39, 11, 39, 31), 'socket.gethostname', 'socket.gethostname', ({}, {}), '()', False, 'import socket\n'), ((40, 56, 40, 87), 'time.strftime', 'time.strftime', ({(40, 70, 40, 86): '"""%Y-%m-%d-%H-%M"""'}, {}), "('%Y-%m-%d-%H-%M')", False, 'import time\n'), ((16, 27, 16, 52), 'os.path.getsize', 'os.path.getsize', ({(16, 43, 16, 51): 'filename'}, {}), '(filename)', False, 'import os\n'), ((25, 12, 28, 32), 'sys.stdout.write', 'sys.stdout.write', ({(26, 16, 28, 31): "('\\r%s  %s / %s  (%.2f%%)' % (self._filename, self._seen_so_far, self._size,\n    percentage))"}, {}), "('\\r%s  %s / %s  (%.2f%%)' % (self._filename, self.\n    _seen_so_far, self._size, percentage))", False, 'import sys\n'), ((29, 12, 29, 30), 'sys.stdout.flush', 'sys.stdout.flush', ({}, {}), '()', False, 'import sys\n')]