#!/usr/bin/python
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright (c) Carsten Grzemba
#

import pkg.lint.base as base
import subprocess as sp
import os
import re

class OpenCSWActionChecker(base.ActionChecker):
    """An OpenCSW-specific class to check actions."""

    name = "opencsw.action"

    def __init__(self, config):
            self.description = _(
                "checks OpenCSW packages for common action errors")
            super(OpenCSWActionChecker, self).__init__(config)

    def split_packages(self, manifest, engine, pkglint_id="001"):
       """Warn if there shared libries in standard path
       """
    split_packages.pkglint_desc = _("Package should split out subpackages for shared libraries")
        
class OpenCSWManifestChecker(base.ManifestChecker):
    """An opencsw.org-specific class to check manifests."""

    name = "opencsw.manifest"

    def __init__(self, config):
        self.description = _(
            "checks OpenCSW packages for common errors")
        super(OpenCSWManifestChecker, self).__init__(config)

    def recipe_uncommited(self, manifest, engine, pkglint_id="001"):
        """Warn when a build recipe is not commited
        """
        p = sp.Popen("cd ../../.. && svn st --ignore-externals", shell=True, stdout=sp.PIPE,stderr=sp.PIPE)
        p.wait()
        err = p.stderr.read()
        if len(err) > 0:
            engine.error(
                _("SVN error for {pkg} recipe on path {path}\n{msg}").format(pkg=manifest.fmri, path=os.path.abspath(''), msg=err),
                msgid="{0}{1}.1".format(self.name, pkglint_id))
        else:
            if p.stdout.read():
                engine.warning(
                    _("Build recipe should commited for publish {pkg}").format(pkg=manifest.fmri),
                    msgid="{0}{1}.1".format(self.name, pkglint_id))
    recipe_uncommited.pkglint_desc = _("Build recipe should commited to sourceforge")
    

    def packageprefixname_notset(self, manifest, engine, pkglint_id="002"):
        """ set name=pkg.fmri value=diagnotic/nrpe@2.15

        """
       
        fmri_name = manifest.fmri.get_name() 
        if fmri_name.find('/',1) < 0 or re.match("opencsw/.*", fmri_name):
            engine.error(_("IPS_PACKAGE_PREFIX must set in build recipe and must end with a '/', missing prefix in {fmri}").format(fmri=manifest.fmri),
						msgid="%s%s.2" % (self.name, pkglint_id))

    packageprefixname_notset.pkglint_desc = _( "build recipe must set package name prefix for package FMRI" )
