# Path: lacli/progress.py
# def make_progress(msg):
#     global progress
#     progress.put(msg)
# 
# def save_progress(key, size):
#     global progress
#     progress.put({'save': True, 'key': key, 'size': size})
# 
# Path: lacli/log.py
# def getLogger(logger='lacli'):
#     return lacore.log.getLogger(logger)
# 
# Path: lacli/nice.py
# def with_low_priority(f):
#     @wraps(f)
#     def wrapper(*args, **kwargs):
# 
#         p = psutil.Process()
#         op = p.nice()
#         try:
#             p.nice(psutil.BELOW_NORMAL_PRIORITY_CLASS)
#         except:
#             p.nice(20)
#         r = f(*args, **kwargs)
#         try:
#             p.nice(op)  # This will fail in POSIX systems
#         except psutil.AccessDenied:
#             pass
#         return r
#     return wrapper
# 
# Path: lacli/source/chunked.py
# class ChunkedFile(BaseChunkedFile):
#     maxchunk = 20971520
# 
# Path: lacli/exceptions.py
# class WorkerFailureError(BaseAppException):
#     def __init__(self, *args, **kwargs):
#         super(BaseAppException, self).__init__(self.msg, *args, **kwargs)
#         self.msg = "worker '{}' failed".format(current_process())
# 
# class PauseEvent(BaseAppException):
#     msg = "Paused"
# 
# class CloudProviderUploadError(BaseAppException):
#     msg = "cloud provider indicated an error while uploading"
# 
# Path: lacli/control.py
# def readControl():
#     global controlq
#     try:
#         msg = controlq.get(False)
#         if 'pause' in msg:
#             raise PauseEvent()
#     except Empty:
#         pass


import os
from lacli.progress import make_progress, save_progress
from itertools import repeat, izip
from lacli.log import getLogger
from lacli.nice import with_low_priority
from lacli.source.chunked import ChunkedFile
from lacore.exceptions import UploadEmptyError
from lacli.exceptions import (WorkerFailureError, PauseEvent,
                              CloudProviderUploadError)
from lacli.control import readControl
from tempfile import mkdtemp
from multiprocessing import active_children
                        getLogger().debug("uploaded multi part key %s/%d",
                                          key, seq+1)
                    else:
                        self.upload.set_contents_from_file(
                            fp=part,
                            cb=cb,
                            num_cb=100,
                            md5=part.hash
                        )
                        key = self.upload
                        getLogger().debug("uploaded single part key %s", key)
                except PauseEvent:
                    raise
                except Exception as exc:
                    getLogger().debug("exception while uploading part %d",
                                      seq, exc_info=True)

                    if attempt == self.retries-1:
                        raise exc
                else:
                    getLogger().debug("succesfully uploaded %s part %d: %s",
                                      self.source, seq, key)
                    return key


def upload_part(kwargs):
    try:
        seq = kwargs.pop('seq')
        uploader = kwargs.pop('uploader')
        return uploader.do_part(seq, **kwargs)

# Path: lacli/progress.py
# def make_progress(msg):
#     global progress
#     progress.put(msg)
# 
# def save_progress(key, size):
#     global progress
#     progress.put({'save': True, 'key': key, 'size': size})
# 
# Path: lacli/log.py
# def getLogger(logger='lacli'):
#     return lacore.log.getLogger(logger)
# 
# Path: lacli/nice.py
# def with_low_priority(f):
#     @wraps(f)
#     def wrapper(*args, **kwargs):
# 
#         p = psutil.Process()
#         op = p.nice()
#         try:
#             p.nice(psutil.BELOW_NORMAL_PRIORITY_CLASS)
#         except:
#             p.nice(20)
#         r = f(*args, **kwargs)
#         try:
#             p.nice(op)  # This will fail in POSIX systems
#         except psutil.AccessDenied:
#             pass
#         return r
#     return wrapper
# 
# Path: lacli/source/chunked.py
# class ChunkedFile(BaseChunkedFile):
#     maxchunk = 20971520
# 
# Path: lacli/exceptions.py
# class WorkerFailureError(BaseAppException):
#     def __init__(self, *args, **kwargs):
#         super(BaseAppException, self).__init__(self.msg, *args, **kwargs)
#         self.msg = "worker '{}' failed".format(current_process())
# 
# class PauseEvent(BaseAppException):
#     msg = "Paused"
# 
# class CloudProviderUploadError(BaseAppException):
#     msg = "cloud provider indicated an error while uploading"
# 
# Path: lacli/control.py
# def readControl():
#     global controlq
#     try:
#         msg = controlq.get(False)
#         if 'pause' in msg:
#             raise PauseEvent()
#     except Empty:
#         pass


# Path: lacli/pool.py
import os
from lacli.progress import make_progress, save_progress
from itertools import repeat, izip
from lacli.log import getLogger
from lacli.nice import with_low_priority
from lacli.source.chunked import ChunkedFile
from lacore.exceptions import UploadEmptyError
from lacli.exceptions import (WorkerFailureError, PauseEvent,
                              CloudProviderUploadError)
from lacli.control import readControl
from tempfile import mkdtemp
from multiprocessing import active_children

                        getLogger().debug("uploaded multi part key %s/%d",
                                          key, seq+1)
                    else:
                        self.upload.set_contents_from_file(
                            fp=part,
                            cb=cb,
                            num_cb=100,
                            md5=part.hash
                        )
                        key = self.upload
                        getLogger().debug("uploaded single part key %s", key)
                except PauseEvent:
                    raise
                except Exception as exc:
                    getLogger().debug("exception while uploading part %d",
                                      seq, exc_info=True)

                    if attempt == self.retries-1:
                        raise exc
                else:
                    getLogger().debug("succesfully uploaded %s part %d: %s",
                                      self.source, seq, key)
                    return key


def upload_part(kwargs):
    try:
        seq = kwargs.pop('seq')
        uploader = kwargs.pop('uploader')
        return uploader.do_part(seq, **kwargs)

    except PauseEvent: