import os
import sys
import json
import shutil
from os.path import dirname, isdir, join



THIS_DIR = dirname(__file__)

PY_TMPL = """\
if __name__ == '__main__':
    import sys
    from %s import %s

    sys.exit(%s())
"""

prefix = sys.prefix
python = sys.executable


def _unlink(path):
    try:
        os.unlink(path)
    except OSError:
        pass


def create_entry_point(path, module, func):
    pyscript = PY_TMPL % (module, func, func)
    if sys.platform == 'win32':
        with open(path + '-script.py', 'w') as fo:
            fo.write(pyscript)
        shutil.copyfile(join(THIS_DIR,
                             'cli-%d.exe' % (8 * tuple.__itemsize__)),
                        path + '.exe')
    else:
        with open(path, 'w') as fo:
            fo.write('#!%s\n' % python)
            fo.write(pyscript)
        os.chmod(path, int('755', 8))


def create_entry_points(items, remove=False):
    if not items:
        return
    bin_dir = join(prefix, 'Scripts' if sys.platform == 'win32' else 'bin')
    if not isdir(bin_dir):
        os.mkdir(bin_dir)
    for cmd, module, func in items:
        path = join(bin_dir, cmd)
        if remove:
            if sys.platform == 'win32':
                _unlink(path + '-script.py')
                _unlink(path + '.exe')
            else:
                _unlink(path)
        else:
            create_entry_point(path, module, func)


def read_data():
    with open(join(THIS_DIR, 'data.json')) as fi:
        return json.load(fi)


def link():
    d = read_data()
    create_entry_points(d['entry_points'])


def unlink():
    d = read_data()
    create_entry_points(d['entry_points'], remove=True)


def main():
    from optparse import OptionParser
    p = OptionParser()
    p.add_option("--unlink", action="store_true")
    p.add_option("--verbose", action="store_true")
    opts, args = p.parse_args()

    if opts.unlink:
        unlink()
    else:
        link()


if __name__ == '__main__':
    main()

@@ -68,16 +68,24 @@
 dirname,
+ exists,
  isdir, 
@@ -89,16 +89,62 @@
 r, join%0A
+from distutils.sysconfig import get_python_lib
 %0A%0A%0ATHIS_
@@ -327,75 +327,1051 @@
 ef _
-unlink(path):%0A    try:%0A        os.unlink(path)%0A    except OSError:%0A
+link(src, dst):%0A    try:%0A        os.link(src, dst)%0A        # on Windows os.link raises AttributeError%0A    except (OSError, AttributeError):%0A        shutil.copy2(src, dst)%0A%0A%0Adef _unlink(path):%0A    try:%0A        os.unlink(path)%0A    except OSError:%0A        pass%0A%0A%0Adef link_files(src_root, dst_root, files):%0A    for f in files:%0A        src = join(src_root, f)%0A        dst = join(dst_root, f)%0A        dst_dir = dirname(dst)%0A        if not isdir(dst_dir):%0A            os.makedirs(dst_dir)%0A        if exists(dst):%0A            _unlink(dst)%0A        _link(src, dst)%0A%0A%0Adef unlink_files(root, files):%0A    dst_dirs1 = set()%0A    for f in files:%0A        path = join(root, f)%0A        _unlink(path)%0A        dst_dirs1.add(dirname(path))%0A%0A    dst_dirs2 = set()%0A    for path in dst_dirs1:%0A        while len(path) %3E len(prefix):%0A            dst_dirs2.add(path)%0A            path = dirname(path)%0A%0A    for path in sorted(dst_dirs2, key=len, reverse=True):%0A        try:%0A            os.rmdir(path)%0A        except OSError: # directory might not exist or not be empty%0A    
     
@@ -2600,16 +2600,146 @@
 ints'%5D)%0A
+    link_files(join(THIS_DIR, 'site-packages'),%0A               join(prefix, get_python_lib()),%0A               d%5B'site-packages'%5D)%0A
 %0A%0Adef un

add linking site-packages files