commit 10da8243f2f5b1f1326035da08c0bd0257983685
Author: Michal Srb <msrb(a)redhat.com>
Date: Mon May 19 16:55:23 2014 +0200
[pom] Merge with maven_dependency
These classes should be probably together
python/javapackages/maven_dependency.py | 181 -------------------------------
python/javapackages/pom.py | 154 ++++++++++++++++++++++++++-
2 files changed, 153 insertions(+), 182 deletions(-)
---
diff --git a/python/javapackages/pom.py b/python/javapackages/pom.py
index 06b7761..59bc7a6 100644
--- a/python/javapackages/pom.py
+++ b/python/javapackages/pom.py
@@ -1,5 +1,5 @@
#!/usr/bin/python
-# Copyright (c) 2013, Red Hat, Inc
+# Copyright (c) 2014, Red Hat, Inc
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@@ -29,6 +29,7 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Authors: Stanislav Ochotnicky <sochotnicky(a)redhat.com>
+# Michal Srb <msrb(a)redhat.com>
from lxml.etree import ElementTree, XMLParser
@@ -200,3 +201,154 @@ class POM(object):
continue
result.add(adep)
return result
+
+
+class MavenDependency(object):
+
+ def __init__(self, groupId, artifactId, version="",
extension="",
+ classifier="", exclusions=None):
+ self.groupId = groupId
+ self.artifactId = artifactId
+ self.version = version
+ self.extension = extension
+ self.classifier = classifier
+ self.exclusions = exclusions or set()
+
+ # TODO: these methods should be shared with with pom.py
+ # maybe create something like pom_commons.py,
+ # or move MavenDependency to pom.py?
+ @classmethod
+ def __findall(cls, xpath, xmlnode):
+ ret = xmlnode.xpath(xpath,
namespaces=dict(pom='http://maven.apache.org/POM/4.0.0'))
+ # perhaps there is no namespace?
+ if len(ret) == 0:
+ ret = xmlnode.xpath(xpath.replace('pom:',''))
+ return ret
+
+ @classmethod
+ def __find(cls, xpath, xmlnode):
+ ret = cls.__findall(xpath, xmlnode)
+ if len(ret) > 0:
+ ret = ret[0]
+ else:
+ ret = None
+
+ return ret
+
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+ def __hash__(self):
+ return self.groupId.__hash__() + \
+ self.artifactId.__hash__() + \
+ self.version.__hash__() + \
+ self.extension.__hash__() + \
+ self.classifier.__hash__()
+
+ def __unicode__(self):
+ return self.artifact.__unicode__()
+
+ def __str__(self):
+ return ":".join(self.groupId, self.artifactId, self.version,
+ self.extension, self.classifier)
+
+ @classmethod
+ def from_xml_element(cls, xmlnode, namespace=""):
+ """
+ Create Dependency from xml.etree.ElementTree.Element as contained
+ within pom.xml
+ """
+
+ parts = {'groupId': '',
+ 'artifactId': '',
+ 'extension': '',
+ 'classifier': '',
+ 'version': ''}
+
+ for key in parts:
+ #node = xmlnode.find("./{*}" + key,
namespaces=dict(pom=namespace))
+ node = MavenDependency.__find("./" + key, xmlnode)
+ if node is not None and node.text is not None:
+ parts[key] = node.text.strip()
+
+ # TODO: do we want to mix POM and Ivy files here?
+ # ivy specifies version as "rev" in case of dependencies:
+ if xmlnode.attrib:
+ rev = xmlnode.attrib.get("rev")
+ if rev:
+ parts['version'] = rev
+
+ exclusions = set()
+ exclxml = xmlnode.find('./{*}exclusions')
+ if exclxml is not None:
+ for excl in exclxml:
+ e = MavenExclusion.from_xml_element(excl)
+ exclusions.add(e)
+
+ return cls(parts['groupId'], parts['artifactId'],
+ version=parts['version'],
extension=parts['extension'],
+ classifier=parts['classifier'], exclusions=exclusions)
+
+ @classmethod
+ def merge_dependencies(cls, dominant, recessive):
+ """
+ Merge two dependencies into one. Information missing in dominant dependency will
+ be copied from recessive dependency. Returns new merged dependency.
+ """
+ ret = cls(dominant.groupId, dominant.artifactId, dominant.extension,
+ dominant.classifier, dominant.version, dominant.namespace)
+ for key in ("artifactId", "groupId", "extension",
"version",
+ "classifier", "namespace"):
+ if not getattr(ret, key):
+ setattr(ret, key, getattr(recessive, key))
+ return ret
+
+
+class MavenExclusion(object):
+ """ The <exclusion> element contains informations required to
exclude
+ an artifact to the project. """
+ def __init__(self, groupId, artifactId):
+ self.groupId = groupId.strip()
+ self.artifactId = artifactId.strip()
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+ def __hash__(self):
+ return self.groupId.__hash__() + \
+ self.artifactId.__hash__()
+
+ def __str__(self):
+ return unicode(self).encode('utf-8')
+
+ def __unicode__(self):
+ return ":".join([self.groupId, self.artifactId])
+
+ @classmethod
+ def from_xml_element(cls, xmlnode):
+ """
+ Create MavenExclusion from xml.etree.ElementTree.Element as contained
+ within pom.xml
+ """
+
+ parts = {'groupId': '',
+ 'artifactId': ''}
+
+ for key in parts:
+ node = xmlnode.find("./{*}" + key)
+ if node is not None and node.text is not None:
+ parts[key] = node.text.strip()
+
+ return cls(parts['groupId'], parts['artifactId'])
+