#!/usr/bin/env python
# -*- coding: utf-8 mode: python -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8

NAME = 'pyulib'
VERSION = None
DESCRIPTION = 'Librairies python de utools'
AUTHOR = 'Jephte CLAIN'
EMAIL = 'Jephte.Clain@univ-reunion.fr'
MODULES = ('i_need_py23', 'i_need_py24', 'i_need_py25',
           'i_need_py26', 'i_need_py27',
           'ULIB_CONFIG',
           )
SRCDIR = 'src'
PACKAGE_DIR = {'': SRCDIR}
PACKAGE_DATA = {}
PACKAGES = []
SCRIPTS = []

import os, sys, re, fnmatch
from os import path

RE_VERSION = re.compile(r'(\d+(?:\.\d+)*)(?:-r(\d+/\d+/\d+))?')
def get_version(basedir=None):
    if basedir is None:
        basedir = path.split(path.abspath(sys.argv[0]))[0]
    version_txt = path.join(basedir, 'VERSION.txt')
    if not path.isfile(version_txt): return ''
    try:
        inf = open(version_txt, 'rb')
        try: line = inf.readline()
        finally: inf.close()
    except:
        return ''
    mo = RE_VERSION.match(line)
    if not mo: return ''
    return mo.group(1)

def findf(spec, bp):
    """Transformer le package bp en chemin, puis chercher récursivement les
    fichiers correspondant à la spécification spec à partir de SRCDIR/bp
    """
    files = []
    bp = bp.replace(".", "/")
    bpdir = path.join(SRCDIR, bp)
    bpnames = os.listdir(bpdir)
    for specname in fnmatch.filter(bpnames, spec):
        specfile = path.join(bpdir, specname)
        if path.isfile(specfile):
            files.append(specname)
        else:
            for dirpath, dirnames, filenames in os.walk(specfile):
                dirnames.remove('.svn')
                dirpath = dirpath[len(bpdir)+1:]
                files.extend([path.join(dirpath, filename) for filename in filenames])
    return files
    
def fixp(p, bp):
    """Transformer le package bp en chemin, puis exprimer le chemin relatif p
    par rapport au chemin du package, puis ajouter SRCDIR/ devant le chemin
    """
    bp = bp.replace(".", "/")
    return path.join(SRCDIR, bp, p)
def addp(name, data=()):
    """Ajouter un package, avec ses fichiers de données
    """
    global PACKAGES, PACKAGE_DATA
    PACKAGES.append(name)
    if data:
        files = []
        for spec in data:
            files.extend(findf(spec, name))
        PACKAGE_DATA[name] = files
def adds(name, scripts=()):
    """Ajouter des scripts contenus dans un package
    """
    global SCRIPTS
    if scripts:
        SCRIPTS.extend(map(lambda s: fixp(s, name), scripts))

if VERSION is None: VERSION = get_version()
addp('ulib')
addp('ulib.all')
addp('ulib.base')
addp('ulib.ext')
addp('ulib.ext.optik141', ['README.txt'])
addp('ulib.ext.simplejson', ['_speedups.c'])
addp('ulib.ext.tarfile', ['README.txt'])
addp('ulib.ext.web')
addp('ulib.ext.web.wsgiserver', ['LICENSE.txt'])
addp('ulib.ext.web.contrib')
addp('ulib.ext.xpath')
addp('ulib.formats')
addp('ulib.gae')
addp('ulib.json')
addp('ulib.optparse')
addp('ulib.sa')
addp('ulib.tasks')
addp('ulib.templ')
addp('ulib.web')
addp('ulib.p')
addp('ulib.p.templ')
addp('ulib.p.uinc')
addp('ulib.p.vcs')
addp('ulib.p.wop')

if __name__ == '__main__':
    from distutils.core import setup
    setup(name=NAME, version=VERSION,
          description=DESCRIPTION, author=AUTHOR, author_email=EMAIL,
          py_modules=MODULES,
          package_dir=PACKAGE_DIR, package_data=PACKAGE_DATA, packages=PACKAGES,
          scripts=SCRIPTS,
          )