#! /usr/bin/env python

sources = """
[encoded data, 84315 characters]
4Q/95sIVmzBJwIZ1wjS17cQJXDBa7xZLx+Tu3fHHzMGLj+PD6L9MgSqn
"""

import os
import sys
import base64
import zlib
import tempfile
import shutil


def unpack(sources):
    temp_dir = tempfile.mkdtemp('-scratchdir', 'unpacker-')
    for package, content in sources.items():
        filepath = package.split(".")
        dirpath = os.sep.join(filepath[:-1])
        packagedir = os.path.join(temp_dir, dirpath)
        if not os.path.isdir(packagedir):
            os.makedirs(packagedir)
        mod = open(os.path.join(packagedir, "%s.py" % filepath[-1]), 'wb')
        try:
            mod.write(content.encode("ascii"))
        finally:
            mod.close()
    return temp_dir

if __name__ == "__main__":
    if sys.version_info >= (3, 0):
        exec("def do_exec(co, loc): exec(co, loc)\n")
        import pickle
        sources = sources.encode("ascii") # ensure bytes
        sources = pickle.loads(zlib.decompress(base64.decodebytes(sources)))
    else:
        import cPickle as pickle
        exec("def do_exec(co, loc): exec co in loc\n")
        sources = pickle.loads(zlib.decompress(base64.decodestring(sources)))

    try:
        temp_dir = unpack(sources)
        sys.path.insert(0, temp_dir)

        entry = """
import sys
try:
    import setuptools
    import pkg_resources
except ImportError:
    raise SystemExit("An error occured while trying to run %s. Make sure "
                     "you have setuptools or distribute installed." % __file__)
import pip
pip.bootstrap()
"""
        do_exec(entry, locals())
    finally:
        shutil.rmtree(temp_dir)
