# 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

        uploaded = len(etags)
        total = self.source.chunks
        getLogger().debug("Uploaded {} out of {} chunks".format(
            uploaded, total))
        size = self.source.size
        if uploaded < total:
            for seq in xrange(uploaded, total):
                make_progress({'part': seq, 'tx': 0})
            skip = self.source.chunkstart(uploaded)
            newsource = ChunkedFile(
                self.source.path, skip=skip, chunk=self.source.chunk)
            size = size - newsource.size
        getLogger().debug("saving progress for {}".format(key))
        save_progress(name, size)
        return (key.etag, newsource)

    @with_low_priority
    def do_part(self, seq, **kwargs):
        """ transfer a part. runs in a separate process. """

        key = None
        for attempt in range(self.retries):
            getLogger().debug("attempt %d/%d to transfer part %d",
                              attempt, self.retries, seq)

            with self.source.chunkfile(seq, **kwargs) as part:
                try:
                    def cb(tx, total):
                        readControl()

# 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


        uploaded = len(etags)
        total = self.source.chunks
        getLogger().debug("Uploaded {} out of {} chunks".format(
            uploaded, total))
        size = self.source.size
        if uploaded < total:
            for seq in xrange(uploaded, total):
                make_progress({'part': seq, 'tx': 0})
            skip = self.source.chunkstart(uploaded)
            newsource = ChunkedFile(
                self.source.path, skip=skip, chunk=self.source.chunk)
            size = size - newsource.size
        getLogger().debug("saving progress for {}".format(key))
        save_progress(name, size)
        return (key.etag, newsource)

    @with_low_priority
    def do_part(self, seq, **kwargs):
        """ transfer a part. runs in a separate process. """

        key = None
        for attempt in range(self.retries):
            getLogger().debug("attempt %d/%d to transfer part %d",
                              attempt, self.retries, seq)

            with self.source.chunkfile(seq, **kwargs) as part:
                try:
                    def cb(tx, total):
                        readControl()

                        make_progress({'part': seq,