#!/usr/bin/env python

"""
Simple script to quickly change the symbolic link to your nipy source branch.

When you build your source branch, perform an inplace build::

   cd trunk-lp # directory containing 'nipy' package directory
   python setup.py build_ext --inplace

Then ``mynipy`` will update the symlink in your site-packages directory
to point to the branch you specify.  This allows you to quickly switch
your working version of nipy without having to reinstall to your
site-packages or modify your PYTHONPATH.

Install
-------

We don't have a proper installer yet, simply copy this into a bin
directory and make sure it's permissions are executable.  For instance,
I installed mynipy in my local bin directory::

    $HOME/local/bin/mynipy

Usage
-----

From your nipy-repo, which has several nipy branches as
subdirectories like this::

    nipy-repo/trunk-lp
    nipy-repo/trunk-dev
    nipy-repo/trunk-mbrett

This will make the 'trunk-mbrett' my current nipy package::
    
    $ mynipy trunk-mbrett
    
"""

import os
from os.path import join as pjoin, split as psplit, \
    isdir, isfile, abspath
import sys
import subprocess

cwd = os.getcwd()
try:
    nipypath = abspath(sys.argv[1])
except IndexError: # default, current directory
    nipypath = cwd
nipy_pkg_path = pjoin(nipypath, 'nipy')

if not isdir(nipy_pkg_path):
    raise OSError('No nipy path in input path ' + nipypath)

print "Changing 'nipy' path to: \n  ", nipy_pkg_path, "\n"

# Find where nipy is now. If we're in a source branch directory, the
# 'import nipy' below would give us the source dir, not the
# site-packages directory, so we ..
if isdir('nipy') and isfile(pjoin('nipy', '__init__.py')):
    if cwd in sys.path:
        sys.path.remove(cwd)
    os.chdir('..')
try:
    import nipy
    site_pkgs, nipy_ln  = psplit(nipy.__path__[0])
except ImportError:
    # first time this script is run, we'll place it where numpy is installed
    try:
        import numpy
    except ImportError:
        raise ImportError('Unable to determine where to install nipy.')
    else:
        print 'First developer install of nipy,'
        print '\t installing nipy link with numpy is installed.'
        site_pkgs, numpy_ln  = psplit(numpy.__path__[0])
        site_pkgs = pjoin(site_pkgs, 'nipy')

cmd = 'ln -sf %s %s' % (nipy_pkg_path, site_pkgs)
print cmd
subprocess.call(cmd, shell=True)
