[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gump-commits
Subject:    svn commit: r980314 - /gump/trunk/python/gump/core/model/builder.py
From:       bodewig () apache ! org
Date:       2010-07-29 5:42:54
Message-ID: 20100729054254.B7C5B238897D () eris ! apache ! org
[Download RAW message or body]

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 <ant/> 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 <configure/> element
 class Configure(Builder):
     """ A configure command (within a project)"""


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic