diff options
author | Alistair Bush <ali_bush@gentoo.org> | 2007-12-31 03:52:47 +0000 |
---|---|---|
committer | Alistair Bush <ali_bush@gentoo.org> | 2007-12-31 03:52:47 +0000 |
commit | e96eb3c3f6803a02f6e0d4be3262930eb2119253 (patch) | |
tree | 94944ae3ebca7dacd3bbfd8f4a5001faf94ec150 | |
parent | Committing refactor of maven-helper before updating the layout of the javatoo... (diff) | |
download | javatoolkit-e96eb3c3f6803a02f6e0d4be3262930eb2119253.tar.gz javatoolkit-e96eb3c3f6803a02f6e0d4be3262930eb2119253.tar.bz2 javatoolkit-e96eb3c3f6803a02f6e0d4be3262930eb2119253.zip |
Reversing refactor of maven-helper. Work now to be done in branch.
svn path=/projects/javatoolkit/trunk/; revision=5874
-rw-r--r-- | src/maven/javatoolkit/__init__.py | 5 | ||||
-rw-r--r-- | src/maven/javatoolkit/maven/MavenPom.py | 199 | ||||
-rw-r--r-- | src/maven/javatoolkit/maven/__init__.py | 5 | ||||
-rwxr-xr-x | src/maven/maven-helper.py | 430 |
4 files changed, 311 insertions, 328 deletions
diff --git a/src/maven/javatoolkit/__init__.py b/src/maven/javatoolkit/__init__.py deleted file mode 100644 index fff4640..0000000 --- a/src/maven/javatoolkit/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -''' -javatoolkit module -''' - -__version__ = '1.1' diff --git a/src/maven/javatoolkit/maven/MavenPom.py b/src/maven/javatoolkit/maven/MavenPom.py deleted file mode 100644 index 9202779..0000000 --- a/src/maven/javatoolkit/maven/MavenPom.py +++ /dev/null @@ -1,199 +0,0 @@ -# -*- coding: UTF-8 -*- - -# Copyright 2007 Gentoo Foundation -# Distributed under the terms of the GNU General Public License v2 -# $Header: $ - -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) - -#:set expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap diff --git a/src/maven/javatoolkit/maven/__init__.py b/src/maven/javatoolkit/maven/__init__.py deleted file mode 100644 index fff4640..0000000 --- a/src/maven/javatoolkit/maven/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -''' -javatoolkit module -''' - -__version__ = '1.1' diff --git a/src/maven/maven-helper.py b/src/maven/maven-helper.py index 799001d..b7acb6e 100755 --- a/src/maven/maven-helper.py +++ b/src/maven/maven-helper.py @@ -21,73 +21,265 @@ import sys import StringIO from optparse import OptionParser, make_option -from javatoolkit.maven import MavenPom -from java_config_2.OutputFormatter import OutputFormatter + + __version__ = "$Revision: 1.1 $"[11:-2] + + +# 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) + + + if __name__ == '__main__': - - global printer - printer = OutputFormatter(True, True) - - 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 fatalError(msg): - printer._printError(msg) - 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() + 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() @@ -95,60 +287,60 @@ if __name__ == '__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 - fatalError("No action was specified.") - - if options.files: - if len(options.files) > 1: - fatalError("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: - fatalError("Please specify one and only one source.") - - if options.p_source[0] not in valid_sources: - fatalError("Source %s is not valid" % options.p_source[0]) - - if options.p_target: - if len(options.p_target) != 1: - fatalError("Please specify one and only one target.") - - if options.p_target[0] not in valid_sources: - fatalError("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() + 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() |