# 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
        self.key = key
        self.upload_id = None
        self.complete = None
        self.tempdir = None
        self.step = step

    def __str__(self):
        return "<MPUpload key={} id={} source={}>".format(
            self.key, self.upload_id, self.source)

    def iterargs(self, chunks):
        partinfo = repeat(None)
        if not self.source.isfile:
            partinfo = self.source._savedchunks(self.tempdir)
        for seq, info in izip(xrange(chunks), partinfo):
            yield {'uploader': self, 'seq': seq, 'fname': info}

    def _getupload(self):
        if self.source.chunks > 1 and self.source.isfile:
            if self.upload_id is None:
                return self.conn.newupload(self.key)
            else:
                return self.conn.getupload(self.upload_id)
        else:
            return self.conn.newkey(self.key)

    def __enter__(self):
        try:
            self.upload = self._getupload()
        except Exception as e:

# 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

        self.key = key
        self.upload_id = None
        self.complete = None
        self.tempdir = None
        self.step = step

    def __str__(self):
        return "<MPUpload key={} id={} source={}>".format(
            self.key, self.upload_id, self.source)

    def iterargs(self, chunks):
        partinfo = repeat(None)
        if not self.source.isfile:
            partinfo = self.source._savedchunks(self.tempdir)
        for seq, info in izip(xrange(chunks), partinfo):
            yield {'uploader': self, 'seq': seq, 'fname': info}

    def _getupload(self):
        if self.source.chunks > 1 and self.source.isfile:
            if self.upload_id is None:
                return self.conn.newupload(self.key)
            else:
                return self.conn.getupload(self.upload_id)
        else:
            return self.conn.newkey(self.key)

    def __enter__(self):
        try:
            self.upload = self._getupload()
        except Exception as e:

            getLogger().debug("error getting upload", exc_info=True)