# 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
        if step is None:
            step = len(active_children()) or 5
        if chunks > step:
            chunks = step
        return pool.imap(upload_part, self.iterargs(chunks))

    def get_result(self, rs):
        etags = []
        key = name = None
        newsource = None
        if rs is not None:
            try:
                while True:
                    key = rs.next(self.conn.timeout())
                    getLogger().debug("got key {} with etag: {}".format(
                        key.name, key.etag))
                    etags.append(key.etag)
            except StopIteration:
                pass
            except WorkerFailureError:
                getLogger().debug("error getting result.", exc_info=True)

        if not etags:
            raise UploadEmptyError()

        if hasattr(self.upload, 'complete_upload'):
            try:
                key = self.conn.complete_multipart(self.upload, etags)
            except Exception as e:
                getLogger().debug("error completing multipart", exc_info=True)

# 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

        if step is None:
            step = len(active_children()) or 5
        if chunks > step:
            chunks = step
        return pool.imap(upload_part, self.iterargs(chunks))

    def get_result(self, rs):
        etags = []
        key = name = None
        newsource = None
        if rs is not None:
            try:
                while True:
                    key = rs.next(self.conn.timeout())
                    getLogger().debug("got key {} with etag: {}".format(
                        key.name, key.etag))
                    etags.append(key.etag)
            except StopIteration:
                pass
            except WorkerFailureError:
                getLogger().debug("error getting result.", exc_info=True)

        if not etags:
            raise UploadEmptyError()

        if hasattr(self.upload, 'complete_upload'):
            try:
                key = self.conn.complete_multipart(self.upload, etags)
            except Exception as e:
                getLogger().debug("error completing multipart", exc_info=True)

                raise CloudProviderUploadError(e)