Return-Path: Delivered-To: apmail-gump-commits-archive@www.apache.org Received: (qmail 70589 invoked from network); 29 Jul 2010 05:44:15 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 29 Jul 2010 05:44:15 -0000 Received: (qmail 61471 invoked by uid 500); 29 Jul 2010 05:44:14 -0000 Mailing-List: contact commits-help@gump.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: general@gump.apache.org Delivered-To: mailing list commits@gump.apache.org Received: (qmail 61464 invoked by uid 99); 29 Jul 2010 05:44:13 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 29 Jul 2010 05:44:13 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 29 Jul 2010 05:44:12 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id B7C5B238897D; Thu, 29 Jul 2010 05:42:54 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r980314 - /gump/trunk/python/gump/core/model/builder.py Date: Thu, 29 Jul 2010 05:42:54 -0000 To: commits@gump.apache.org From: bodewig@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100729054254.B7C5B238897D@eris.apache.org> Author: bodewig Date: Thu Jul 29 05:42:54 2010 New Revision: 980314 URL: http://svn.apache.org/viewvc?rev=980314&view=rev Log: try to parse version from POM Modified: gump/trunk/python/gump/core/model/builder.py Modified: gump/trunk/python/gump/core/model/builder.py URL: http://svn.apache.org/viewvc/gump/trunk/python/gump/core/model/builder.py?rev=980314&r1=980313&r2=980314&view=diff ============================================================================== --- gump/trunk/python/gump/core/model/builder.py (original) +++ gump/trunk/python/gump/core/model/builder.py Thu Jul 29 05:42:54 2010 @@ -23,11 +23,13 @@ import os import sys +import xml.dom.minidom from xml.dom import getDOMImplementation from gump.util import getIndent from gump.util.domutils import domAttributeIsTrue, getDomAttributeValue, \ - hasDomAttribute, hasDomChild, transferDomAttributes + getDomChild, getDomTextValue, hasDomAttribute, hasDomChild, \ + transferDomAttributes from gump.core.model.depend import INHERIT_NONE, ProjectDependency from gump.core.model.object import ModelObject @@ -198,14 +200,7 @@ class Builder(ModelObject, PropertyConta # Complete them all self.completeProperties(workspace) - # Set this up... - if self.hasDomAttribute('basedir'): - self.basedir = os.path.abspath(os.path.join( - self.project.getModule().getWorkingDirectory() or dir.base, - self.getDomAttributeValue('basedir') - )) - else: - self.basedir = self.project.getBaseDirectory() + self.resolve_basedir() # Check for debugging properties self.setDebug(self.domAttributeIsTrue('debug')) @@ -224,6 +219,19 @@ class Builder(ModelObject, PropertyConta def getBaseDirectory(self): return self.basedir + def resolve_basedir(self): + """ Sets basedir based on the basedir attribute or the + project's configuration """ + if not self.basedir: + if self.hasDomAttribute('basedir'): + self.basedir = os.path.abspath(os.path.join( + self.project.getModule().getWorkingDirectory() + or dir.base, + self.getDomAttributeValue('basedir') + )) + else: + self.basedir = self.project.getBaseDirectory() + # represents an element class BaseAnt(Builder): """ An Ant command (within a project)""" @@ -331,29 +339,70 @@ class Maven2(Builder): class Mvn2Install(Maven2): """ Installs a single file into the local mvn repository """ + ARTIFACT_ID = 'artifactId' + FILE = 'file' + GOAL = 'install:install-file' + PACKAGING = 'packaging' + PARENT = 'parent' + POM = 'pom' + VERSION = 'version' + def __init__(self, dom, project): Maven2.__init__(self, dom, project) - self.goal = 'install:install-file' - self.packaging = self.getDomAttributeValue('packaging', 'pom') - self.file = self.getDomAttributeValue('file', 'pom.xml') - self.version = self.getDomAttributeValue('version') - self.artifactId = self.getDomAttributeValue('artifactId') + self.goal = Mvn2Install.GOAL + self.packaging = self.getDomAttributeValue(Mvn2Install.PACKAGING, + Mvn2Install.POM) + self.file = self.getDomAttributeValue(Mvn2Install.FILE, 'pom.xml') + self.version = self.getDomAttributeValue(Mvn2Install.VERSION) + self.artifactId = self.getDomAttributeValue(Mvn2Install.ARTIFACT_ID) def expand(self, project, workspace): """ Turns the builder's attributes into properties """ Builder.expand(self, project, workspace) impl = getDOMImplementation() - if (self.artifactId): - self.add_property(impl, 'artifactId', self.artifactId) + self._add_property(impl, Mvn2Install.ARTIFACT_ID, + self.artifactId or project.getName()) + self._add_property(impl, 'groupId', project.getArtifactGroup()) + self._add_property(impl, Mvn2Install.PACKAGING, self.packaging) + self._add_property(impl, Mvn2Install.FILE, self.file) + + def complete(self, project, workspace): + """ + Complete the model from XML - potentially parse POM for version + """ + impl = getDOMImplementation() + if self.version: + self._add_property(impl, Mvn2Install.VERSION, self.version) + elif self.packaging == Mvn2Install.POM: + try: + self.resolve_basedir() + pomDoc = self._read_pom() + root = pomDoc.documentElement + if not root.tagName == 'project': + project.addError('file is not a POM, its root element is ' + + root.tagName) + return + + version = _extract_version_from_pom(root) + + if not version: + project.addError("POM doesn't specify a version, you must" + + " provide the version attribute") + return + version_text = getDomTextValue(version) + self._add_property(impl, Mvn2Install.VERSION, + version_text) + except Exception, details: + project.addError('failed to parse POM because of ' + + str(details)) else: - self.add_property(impl, 'artifactId', project.getName()) - self.add_property(impl, 'groupId', project.getArtifactGroup()) - self.add_property(impl, 'packaging', self.packaging) - self.add_property(impl, 'file', self.file) - self.add_property(impl, 'version', self.version) + project.addError("version attribute is mandatory if the file is" + + " not a POM.") - def add_property(self, impl, name, value): + Builder.complete(self, project, workspace) + + def _add_property(self, impl, name, value): """ Adds a named property """ doc = impl.createDocument(None, 'property', None) prop = doc.documentElement @@ -361,6 +410,22 @@ class Mvn2Install(Maven2): prop.setAttribute('value', value) self.importProperty(prop) + def _read_pom(self): + """ locates the POM, parses it and returns it as DOM Document """ + pom = os.path.join(self.getBaseDirectory(), self.file) + return xml.dom.minidom.parse(pom) + +def _extract_version_from_pom(root): + """ Tries to extract the version DOM element from a POM DOM tree """ + version = None + if hasDomChild(root, Mvn2Install.VERSION): + version = getDomChild(root, Mvn2Install.VERSION) + elif hasDomChild(root, Mvn2Install.PARENT): + parent = getDomChild(root, Mvn2Install.PARENT) + if hasDomChild(parent, Mvn2Install.VERSION): + version = getDomChild(parent, Mvn2Install.VERSION) + return version + # represents an element class Configure(Builder): """ A configure command (within a project)"""