"""Distutils based setup script for Martel.

This uses Distutils (http://python.org/sigs/distutils-sig/) the
standard python mechanism for installing packages. For the easiest
installation just type the command:

  python setup.py install

For more details about the options available from distutils, look at
the 'Installing Python Modules' distutils documentation, available
from:

  http://python.org/sigs/distutils-sig/doc/

Or, if all else fails, feel free to write to the biopython list
at biopython@biopython.org and ask for help.
"""

# This setup.py is a modified vesion of the standard Biopython
# setup.py, which explains why some of the code is overkill :)

# Martel includes 'sre_parse.py' and 'sre_constants.py' which are
# needed by Python's own sre regular expressions.  I'll play some
# nasty tricks to import what Python expects.  Perhaps instead I
# should change the file names?
import sys
sys_path = sys.path
while 1:
    try:
        sys.path.remove("")
    except ValueError:
        break
import sre_constants, sre_parse
sys.path = sys_path
        

import sys
import os
try:
    from distutils.core import setup
    from distutils.command.install import install
    from distutils.core import Command
except ImportError:
    print "Martel installation requires distutils, avaiable with python 2.0"
    print "or better, or from:"
    print "  http://python.org/sigs/distutils-sig/download.html"
    sys.exit(0)


# --- check for installed programs needed by Martel

def check_install(name, check_library, location, other_messages = None):
    """Check if a program is installed and print a warning message if not.

    This helps users at least know they are missing some installed stuff
    and where to get it when they install Martel.

    Arguments:
    
    o check_library -- a function to check whether or not the specified
    program and version is present, returns 1 if it is, 0 otherwise.

    o name -- the name of the library we are looking for

    o location -- a URL where the library can be downloaded

    o other_messages -- other random messages to print if the library
    is not present (ie. version information, etc...)
    """
    if not(check_library()):
        print "\nWARNING -- %s is not installed." % name
        print "You should install this from:"
        print location
        print "because otherwise Martel will not be useful"
        if other_messages:
            print other_messages

# -- functions to check for specific libraries and versions.

def check_mxTextTools():
    try:
        from mx import TextTools
        return 1
    except ImportError:
        pass
    try:
        import TextTools
        return 1
    except ImportError:
        pass
    return 0

class my_install(install):
    """Override the standard install to check for dependencies.

    This will just run the normal install, and then print warning messages
    if packages are missing.
    """
    def run(self):
        # run the normal install and everthing
        install.run(self)

        # now print warning messages if we are missing stuff
        check_install("mxTextTools", check_mxTextTools,
                      "http://www.lemburg.com/files/python/mxExtensions.html")

class run_tests(Command):
    """Run all of the tests for the package.

    This is a automatic test run class to make distutils kind of act like
    perl. With this you can do:

    python setup.py build
    python setup.py install
    python setup.py test
    """
    description = "Automatically run the test suite for the package."

    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        this_dir = os.getcwd()

        # change to the test dir and run the tests
        os.chdir("test")
        import run_tests
        run_tests.main([])

        # change back to the current directory
        os.chdir(this_dir)

setup(name='Martel', 
      version='0.6',
      author='Dalke Scientific Software, LLC of the The Biopython Consortium',
      author_email='dalke@acm.org',
      url='http://www.biopython.org/~dalke/Martel/',

      cmdclass = {"install" : my_install,
                  "test" : run_tests},
      package_dir = {"Martel": ""},
      packages = ["Martel"],
      
      )

