# Path: mongodb_consistent_backup/Common/Util.py
# def file_md5hash(file_path, blocksize=65536):
#     md5hash = md5()
#     with open(file_path, "rb") as f:
#         for block in iter(lambda: f.read(blocksize), b""):
#             md5hash.update(block)
#     return md5hash.hexdigest()
# 
# Path: mongodb_consistent_backup/Errors.py
# class OperationError(Error):
#     """Raised when an operation failed in an expected but unrecoverable way"""
#     pass


import boto.s3.multipart
import logging
import os
from boto.s3.key import Key
from copy_reg import pickle
from math import ceil
from multiprocessing import Pool, TimeoutError
from time import sleep
from types import MethodType
from S3Session import S3Session
from S3UploadThread import S3UploadThread
from mongodb_consistent_backup.Common.Util import file_md5hash
from mongodb_consistent_backup.Errors import OperationError
                    logging.info("Uploaded AWS S3 key successfully: s3://%s%s" % (self.bucket_name, key_name))

    def complete(self, output_tuple):
        file_name, key_name, mp_num = output_tuple
        if file_name:
            upload = self._uploads[file_name]
            logging.debug("Got success callback for upload: s3://%s%s (multipart: %s)" % (self.bucket_name, key_name, mp_num))
            if mp_num and "parts" in upload and mp_num in upload["parts"]:
                upload["parts"][mp_num]["complete"] = True
                self.complete_multipart(file_name, key_name)
            else:
                upload["complete"] = True
                self.set_key_acl(key_name)
                if self.remove_uploaded:
                    self.remove_file(file_name)
                logging.info("Uploaded AWS S3 key successfully: s3://%s%s" % (self.bucket_name, key_name))

    def get_multipart_upload(self, file_name, key_name):
        if file_name not in self._mp_uploads:
            self._mp_uploads[file_name] = {
                "upload": self.bucket.initiate_multipart_upload(key_name),
                "complete": False
            }
        return self._mp_uploads[file_name]["upload"]

    def get_file_size(self, file_name):
        if os.path.isfile(file_name):
            return os.stat(file_name).st_size
        else:
            logging.error("Upload file does not exist (or is not a file): %s" % file_name)

# Path: mongodb_consistent_backup/Common/Util.py
# def file_md5hash(file_path, blocksize=65536):
#     md5hash = md5()
#     with open(file_path, "rb") as f:
#         for block in iter(lambda: f.read(blocksize), b""):
#             md5hash.update(block)
#     return md5hash.hexdigest()
# 
# Path: mongodb_consistent_backup/Errors.py
# class OperationError(Error):
#     """Raised when an operation failed in an expected but unrecoverable way"""
#     pass


# Path: mongodb_consistent_backup/Upload/S3/S3UploadPool.py
import boto.s3.multipart
import logging
import os
from boto.s3.key import Key
from copy_reg import pickle
from math import ceil
from multiprocessing import Pool, TimeoutError
from time import sleep
from types import MethodType
from S3Session import S3Session
from S3UploadThread import S3UploadThread
from mongodb_consistent_backup.Common.Util import file_md5hash
from mongodb_consistent_backup.Errors import OperationError

                    logging.info("Uploaded AWS S3 key successfully: s3://%s%s" % (self.bucket_name, key_name))

    def complete(self, output_tuple):
        file_name, key_name, mp_num = output_tuple
        if file_name:
            upload = self._uploads[file_name]
            logging.debug("Got success callback for upload: s3://%s%s (multipart: %s)" % (self.bucket_name, key_name, mp_num))
            if mp_num and "parts" in upload and mp_num in upload["parts"]:
                upload["parts"][mp_num]["complete"] = True
                self.complete_multipart(file_name, key_name)
            else:
                upload["complete"] = True
                self.set_key_acl(key_name)
                if self.remove_uploaded:
                    self.remove_file(file_name)
                logging.info("Uploaded AWS S3 key successfully: s3://%s%s" % (self.bucket_name, key_name))

    def get_multipart_upload(self, file_name, key_name):
        if file_name not in self._mp_uploads:
            self._mp_uploads[file_name] = {
                "upload": self.bucket.initiate_multipart_upload(key_name),
                "complete": False
            }
        return self._mp_uploads[file_name]["upload"]

    def get_file_size(self, file_name):
        if os.path.isfile(file_name):
            return os.stat(file_name).st_size
        else:
            logging.error("Upload file does not exist (or is not a file): %s" % file_name)

            raise OperationError("Upload file does not exist (or is not a file)!")