diff options
author | Alistair Bush <ali_bush@gentoo.org> | 2007-12-31 06:48:00 +0000 |
---|---|---|
committer | Alistair Bush <ali_bush@gentoo.org> | 2007-12-31 06:48:00 +0000 |
commit | 68b04346aa56009c9dd19f5eca389ed8a3be2b48 (patch) | |
tree | 0ebe31b595e6bbe1bc17e26f127258999713cdb6 | |
parent | Branch trunk to allow project layout to be changed. (diff) | |
download | javatoolkit-68b04346aa56009c9dd19f5eca389ed8a3be2b48.tar.gz javatoolkit-68b04346aa56009c9dd19f5eca389ed8a3be2b48.tar.bz2 javatoolkit-68b04346aa56009c9dd19f5eca389ed8a3be2b48.zip |
Refactored code of active sub-projects into new file structure. Currently missing build mechanism.
svn path=/projects/javatoolkit/branches/layout_refactor_branch/; revision=5876
-rw-r--r-- | src/py/javatoolkit/__init__.py | 14 | ||||
-rw-r--r-- | src/py/javatoolkit/classpath.py | 72 | ||||
-rw-r--r-- | src/py/javatoolkit/maven/MavenPom.py | 210 | ||||
-rw-r--r-- | src/py/javatoolkit/maven/__init__.py | 7 | ||||
-rw-r--r-- | src/py/javatoolkit/output.py | 28 | ||||
-rw-r--r-- | src/py/javatoolkit/xml/DomRewriter.py | 121 | ||||
-rw-r--r-- | src/py/javatoolkit/xml/SaxRewriter.py | 132 | ||||
-rw-r--r-- | src/py/javatoolkit/xml/__init__.py | 7 | ||||
-rwxr-xr-x | src/py/maven-helper.py | 153 | ||||
-rwxr-xr-x | src/py/xml-rewrite-3.py | 250 |
10 files changed, 994 insertions, 0 deletions
diff --git a/src/py/javatoolkit/__init__.py b/src/py/javatoolkit/__init__.py new file mode 100644 index 0000000..89a74a8 --- /dev/null +++ b/src/py/javatoolkit/__init__.py @@ -0,0 +1,14 @@ +#! /usr/bin/python2 +# +# Copyright(c) 2004, Karl Trygve Kalleberg <karltk@gentoo.org> +# Copyright(c) 2004, Gentoo Foundation +# +# Licensed under the GNU General Public License, v2 +# +# $Header: /var/cvsroot/gentoo-src/javatoolkit/src/javatoolkit/__init__.py,v 1.2 2004/11/08 19:21:52 karltk Exp $ + +from classpath import * +from output import * + +if __name__ == "__main__": + print "This is not an executable module" diff --git a/src/py/javatoolkit/classpath.py b/src/py/javatoolkit/classpath.py new file mode 100644 index 0000000..c570774 --- /dev/null +++ b/src/py/javatoolkit/classpath.py @@ -0,0 +1,72 @@ +#! /usr/bin/python2 +# +# Copyright(c) 2004, Karl Trygve Kalleberg <karltk@gentoo.org> +# Copyright(c) 2004, Gentoo Foundation +# +# Licensed under the GNU General Public License, v2 +# +# $Header: /var/cvsroot/gentoo-src/javatoolkit/src/javatoolkit/classpath.py,v 1.4 2004/11/08 20:06:06 karltk Exp $ + +class ClasspathIter: + """An iterator for the Classpath class, below.""" + + def __init__(self, classpath): + self._classpath = classpath + self._index = 0 + + def next(self): + self._index += 1 + if self._index >= len(self._classpath.classpath): + raise StopIteration + return self._classpath.classpath[self._index] + + +class Classpath: + """A classpath object provides a collection interface to the elements of a : separated path list. """ + + def __init__(self, classpath_string = None): + if classpath_string != None: + cs = classpath_string.strip().strip("\"") + self.classpath = cs.split(":") + else: + self.classpath = [] + + + def __iter__(self): + """Returns iterator. Elements of the original classpath string are considered split by ':'.""" + + return ClasspathIter(self) + + + def __len__(self): + """Returns length (number of elements) in this classpath.""" + + return len(self.classpath) + + + def __getitem__(self, i): + """Returns i'th element.""" + + return self.classpath[i] + + + def __setitem__(self, i, val): + """Sets i'th element.""" + + self.classpath[i] = val + + + def __str__(self): + """Constructs a suitable string representation of the classpath.""" + + return ":".join(self.classpath) + + + def append(self, element): + """Appends an path to the classpath.""" + + self.classpath.append(element) + + +if __name__ == "__main__": + print "This is not an exectuable module"
\ No newline at end of file diff --git a/src/py/javatoolkit/maven/MavenPom.py b/src/py/javatoolkit/maven/MavenPom.py new file mode 100644 index 0000000..8ad8cca --- /dev/null +++ b/src/py/javatoolkit/maven/MavenPom.py @@ -0,0 +1,210 @@ +# Copyright 2004-2007 Gentoo Foundation +# Distrubuted under the terms of the GNU General Public Licence v2 + +# Authors: +# koirky <kiorky@cryptelium.net> The code: +# ali_bush <ali_bush@gentoo.org> Refactored into module. +# Python based POM navigator + +# Changelog +# ali_bush <ali_bush@gentoo.org> +# 31/12/07 Refacted by separating MavenPom into namespace +# +# kiorky <kiorky@cryptelium.net> +# 31/05/2007 Add rewrite feature +# +# kiorky <kiorky@cryptelium.net> +# 08/05/2007 initial version + +import sys +import StringIO + +# either a very simplified representation of a maven pom +# or a fully xml rewritten pom +class MavenPom: + def __init__(self,cli_options = None): + self.group = '' + self.artifact = '' + self.version = '' + self.name = '' + self.is_child = "false" + self.dependencies = [] + self.buffer = StringIO.StringIO() + self.__write = self.buffer.write + self.mydoc = None + self.cli_options = cli_options + + + def getInfos(self,node): + for child_node in node.childNodes: + if child_node.nodeType == child_node.ELEMENT_NODE: + if child_node.childNodes: + if child_node.childNodes[0].nodeValue != "": + if child_node.nodeName == "version": + self.version = child_node.childNodes[0].nodeValue + + if child_node.nodeName == "artifactId": + self.artifact = child_node.childNodes[0].nodeValue + + if child_node.nodeName == "groupId": + self.group = child_node.childNodes[0].nodeValue + + if child_node.nodeName == "name": + self.name = child_node.childNodes[0].nodeValue + + + def getDescription(self,mydoc,**kwargs): + if mydoc: + self.project = mydoc.getElementsByTagName("project")[0] + # get inherited properties from parent pom if any + if self.group == "" or self.version == "" or self.artifact == "": + for node in self.project.childNodes: + if node.nodeName == "parent": + self.is_child = "true" + self.getInfos(node) + + self.getInfos(self.project) + + # get our deps + for node in self.project.childNodes: + if node.nodeName == "dependencies": + for dependency_node in node.childNodes: + if dependency_node.nodeName == "dependency": + dep = MavenPom() + for child_node in dependency_node.childNodes: + if child_node.nodeType == child_node.ELEMENT_NODE: + dep.getInfos(child_node) + + self.dependencies.append(dep) + + if self.cli_options.p_group: + self.__write("pom group:%s\n" % self.group ) + + if self.cli_options.p_ischild: + self.__write("pom ischild:%s\n" % self.is_child ) + + if self.cli_options.p_artifact: + self.__write("pom artifact:%s\n" % self.artifact ) + + if self.cli_options.p_version: + self.__write("pom version:%s\n" % self.version ) + + if self.cli_options.p_dep: + i=0 + for dependency in self.dependencies: + i=i+1 + self.__write("%d:dep_group:%s\n" % (i,dependency.group) ) + self.__write("%d:dep_artifact:%s\n" % (i,dependency.artifact) ) + self.__write("%d:dep_version:%s\n" % (i,dependency.version) ) + + + def read(self): + return self.buffer.getvalue() + + + def rewrite(self,xmldoc,**kwargs): + # desactivate all dependencies + dependencies_root = ( xmldoc.getElementsByTagName("dependencies") or [] ) + for node in dependencies_root: + copylist_child_Nodes =list(node.childNodes) + for child_node in copylist_child_Nodes: + node.removeChild(child_node) + child_node.unlink() + + # add our classpath using system scope + if self.cli_options.classpath: + i=0 + dependencies_root = ( xmldoc.getElementsByTagName("dependencies") or [] ) + if dependencies_root: + for node in dependencies_root: + for classpath_element in self.cli_options.classpath[0].split(':'): + if classpath_element: + dependency_elem = xmldoc.createElement("dependency") + dependency_elem.appendChild( self.create_element(xmldoc, "groupId", "sexy")) + dependency_elem.appendChild( self.create_element(xmldoc, "artifactId", "gentoo%d" % (i))) + dependency_elem.appendChild( self.create_element(xmldoc, "version", "666")) + dependency_elem.appendChild( self.create_element(xmldoc, "scope", "system")) + dependency_elem.appendChild( self.create_element(xmldoc, "systemPath", classpath_element)) + node.appendChild(dependency_elem) + i += 1 + + # overwrite source/target options if any + # remove version node for all plugins + if self.cli_options.p_source or self.cli_options.p_target: + dependencies_root = ( xmldoc.getElementsByTagName("plugin") or [] ) + # remove part + if len(dependencies_root) > 0: + for node in dependencies_root: + for child_node in node.childNodes: + if child_node.nodeName == "version": + node.removeChild(child_node) + child_node.unlink() + + if child_node.nodeName == "artifactId": + if "maven-compiler-plugin" == child_node.childNodes[0].data: + node.parentNode.removeChild(node) + node.unlink() + + # creation/overwrite part + plugin_node = self.create_element(xmldoc,"plugin") + group_node = self.create_element(xmldoc,"groupId","org.apache.maven.plugins") + artifact_node = self.create_element(xmldoc,"artifactId","maven-compiler-plugin") + configuration_node = self.create_element(xmldoc,"configuration") + plugin_node.appendChild(group_node) + plugin_node.appendChild(artifact_node) + plugin_node.appendChild(configuration_node) + if self.cli_options.p_target: + target_node = self.create_element(xmldoc,"target",self.cli_options.p_target[0]) + configuration_node.appendChild(target_node) + + if self.cli_options.p_source: + source_node = self.create_element(xmldoc,"source",self.cli_options.p_source[0]) + configuration_node.appendChild(source_node) + + plugins_nodes = ( xmldoc.getElementsByTagName("plugins") or [] ) + # no plugins node + if len(plugins_nodes) < 1 : + plugins_node = self.create_element(xmldoc,"plugins") + plugins_nodes.append(plugins_node) + + for plugins_node in plugins_nodes: + # add our generated plugin node + plugins_node.appendChild(plugin_node) + + # no build node + build_nodes = ( xmldoc.getElementsByTagName("build") or [] ) + if len(build_nodes) < 1 : + build_node = self.create_element(xmldoc,"build") + build_nodes.append(build_node) + # add build node to project_node + project_nodes = ( xmldoc.getElementsByTagName("project") or [] ) + for project_node in project_nodes: + project_node.appendChild(build_node) + + # add plugins structure to the build node + for build_node in build_nodes: + build_node.appendChild(plugins_node.cloneNode(deep=True)) + + from xml.dom.ext import PrettyPrint + self.write = self.__write + PrettyPrint(xmldoc,self) + self.write = None + + + def create_element(self,xmldoc,element_name,text_value=None): + element = None + if element_name: + element = xmldoc.createElement(element_name) + if text_value: + text_node = xmldoc.createTextNode(text_value) + element.appendChild(text_node) + + return element + + + def parse(self,in_stream,callback=None,**kwargs): + from xml.dom.minidom import parseString + self.mydoc = parseString(in_stream) + + if callback: + callback(self.mydoc,**kwargs) diff --git a/src/py/javatoolkit/maven/__init__.py b/src/py/javatoolkit/maven/__init__.py new file mode 100644 index 0000000..c339ef6 --- /dev/null +++ b/src/py/javatoolkit/maven/__init__.py @@ -0,0 +1,7 @@ +''' +javatoolkit.maven module +''' + +__version__ = '1.1' + +#set expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap diff --git a/src/py/javatoolkit/output.py b/src/py/javatoolkit/output.py new file mode 100644 index 0000000..05192ef --- /dev/null +++ b/src/py/javatoolkit/output.py @@ -0,0 +1,28 @@ +#! /usr/bin/python2 +# +# Copyright(c) 2004, Karl Trygve Kalleberg <karltk@gentoo.org> +# Copyright(c) 2004, Gentoo Foundation +# +# Licensed under the GNU General Public License, v2 +# +# $Header: /var/cvsroot/gentoo-src/javatoolkit/src/javatoolkit/output.py,v 1.1 2004/11/08 19:21:52 karltk Exp $ + +import sys + +# FIXME: Use gentoolkit stuff instead + +def eerror(s): + sys.stderr.write("!!! " + s + "\n") + +def ewarn(s): + sys.stdout.write("* " + s + "\n") + +def einfo(s): + sys.stdout.write("* " + s + "\n") + +def die(err, s): + eerror(s) + sys.exit(err) + +if __name__ == "__main__": + print "This is not an executable module"
\ No newline at end of file diff --git a/src/py/javatoolkit/xml/DomRewriter.py b/src/py/javatoolkit/xml/DomRewriter.py new file mode 100644 index 0000000..f5daf9f --- /dev/null +++ b/src/py/javatoolkit/xml/DomRewriter.py @@ -0,0 +1,121 @@ +# -*- coding: UTF-8 -*- + +# Copyright 2004-2008 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Header: $ + +from xml.dom import NotFoundErr +#import os +#import sys +#import StringIO +#import xml.sax.saxutils import quoteattr,escape + +class DomRewriter: + """ + The old DOM rewriter is still around for index based stuff. It can + be used for all the complex stuff but portage needed features should + be in StreamRewriterBase subclasses as they are much faster. + """ + from xml.dom import NotFoundErr + def __init__(self, modifyElems = None, attributes = None , values=None, index=None): + self.modifyElems = modifyElems + self.attributes = attributes + self.values = values + self.index = index + + + def delete_elements(self, document, **kwargs): + if not self.modifyElems: + return + + tomodify = [] + for tag in self.modifyElems: + matches = document.getElementsByTagName(tag) + if matches: + if self.index == None: + for match in matches: + tomodify.append(match) + else: + tomodify.append(matches[self.index]) + + for elem in tomodify: + for i,attr in enumerate(self.attributes): + if self.values: + elem.setAttribute(attr, self.values[i]) + else: + try: + elem.removeAttribute(attr) + except DomRewriter.NotFoundErr: + continue + + + def add_gentoo_classpath(self,document,**kwargs): + newcp = kwargs.has_key('classpath') and kwargs['classpath'] or "void" + newcp = newcp.split(":") + gcp = document.createElement("path") + for cp in newcp: + pe = document.createElement("pathelement") + pe.setAttribute("path",cp) + gcp.appendChild(pe) + + + # classpath nodes: + # if no refud: + # remove inner elems + # add our gentoo classpath node + # else + # rename refid references + matches = document.getElementsByTagName("classpath") + handled_refs = set() + for match in matches: + if not match.hasAttribute("refid"): + for node in match.childNodes[:]: + match.removeChild(node) + node.unlink() + + match.appendChild(gcp.cloneNode(True)) + else: + refid = match.getAttribute("refid") + for ref in document.getElementsByTagName("path"): + id = ref.getAttribute("id") + if id not in handled_refs and id == refid: + for node in ref.childNodes[:]: + ref.removeChild(node) + node.unlink() + + for pathnode in (gcp.cloneNode(deep=True)).childNodes: + ref.appendChild(pathnode.cloneNode(deep=True)) + + handled_refs.add(id) + + # rewrite javac elements + matches = document.getElementsByTagName("javac") + for match in matches: + classpath = match.getAttribute("classpath") + if classpath: + match.removeAttribute("classpath") + + for node in match.childNodes[:]: + if node.nodeName == "classpath": + match.removeChild(node) + node.unlink() + + classpath = document.createElement("classpath") + classpath.appendChild(gcp.cloneNode(True)) + match.appendChild(classpath) + + + def process(self,in_stream,callback=None,*args,**kwargs): + from xml.dom import minidom + self.document = minidom.parseString(in_stream); + + if callback: + callback(self.document,*args,**kwargs) + + + def write(self,stream): + from xml.dom.ext import PrettyPrint + PrettyPrint(self.document,stream) + + +#set expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap diff --git a/src/py/javatoolkit/xml/SaxRewriter.py b/src/py/javatoolkit/xml/SaxRewriter.py new file mode 100644 index 0000000..bb25e45 --- /dev/null +++ b/src/py/javatoolkit/xml/SaxRewriter.py @@ -0,0 +1,132 @@ +# -*- coding: UTF-8 -*- + +# Copyright 2004-2005 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Header: $ + +#import os +#import sys +#import StringIO + +from xml.sax.saxutils import XMLGenerator + +class SaxRewriter(XMLGenerator): + """ + Using Sax gives us the support for writing back doctypes and all easily + and is only marginally slower than expat as it is just a tight layer over it + """ + def __init__(self, **kwds): + self.elems = kwds.has_key('elems') and kwds['elems'] or [] + self.attributes = kwds.has_key('attributes') and kwds['attributes'] or [] + self.values = kwds.has_key('values') and kwds['values'] or [] + self.sourceElems = kwds.has_key('sourceElems') and kwds['sourceElems'] or [] + self.sourceAttributes = kwds.has_key('sourceAttributes') and kwds['sourceAttributes'] or [] + self.sourceValues = kwds.has_key('sourceValues') and kwds['sourceValues'] or [] + self.targetElems = kwds.has_key('targetElems') and kwds['targetElems'] or [] + self.targetAttributes = kwds.has_key('targetAttributes') and kwds['targetAttributes'] or [] + self.targetValues = kwds.has_key('targetValues') and kwds['targetValues'] or [] + + self.deleteElems = kwds.has_key('deleteElems') and kwds['deleteElems'] or [] + self.deleteAttributes = kwds.has_key('deleteAttributes') and kwds['deleteAttributes'] or [] + + self.src_dirs = kwds.has_key('src_dirs') and kwds['src_dirs'] or [] + self.output_dir = kwds.has_key('output_dir') and kwds['output_dir'] or None + + self.buffer = StringIO.StringIO() + + XMLGenerator.__init__(self, self.buffer, 'UTF-8') + + + def add_gentoo_javadoc(self, name, attrs): + self.p(u'<%s ' % name) + for a,v in attrs.items(): + self.write_attr(a,v) + + self.p(u'>') + + if name == "project": + javadoc_str = """ + <target name=\"gentoojavadoc\" > + <mkdir dir=\"""" + self.output_dir + """\" /> + <javadoc + destdir=\"""" + self.output_dir + """\" + author="true" + version="true" + use="true" + windowtitle="javadoc"> + """ + + for src_dir in self.src_dirs: + javadoc_str += """ + <fileset dir=\"""" + src_dir + """\" defaultexcludes="yes"> + <include name="**/*.java"/> + </fileset> + """ + + javadoc_str += """ + </javadoc> + </target> + """ + + self.p(u'%s' % javadoc_str) + + + # write as they are or delete if wanted attributes first + # next, add / update + def modify_elements(self, name, attrs): + self.p(u'<%s ' % name) + + match = ( name in self.elems ) + matchSource = ( name in self.sourceElems ) + matchTarget = ( name in self.targetElems ) + matchDelete = ( name in self.deleteElems ) + + for a,v in attrs.items(): + if not ( + (match and a in self.attributes) + or (matchSource and a in self.sourceAttributes) + or (matchTarget and a in self.targetAttributes) + or (matchDelete and a in self.deleteAttributes) + ): + self.write_attr(a,v) + + if matchSource: + for i, attr in enumerate(self.sourceAttributes): + self.write_attr(attr, self.sourceValues[i]) + + if matchTarget: + for i, attr in enumerate(self.targetAttributes): + self.write_attr(attr, self.targetValues[i]) + + if match: + for i, attr in enumerate(self.attributes): + self.write_attr(attr, self.values[i]) + + self.p(u'>') + + + def char_data(self, data): + self.p(escape(data)) + + + def write(self, out_stream): + value = self.buffer.getvalue() + out_stream.write(value) + self.buffer.truncate(0) + + + def p(self,str): + self.buffer.write(str.encode('utf8')) + + + def write_attr(self,a,v): + self.p(u'%s=%s ' % (a,quoteattr(v, {u'©':'©'}))) + + + def process(self, in_stream, callback): + self.startElement = callback + from xml.sax import parseString + parseString(in_stream, self) + self.p(u'\n') + +#set expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap diff --git a/src/py/javatoolkit/xml/__init__.py b/src/py/javatoolkit/xml/__init__.py new file mode 100644 index 0000000..ac60ace --- /dev/null +++ b/src/py/javatoolkit/xml/__init__.py @@ -0,0 +1,7 @@ +#! /usr/bin/python2 +# +# Copyright(c) 2007, Gentoo Foundation +# Licensed under the GNU General Public License, v2 + +if __name__ == "__main__": + print "This is not an executable module" diff --git a/src/py/maven-helper.py b/src/py/maven-helper.py new file mode 100755 index 0000000..8243f72 --- /dev/null +++ b/src/py/maven-helper.py @@ -0,0 +1,153 @@ +#!/usr/bin/env python +# -*- coding: UTF-8 -*- +# vim: set ai ts=8 sts=0 sw=8 tw=0 noexpandtab: + +# Copyright 2004-2007 Gentoo Foundation +# Distributed under the terms of the GNU General Public Licence v2 + +# Authors: +# kiorky <kiorky@cryptelium.net>: +# Maintainer: Gentoo Java Herd <java@gentoo.org> +# Python based POM navigator + +# ChangeLog +# kiorky <kiorky@cryptelium.net>: +# 31/05/2007 Add rewrite feature +# +# kiorky <kiorky@cryptelium.net>: +# 08/05/2007 initial version + + +import sys +import StringIO +from optparse import OptionParser, make_option +from javatoolkit.maven import MavenPom + +__version__ = "$Revision: 1.1 $"[11:-2] + + +if __name__ == '__main__': + usage = "XML MAVEN POM MODULE " + __version__ + "\n" + usage += "Copyright 2004,2006,2007 Gentoo Foundation\n" + usage += "Distributed under the terms of the GNU General Public Lincense v2\n" + usage += "Please contact the Gentoo Java Team <java@gentoo.org> with problems.\n" + usage += "\n" + usage += "Usage:\n" + usage += " %s [-a] [-v] [-g] [-d] [-f fic.xml]\n" % sys.argv[0] + usage += "Or:\n" + usage += " %s --rewrite [--classpath some.jar:class.jar:path.jar] [--source JVM_VER ] |--target JVM_VER]\n" % sys.argv[0] + usage += " JVM_VER ::= 1.4 || 1.5 " + usage += "\n" + usage += "If the -f parameter is not utilized, the script will read and\n" + usage += "write to stdin and stdout respectively. The use of quotes on\n" + usage += "parameters will break the script.\n" + + + def error(message): + print "ERROR: " + message + sys.exit(1) + + + def doAction(stream,options): + pom = MavenPom(options) + if options.p_rewrite: + pom.parse(stream, pom.rewrite) + elif options.p_ischild or options.p_group or options.p_dep or options.p_artifact or options.p_version: + pom.parse(stream, pom.getDescription) + + return pom + + + def run(): + if options.files: + import os + for file in options.files: + # First parse the file into memory + cwd = os.getcwd() + dirname = os.path.dirname(file) + if dirname != '': # for file comes out as '' + os.chdir(os.path.dirname(file)) + + f = open(os.path.basename(file),"r") + fs = f.read() + f.close() + # parse file and return approtiate pom object + pom = doAction(fs,options) + if options.p_rewrite: + f = open(os.path.basename(file),"w") + f.write(pom.read()) + f.close() + else: + print "%s" % pom.read() + + os.chdir(cwd) + + else: + # process stdin + pom = doAction(sys.stdin.read(),options) + print pom.read() + + + +############### MAIN ############### + + + + options_list = [ + make_option ("-a", "--artifact", action="store_true", dest="p_artifact", help="get artifact name."), + make_option ("-c", "--classpath", action="append", dest="classpath", help="set classpath to use with maven."), + make_option ("-s", "--source", action="append", dest="p_source", help="Java source version."), + make_option ("-t", "--target", action="append", dest="p_target", help="Java target version."), + make_option ("-d", "--depependencies" , action="store_true", dest="p_dep", help="get dependencies infos"), + make_option ("-f", "--file", action="append", dest="files", help="Transform files instead of operating on stdout and stdin"), + make_option ("-g", "--group" , action="store_true", dest="p_group", help="get artifact group."), + make_option ("-r", "--rewrite", action="store_true", dest="p_rewrite", help="rewrite poms to use our classpath"), + make_option ("-p", "--ischild", action="store_true", dest="p_ischild", help="return true if this is a child pom"), + make_option ("-v", "--version" , action="store_true", dest="p_version", help="get artifact version."), + ] + + parser = OptionParser(usage, options_list) + (options, args) = parser.parse_args() + + # Invalid Arguments Must be smited! + if not options.p_ischild and not options.p_rewrite and not options.p_dep and not options.p_version and not options.p_artifact and not options.p_group: + print usage + print + error("No action was specified.") + + if options.files: + if len(options.files) > 1: + error("Please specify only one pom at a time.") + + if options.p_rewrite: + valid_sources = ["1.4","1.5"] + for source in valid_sources: + if options.p_source: + if len(options.p_source) != 1: + error("Please specify one and only one source.") + + if options.p_source[0] not in valid_sources: + error("Source %s is not valid" % options.p_source[0]) + + if options.p_target: + if len(options.p_target) != 1: + error("Please specify one and only one target.") + + if options.p_target[0] not in valid_sources: + error("Target %s is not valid" % options.p_target[0]) + + # join any classpathes if any + if options.classpath: + if len(options.classpath) > 1: + start =[] + start.append(options.classpath[0]) + for item in options.classpath[1:]: + start[0] += ":%s" % (item) + + options.classpath = start + + # End Invalid Arguments Check + # main loop + run() + +#set expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap diff --git a/src/py/xml-rewrite-3.py b/src/py/xml-rewrite-3.py new file mode 100755 index 0000000..68421bc --- /dev/null +++ b/src/py/xml-rewrite-3.py @@ -0,0 +1,250 @@ +#!/usr/bin/env python +# -*- coding: UTF-8 -*- + +# Copyright 2004-2008 Gentoo Foundation +# Distributed under the terms of the GNU General Public Licence v2 + +# Authors: +# Saleem Abdulrasool <compnerd@compnerd.org> +# Petteri Räty <betelgeuse@gentoo.org> +# kiorky <kiorky@cryptelium.net> +# Maintainer: Gentoo Java Herd <java@gentoo.org> +# Python based XML modifier + +# ChangeLog +# kiorky <kiorky@cryptelium.net> +# May 2007 - Now, all things can be done in one pass, saving us some times :) +# - javadoc target generation added +# - Rewritten to be more logical +# Petteri Räty <betelgeuse@gentoo.org +# December 06, 2006 - Changed to use xml.parsers.expat and basically rewrote the whole file +# December 29, 2006 - Added a SAX based implementation to handle entities etc ( test on dev-java/skinlf ) +# Saleem A. <compnerd@compnerd.org> +# December 23, 2004 - Initial Write +# December 24, 2004 - Added usage information + +import os +import sys +from optparse import OptionParser, make_option +from javatoolkit.xml import * + +__version__ = "$Revision: 1.7 $"[11:-2] + +#TODO Refactor into javatoolkit.xml if ever used! +#class ExpatRewriter(StreamRewriterBase): +# """ +# The only problem with this Expat based implementation is that it does not +# handle entities doctypes etc properly so for example dev-java/skinlf fails. +# """ +# def process(self, in_stream): +# from xml.parsers.expat import ParserCreate +# parser = ParserCreate() +# +# parser.StartElementHandler = self.start_element +# parser.EndElementHandler = self.end_element +# parser.CharacterDataHandler = self.char_data +# parser.ParseFile(in_stream) +# self.p(u'\n') +# +# +# def start_element(self, name, attrs): +# StreamRewriterBase(self, name, attrs.iteritems()) +# +# +# def end_element(self,name): +# self.p(u'</%s>' % name) + +if __name__ == '__main__': + usage = "XML Rewrite Python Module Version " + __version__ + "\n" + usage += "Copyright 2004,2006,2007 Gentoo Foundation\n" + usage += "Distributed under the terms of the GNU General Public Lincense v2\n" + usage += "Please contact the Gentoo Java Team <java@gentoo.org> with problems.\n" + usage += "\n" + usage += "Usage:\n" + usage += " " + sys.argv[0] + " [-f file] --delete [-g] -n tag [-n tag] -m attribute [-m attribute] [-i index]\n" + usage += " " + sys.argv[0] + " [-f file] --change [-g] -e tag [-e tag] -a attribute -v value [-a attribute -v value] \\\n" + usage += " [--source-element tag] [--source-attribute attribute --source-value value] \\\n" + usage += " [--target-element tag] [--target-attribute attribute --target-value value] [-i index]\n" + usage += "Or:\n" + usage += " " + sys.argv[0] + " [-f file] --javadoc --source-directory dir [--source-directory dir2] --output-directory dir3 \n" + usage += "Or:\n" + usage += " " + sys.argv[0] + " [-f file] -g\n" + usage += "\n" + usage += "Or:\n" + usage += " " + sys.argv[0] + " [-f file] --maven-cleaning\n" + usage += "\n" + usage += "Or for more detailed help:\n" + usage += " " + sys.argv[0] + " -h\n" + usage += "\n" + usage += "Multiple actions can be done simultaneously\n" + usage += "\n" + usage += "If the -f parameter is not utilized, the script will read and\n" + usage += "write to stdin and stdout respectively. The use of quotes on\n" + usage += "parameters will break the script.\n" + + def error(message): + print "ERROR: " + message + sys.exit(1) + + + # instream is a string + def doRewrite(rewriter, in_stream, callback=None, **kwargs): + if callback: + rewriter.process(in_stream, callback, **kwargs) + else: + rewriter.process(in_stream, **kwargs) + + out = StringIO.StringIO() + rewriter.write(out) + return out.getvalue() + + + def processActions(options, f): + out_stream = f.read() + newcp="${gentoo.classpath}" + if options.gentoo_classpath: + rewriter = DomRewriter(options.elements, options.attributes, options.values, options.index) + out_stream = doRewrite(rewriter, out_stream, rewriter.add_gentoo_classpath,classpath = newcp) + + if options.doJavadoc: + rewriter = SaxRewriter(src_dirs = options.src_dirs, output_dir = options.javadoc_dir[0]) + out_stream = doRewrite(rewriter, out_stream, rewriter.add_gentoo_javadoc) + + if options.doAdd or options.doDelete: + # java-ant-2.eclass does not use these options so we can optimize the ExpatWriter + # and let the DomRewriter do these. Also keeps the index option compatible for sure. + if options.index: + rewriter = DomRewriter(options.delete_elements, options.delete_attributes, options.values, options.index) + out_stream = doRewrite(rewriter, out_stream, rewriter.delete_elements) + else: + rewriter = SaxRewriter( + elems = options.elements, + attributes = options.attributes, + values = options.values, + sourceElems = options.source_elements, + sourceAttributes = options.source_attributes, + sourceValues = options.source_values, + targetElems = options.target_elements, + targetAttributes = options.target_attributes, + targetValues = options.target_values, + deleteElems = options.delete_elements, + deleteAttributes = options.delete_attributes + ) + out_stream = doRewrite(rewriter, out_stream, rewriter.modify_elements) + + if options.doMaven: + if options.mavenMultiProjectsDirs: + for elem in options.mavenMultiProjectsDirs: + newcp+=":"+elem + + rewriter = DomRewriter() + out_stream = doRewrite(rewriter, out_stream, rewriter.add_gentoo_classpath, classpath = newcp) + + deleteElems = [] + deleteAttributes = [] + deleteElems.append("target") + deleteAttributes.append("depends") + rewriter = SaxRewriter( deleteElems = deleteElems, deleteAttributes = deleteAttributes) + out_stream = doRewrite(rewriter, out_stream, rewriter.modify_elements) + + return out_stream + + + options_list = [ + make_option ("-a", "--attribute", action="append", dest="attributes", help="Attribute of the matching elements to change. These can be chained for multiple value-attribute pairs"), + make_option ("-b", "--target-element", action="append", dest="target_elements", help="Tag of the element of which the attributes to be changed just in target scope. These can be chained for multiple elements."), + make_option ("-c", "--change", action="store_true", dest="doAdd", default=False, help="Change the value of an attribute. If it does not exist, it will be created."), + make_option ("-d", "--delete", action="store_true", dest="doDelete", default=False, help="Delete an attribute from matching elements."), + make_option ("-e", "--element", action="append", dest="elements", help="Tag of the element of which the attributes to be changed. These can be chained for multiple elements."), + make_option ("-f", "--file", action="append", dest="files", help="Transform files instead of operating on stdout and stdin"), + make_option ("-g", "--gentoo-classpath", action="store_true", dest="gentoo_classpath", help="Rewrite build.xml to use gentoo.classpath where applicable."), + make_option ("-i", "--index", type="int", dest="index", help="Index of the match. If none is specified, the changes will be applied to all matches within the document. Starts from zero."), + make_option ("-j", "--javadoc", action="store_true", dest="doJavadoc", default=False, help="add a basic javadoc target. Sources must be placed in ${WORKDIR}/javadoc_src."), + make_option ("-k", "--target-attribute", action="append", dest="target_attributes", help="Attribute of the matching elements to change. These can be chained for multiple value-attribute pairs (for targetonly)"), + make_option ("-l", "--target-value", action="append", dest="target_values", help="Value to set the attribute to (targeronly)."), + make_option ("-m", "--delete-attribute", action="append", dest="delete_attributes", help="Attribute of the matching elements to delete. These can be chained for multiple value-attribute pairs"), + make_option ("-n", "--delete-element", action="append", dest="delete_elements", help="Tag of the element of which the attributes to be deleted. These can be chained for multiple elements."), + make_option ("-o", "--output-directory", action="append", dest="javadoc_dir", help="javadoc output directory. Must be an existing directory"), + make_option ("-p", "--source-directory", action="append", dest="src_dirs", help="source directory for javadoc generation. Must be an existing directory"), + make_option ("-q", "--maven-cleaning", action="store_true", dest="doMaven", default=False, help="Turns on maven generated build.xml cleanup rewriting."), + make_option ("-r", "--source-element", action="append", dest="source_elements", help="Tag of the element of which the attributes to be changed just in source scope. These can be chained for multiple elements."), + make_option ("-s", "--multi-project-dirs", action="append", dest="mavenMultiProjectsDirs", help="Dirs in classpath notation"), + + make_option ("-t", "--source-attribute", action="append", dest="source_attributes", help="Attribute of the matching elements to change. These can be chained for multiple value-attribute pairs (for source only)"), + make_option ("-v", "--value", action="append", dest="values", help="Value to set the attribute to."), + make_option ("-y", "--source-value", action="append", dest="source_values", help="Value to set the attribute to. (sourceonly)") + ] + parser = OptionParser(usage, options_list) + (options, args) = parser.parse_args() + + # Invalid Arguments Must be smited! + if not options.doAdd and not options.doDelete and not options.gentoo_classpath and not options.doJavadoc and not options.doMaven: + print usage + print + error("No action was specified.") + + if options.doAdd: + if not options.elements and not options.target_elements and not options.source_elements: + error("At least one element (global, source only or target only) and attribute must be specified.") + + for elem in ( options.source_attributes or [] ): + if elem in ( options.attributes or [] ): + error("You can't set an attribute in global and source scope at the same time") + + for elem in ( options.target_attributes or [] ): + if elem in ( options.attributes or [] ): + error("You can't set an attribute in global and target scope at the same time") + + if options.doAdd and (len(options.values or []) != len(options.attributes or []) + or len(options.source_values or [] ) != len(options.source_attributes or []) + or len(options.target_values or [] ) != len(options.target_attributes or [])): + error("You must give attribute(s)/value(s) for every element you are changing.") + + if options.doJavadoc: + if len(options.src_dirs or []) < 1: + error("You must specify as least one src directory.") + + for dir in options.src_dirs: + if not os.path.isdir(dir): + error("You must specify existing directory for src output") + + if len(options.javadoc_dir or []) != 1: + error("You must specify one and only one javadoc output directory.") + + if not os.path.isdir(options.javadoc_dir[0]): + error("You must specify an existing directory for javadoc output") + + if options.doDelete: + if not options.delete_elements: + error("At least one element to delete must be specified.") + + if options.doDelete and ( len(options.attributes or []) < 0): + error("You must give attribute(s) to delete for every element you are changing.") + # End Invalid Arguments Check + + + # main loop + if options.files: + for file in options.files: + print "Rewriting %s" % file + # First parse the file into memory + # Tricks with cwd are needed for relative includes of other xml files to build.xml files + cwd = os.getcwd() + dirname = os.path.dirname(file) + if dirname != '': # for file = build.xml comes out as '' + os.chdir(os.path.dirname(file)) + + f = open(os.path.basename(file), "r") + outxml = processActions(options, f) + os.chdir(cwd) + f.close() + # Then write it back to the file + f = open(file, "w") + f.write(outxml) + f.close() + + else: + outxml = processActions(options, sys.stdin) + sys.stdout.write(outxml) + +#set expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap |