# Path: sprinter/lib/command.py
# def call(command, stdin=None, stdout=subprocess.PIPE, env=os.environ, cwd=None,
#          shell=False, output_log_level=logging.INFO, sensitive_info=False):
#     """ Better, smarter call logic """
#     if not sensitive_info:
#         logger.debug("calling command: %s" % command)
#     else:
#         logger.debug("calling command with sensitive information")
#     try:
#         args = command if shell else whitespace_smart_split(command)
#         kw = {}
#         if not shell and not which(args[0], cwd=cwd):
#             raise CommandMissingException(args[0])
#         if shell:
#             kw['shell'] = True
#         process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=stdout,
#                                    stderr=subprocess.STDOUT, env=env, cwd=cwd,
#                                    **kw)
#         output = process.communicate(input=stdin)[0]
#         if output is not None:
#             try:
#                 logger.log(output_log_level, output.decode('utf-8'))
#             except UnicodeDecodeError:
#                 pass
#         return (process.returncode, output)
#     except OSError:
#         e = sys.exc_info()[1]
#         if not sensitive_info:
#             logger.exception("Error running command: %s" % command)
#             logger.error("Root directory: %s" % cwd)
#             if stdin:
#                 logger.error("stdin: %s" % stdin)
#         raise e
# 
# Path: sprinter/lib/request.py
# def download_to_bytesio(url):
#     """ Return a bytesio object with a download bar """
#     logger.info("Downloading url: {0}".format(url))
#     r = cleaned_request('get', url, stream=True)
#     stream = io.BytesIO()
#     total_length = int(r.headers.get('content-length'))
#     for chunk in progress.bar(r.iter_content(chunk_size=1024), expected_size=(total_length/1024) + 1):
#         if chunk:
#             stream.write(chunk)
#     stream.seek(0)
#     return stream


import os
import shutil
import sys
import tarfile
import tempfile
import zipfile
from .command import call
from .request import download_to_bytesio
            os.makedirs(target_dir)
        tf = tarfile.TarFile.open(fileobj=download_to_bytesio(url))
        if not os.path.exists(target_dir):
            os.makedirs(target_dir)
        common_prefix = os.path.commonprefix(tf.getnames())
        if not common_prefix.endswith('/'):
            common_prefix += "/"
        for tfile in tf.getmembers():
            if remove_common_prefix:
                tfile.name = tfile.name.replace(common_prefix, "", 1)
            if tfile.name != "":
                target_path = os.path.join(target_dir, tfile.name)
                if target_path != target_dir and os.path.exists(target_path):
                    if overwrite:
                        remove_path(target_path)
                    else:
                        continue
                tf.extract(tfile, target_dir)
    except OSError:
        e = sys.exc_info()[1]
        raise ExtractException(str(e))
    except IOError:
        e = sys.exc_info()[1]
        raise ExtractException(str(e))


def extract_zip(url, target_dir, remove_common_prefix=False, overwrite=False):
    try:
        if not os.path.exists(target_dir):
            os.makedirs(target_dir)

# Path: sprinter/lib/command.py
# def call(command, stdin=None, stdout=subprocess.PIPE, env=os.environ, cwd=None,
#          shell=False, output_log_level=logging.INFO, sensitive_info=False):
#     """ Better, smarter call logic """
#     if not sensitive_info:
#         logger.debug("calling command: %s" % command)
#     else:
#         logger.debug("calling command with sensitive information")
#     try:
#         args = command if shell else whitespace_smart_split(command)
#         kw = {}
#         if not shell and not which(args[0], cwd=cwd):
#             raise CommandMissingException(args[0])
#         if shell:
#             kw['shell'] = True
#         process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=stdout,
#                                    stderr=subprocess.STDOUT, env=env, cwd=cwd,
#                                    **kw)
#         output = process.communicate(input=stdin)[0]
#         if output is not None:
#             try:
#                 logger.log(output_log_level, output.decode('utf-8'))
#             except UnicodeDecodeError:
#                 pass
#         return (process.returncode, output)
#     except OSError:
#         e = sys.exc_info()[1]
#         if not sensitive_info:
#             logger.exception("Error running command: %s" % command)
#             logger.error("Root directory: %s" % cwd)
#             if stdin:
#                 logger.error("stdin: %s" % stdin)
#         raise e
# 
# Path: sprinter/lib/request.py
# def download_to_bytesio(url):
#     """ Return a bytesio object with a download bar """
#     logger.info("Downloading url: {0}".format(url))
#     r = cleaned_request('get', url, stream=True)
#     stream = io.BytesIO()
#     total_length = int(r.headers.get('content-length'))
#     for chunk in progress.bar(r.iter_content(chunk_size=1024), expected_size=(total_length/1024) + 1):
#         if chunk:
#             stream.write(chunk)
#     stream.seek(0)
#     return stream


# Path: sprinter/lib/extract.py
import os
import shutil
import sys
import tarfile
import tempfile
import zipfile
from .command import call
from .request import download_to_bytesio

            os.makedirs(target_dir)
        tf = tarfile.TarFile.open(fileobj=download_to_bytesio(url))
        if not os.path.exists(target_dir):
            os.makedirs(target_dir)
        common_prefix = os.path.commonprefix(tf.getnames())
        if not common_prefix.endswith('/'):
            common_prefix += "/"
        for tfile in tf.getmembers():
            if remove_common_prefix:
                tfile.name = tfile.name.replace(common_prefix, "", 1)
            if tfile.name != "":
                target_path = os.path.join(target_dir, tfile.name)
                if target_path != target_dir and os.path.exists(target_path):
                    if overwrite:
                        remove_path(target_path)
                    else:
                        continue
                tf.extract(tfile, target_dir)
    except OSError:
        e = sys.exc_info()[1]
        raise ExtractException(str(e))
    except IOError:
        e = sys.exc_info()[1]
        raise ExtractException(str(e))


def extract_zip(url, target_dir, remove_common_prefix=False, overwrite=False):
    try:
        if not os.path.exists(target_dir):
            os.makedirs(target_dir)

        memory_file = download_to_bytesio(url)