"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/qpy/setup.py $
$Id: setup.py 28588 2006-08-15 17:11:08Z dbinger $
"""
from distutils.core import setup
from distutils.command.build_py import build_py
from distutils.extension import Extension
from glob import glob
import os, sys


class qpy_build_py(build_py):

    def find_package_modules(self, package, package_dir):
        self.check_package(package, package_dir)
        module_files = (glob(os.path.join(package_dir, "*.py")) +
                        glob(os.path.join(package_dir, "*.qpy")))
        modules = []
        setup_script = os.path.abspath(self.distribution.script_name)
        for f in module_files:
            abs_f = os.path.abspath(f)
            module = os.path.splitext(os.path.basename(f))[0]
            modules.append((package, module, f))
        return modules

    def build_module(self, module, module_file, package):
        if type(package) is str:
            package = package.split('.')
        elif type(package) not in (list, tuple):
            raise TypeError, \
                  "'package' must be a string (dot-separated), list, or tuple"
        # Now put the module source file into the "build" area.
        outfile = self.get_module_outfile(self.build_lib, package, module)
        if module_file.endswith(".qpy"): 
            outfile = outfile[0:outfile.rfind('.')] + ".qpy"
        dir = os.path.dirname(outfile)
        self.mkpath(dir)
        return self.copy_file(module_file, outfile, preserve_mode=0)


if __name__ == '__main__':
    from __init__ import __version__
    if 'sdist' in sys.argv:
        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())) == 0, __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
    setup(
        name="qpy",
        version=__version__,
        description="unicode/html content in functions",
        author="CNRI",
        author_email="webmaster@mems-exchange.org",
        url="http://www.mems-exchange.org/software/qpy/",
        license="see LICENSE.txt",
        package_dir=dict(qpy=os.curdir),
        packages=['qpy'],
        scripts=['qpcheck.py', 'qpyrun.py'],
        ext_modules=[Extension(name="qpy.c8", sources=["c8.c"])],
        cmdclass= {'build_py': qpy_build_py},
        long_description=(
        "Qpy provides a convenient mechanism for generating safely-quoted html "
        "text from python code.  It does this by implementing a quoted-string "
        "data type and a modification of the python compiler.  (This main idea "
        "comes from Quixote's htmltext/PTL.)")
        )
