#!/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)

[((69, 94), 'os.path.dirname', 'os.path.dirname', (['__file__'], {}), '(__file__)\n', (84, 94), False, 'import os\n'), ((144, 176), 'sys.path.insert', 'sys.path.insert', (['(0)', 'NEWSBLUR_DIR'], {}), '(0, NEWSBLUR_DIR)\n', (159, 176), False, 'import sys\n'), ((1037, 1143), 'boto3.client', 'boto3.client', (['"""s3"""'], {'aws_access_key_id': 'settings.S3_ACCESS_KEY', 'aws_secret_access_key': 'settings.S3_SECRET'}), "('s3', aws_access_key_id=settings.S3_ACCESS_KEY,\n    aws_secret_access_key=settings.S3_SECRET)\n", (1049, 1143), False, 'import boto3\n'), ((1330, 1360), 'os.path.join', 'os.path.join', (['BACKUP_DIR', 'path'], {}), '(BACKUP_DIR, path)\n', (1342, 1360), False, 'import os\n'), ((1571, 1591), 'os.remove', 'os.remove', (['full_path'], {}), '(full_path)\n', (1580, 1591), False, 'import os\n'), ((1292, 1314), 'os.listdir', 'os.listdir', (['BACKUP_DIR'], {}), '(BACKUP_DIR)\n', (1302, 1314), False, 'import os\n'), ((467, 483), 'threading.Lock', 'threading.Lock', ([], {}), '()\n', (481, 483), False, 'import threading\n'), ((1153, 1173), 'socket.gethostname', 'socket.gethostname', ([], {}), '()\n', (1171, 1173), False, 'import socket\n'), ((1247, 1278), 'time.strftime', 'time.strftime', (['"""%Y-%m-%d-%H-%M"""'], {}), "('%Y-%m-%d-%H-%M')\n", (1260, 1278), False, 'import time\n'), ((389, 414), 'os.path.getsize', 'os.path.getsize', (['filename'], {}), '(filename)\n', (404, 414), False, 'import os\n'), ((739, 849), 'sys.stdout.write', 'sys.stdout.write', (["('\\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))\n", (755, 849), False, 'import sys\n'), ((915, 933), 'sys.stdout.flush', 'sys.stdout.flush', ([], {}), '()\n', (931, 933), False, 'import sys\n')]