#!/usr/bin/env python

# Copyright (C) 2009, Mathieu PASQUET <mpa@makina-corpus.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. Neither the name of the <ORGANIZATION> nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

__docformat__ = 'restructuredtext en'

import os
import re

re_flags = re.M | re.U | re.I | re.S

from zope.interface import implements
from collective.generic.webbuilder import interfaces
from iniparse import INIConfig

from collective.generic.webbuilder.utils import remove_path


class DummyPlugin(object):
    implements(interfaces.IPostGenerationPlugin)

    def __init__(self, paster):
        self.paster = paster

    def process(self, output_dir):
        print "I am a dummy plugin doing nothing inside %s for %s" % (
            output_dir,
            self.paster.configuration.name
        )


def remove_egg_info(p):
    for dirpath, dirnames, filenames in os.walk(p):
        for filename in dirnames + filenames:
            if 'egg-info' in filename:
                remove_path(
                    os.path.join(dirpath, filename)
                )
        for directory in dirnames:
            subdir = os.path.join(dirpath, directory)
            remove_egg_info(subdir)


class EggInfoPlugin(DummyPlugin):

    def process(self, output_dir, project_name, params):
        remove_egg_info(output_dir)


class EggPlugin(DummyPlugin):

    def process(self, output_dir, project_name, params):
        """."""
        eggsnames = []
        devnames = []
        src_dir = os.path.join(output_dir, 'src')
        for path in os.listdir(src_dir):
            p = os.path.join(src_dir, path)
            if os.path.isdir(p):
                if os.path.exists(os.path.join(p, 'setup.py')):
                    eggsnames.append(path)
                    devnames.append(os.path.join('src', path))
        zcmlnames = [n
                     for n in eggsnames
                     if (
                         ('policy' in n)
                         or (n == project_name)
                         or (n == '%s.core' % project_name)
                     )]

        f = os.path.join(output_dir, 'buildout.cfg')
        sf = os.path.join(
            output_dir, 'etc', 'project', 'sources.cfg')
        pf = os.path.join(
            output_dir, 'etc', 'project', '%s.cfg' % project_name)
        if os.path.exists(pf):
            f = pf
        else:
            for i in ['django', 'plone', 'pyramid']:
                pf = os.path.join(
                    output_dir, 'etc', 'project', '%s.cfg' % i)
                if os.path.exists(pf):
                    f = pf
        cfg = INIConfig(open(f))
        srccfg = None
        if os.path.exists(sf):
            srccfg = INIConfig(open(sf))
        else:
            srccfg = cfg

        extdevoption_re = re.compile('develop\s*\+\s*', re_flags)
        devoption_re = re.compile('develop\s*', re_flags)
        autocheckout_option_re = re.compile(
            'auto-checkout\s*', re_flags)
        ext_autocheckout_option_re = re.compile(
            'auto-checkout\s*\+\s*', re_flags)
        exteggsoption_re = re.compile('eggs\s*\+\s*', re_flags)