"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/qp/setup.py $
$Id: setup.py 29024 2006-11-01 15:57:20Z dbinger $
"""
import os
import sys
from os import curdir
from os.path import join, exists
from distutils.core import setup
from distutils.extension import Extension
from shutil import copytree

try:
    from qpy.setup import qpy_build_py
except ImportError:
    print "Please install Qpy first."
    raise SystemExit

from __init__ import __version__

if 'sdist' in sys.argv:
    import qpy
    import durus
    import sancho
    import re

    # Make sure that version numbers have all been updated.
    PAT = re.compile(r'\b%s\b' % re.escape(__version__))
    assert len(PAT.findall(open("LICENSE.txt").read())) == 14, __version__
    assert PAT.search(open("CHANGES.txt").readline()), __version__
    assert len(PAT.findall(open("README.txt").read())) == 3, __version__
    QPYPAT = re.compile(r'\b%s\b' % re.escape(qpy.__version__))
    assert len(QPYPAT.findall(open("README.txt").read())) == 3, qpy.__version__
    DURUSPAT = re.compile(r'\b%s\b' % re.escape(durus.__version__))
    assert len(DURUSPAT.findall(open("README.txt").read())) == 3, durus.__version__
    SANCHOPAT = re.compile(r'\b%s\b' % re.escape(sancho.__version__))
    assert len(SANCHOPAT.findall(open("README.txt").read())) == 1, sancho.__version__    

    # Make sure that copyright statements are current.
    from datetime import datetime
    year = datetime.now().year
    copyright = "Copyright (c) Corporation for National Research Initiatives %s" % year
    assert open("README.txt").read().count(copyright) == 1

if __name__ == '__main__':
    if len(sys.argv) == 3 and sys.argv[1] == 'install_demo_sites':
        sites_directory = sys.argv[2]
        if exists(sites_directory):
            print sites_directory, 'already exists.'
            print "To install the demo sites, copy them "
            print "from the 'demo' directory into %r" % sites_directory
        else:
            copytree('demo', sites_directory)
            f = open(join(sites_directory, "__init__.py"), "w")
            f.close()
    else:
        if os.name == 'posix':
            Passfd = Extension(name="qp.hub.passfd", sources=['hub/passfd.c'])
            ext_modules = [Passfd]
        else:
            ext_modules = []
        setup(name="qp",
              version=__version__,
              author="CNRI",
              author_email="webmaster@mems-exchange.org",
              url="http://www.mems-exchange.org/software/qp/",
              package_dir=dict(qp=curdir),
              scripts=['bin/qp', 'bin/qpcensus.py'],
              license="See LICENSE.txt",
              packages=['qp',
                        'qp.http',
                        'qp.hub',
                        'qp.pub',
                        'qp.fill',
                        'qp.lib',
                        'qp.mail',
                        'qp.sites',
                        ],
              ext_modules = ext_modules,
              description="A web-application framework.",
              cmdclass= {'build_py': qpy_build_py},
              long_description=(
            "QP is a package for defining and running multiple web "
            "applications based on Durus for persistence, standard persistent "
            "Session and User classes, easy interactive database sessions, "
            "qpy for assembling html, and Quixote2-style forms and path "
            "traversal.  ")
              )
