# jhbuild - a build script for GNOME 1.x and 2.x
# Copyright (C) 2001-2004  James Henstridge
# Copyright (C) 2003-2004  Marco Pesenti Gritti
# Copyright (C) 2005  Luis Villa
#
#   cvssetuppymodule.py: rules for building python-y setup.py modules from cvs
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import os

import base
from jhbuild.utils import cvs
from jhbuild.errors import FatalError

class CVSSetupPyModule(base.CVSModule):
    def __init__(self, name, projects, revision, autogenargs='',
		 makeargs='', dependencies=[], suggests=[], cvsroot=None):
        base.CVSModule.__init__(self, name,
				revision=revision,
                                autogenargs=autogenargs,
                                makeargs=makeargs,
                                dependencies=dependencies,
                                suggests=suggests,
                                cvsroot=cvsroot,
                                supports_non_srcdir_builds=False)

    def do_start(self, buildscript):
        builddir = self.get_builddir(buildscript)
        if not buildscript.config.nonetwork: # normal start state
            return (self.STATE_CHECKOUT, None, None)
        elif buildscript.config.nobuild:
            return (self.STATE_DONE, None, None)
        elif buildscript.config.makeclean:
            return (self.STATE_CLEAN, None, None)
        else:
            return (self.STATE_BUILD, None, None)

    def do_configure(self, buildscript):
	return (self.STATE_BUILD, None, None) 

    def do_clean(self, buildscript):
        os.chdir(self.get_builddir(buildscript))
        buildscript.set_action('Cleaning', self)
        cmd = 'python setup.py clean'
        if buildscript.execute(cmd) == 0:
            return (self.STATE_BUILD, None, None)
        else:
            return (self.STATE_BUILD, 'could not clean module',
                    [self.STATE_FORCE_CHECKOUT, self.STATE_CONFIGURE])

    def do_build(self, buildscript):
        os.chdir(self.get_builddir(buildscript))
        buildscript.set_action('Building', self)
        cmd = 'python setup.py build'
        if buildscript.config.makecheck:
            nextstate = self.STATE_CHECK
        else:
            nextstate = self.STATE_INSTALL
        if buildscript.execute(cmd) == 0:
            return (nextstate, None, None)
        else:
            return (nextstate, 'could not build module',
                    [self.STATE_FORCE_CHECKOUT, self.STATE_CONFIGURE])

    def do_check(self, buildscript):
        return (self.STATE_INSTALL, None, None)

    def do_install(self, buildscript):
        os.chdir(self.get_builddir(buildscript))
        buildscript.set_action('Installing', self)
        cmd = 'python setup.py install --prefix=%s' % buildscript.config.prefix
        error = None
        if buildscript.execute(cmd) != 0:
            error = 'could not make module'
        else:
            buildscript.packagedb.add(self.name, self.get_revision() or '')
    	    return (self.STATE_DONE, error, [])

def parse_cvssetuppymodule(node, config, dependencies, suggests, root):
	if root[0] != 'cvs':
        	raise FatalError('%s is not a CVS root' % root[1])
    	cvsroot = root[1]
    	id = node.getAttribute('id')
    	module = id
    	revision = None
    	checkoutdir = None
    	autogenargs = ''
    	makeargs = ''
    	supports_non_srcdir_builds = True
    	if node.hasAttribute('module'):
    	    	module = node.getAttribute('module')
    	if node.hasAttribute('revision'):
    	    	revision = node.getAttribute('revision')
    	if node.hasAttribute('checkoutdir'):
    	    	checkoutdir = node.getAttribute('checkoutdir')
    	if node.hasAttribute('autogenargs'):
		autogenargs = node.getAttribute('autogenargs')
    	if node.hasAttribute('makeargs'):
	    	makeargs = node.getAttribute('makeargs')
    	if node.hasAttribute('supports-non-srcdir-builds'):
        	supports_non_srcdir_builds = \
            (node.getAttribute('supports-non-srcdir-builds') != 'no')

	# override revision tag if requested.
    	revision = config.branches.get(module, revision)
    	autogenargs += ' ' + config.module_autogenargs.get(module,
                                                       config.autogenargs)
    	makeargs += ' ' + config.module_makeargs.get(module, config.makeargs)

    	return CVSSetupPyModule(module, checkoutdir, revision,
                     autogenargs, makeargs,
                     cvsroot=cvsroot,
                     dependencies=dependencies,
                     suggests=suggests)

base.register_module_type('cvssetuppymodule', parse_cvssetuppymodule)


