aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlistair Bush <ali_bush@gentoo.org>2008-01-03 03:16:41 +0000
committerAlistair Bush <ali_bush@gentoo.org>2008-01-03 03:16:41 +0000
commitd4b391a81dbb5bb0866ae338631236718b982adf (patch)
tree187e924c6ac6e27cc7dc0ae966dc9c9033597f9e
parentRefactored code of active sub-projects into new file structure. Currently mis... (diff)
downloadjavatoolkit-d4b391a81dbb5bb0866ae338631236718b982adf.tar.gz
javatoolkit-d4b391a81dbb5bb0866ae338631236718b982adf.tar.bz2
javatoolkit-d4b391a81dbb5bb0866ae338631236718b982adf.zip
Started setup.py and migrated more tools.
svn path=/projects/javatoolkit/branches/layout_refactor_branch/; revision=5884
-rw-r--r--setup.py27
-rw-r--r--src/man/findclass.126
-rw-r--r--src/maven/Makefile21
-rwxr-xr-xsrc/maven/maven-helper.py346
-rw-r--r--src/py/buildparser108
-rwxr-xr-xsrc/py/class-version-verify.py108
-rw-r--r--src/py/findclass85
-rw-r--r--src/py/javatoolkit/cvv.py54
-rw-r--r--src/py/javatoolkit/parser/.cvsignore1
-rw-r--r--src/py/javatoolkit/parser/__init__.py14
-rw-r--r--src/py/javatoolkit/parser/buildproperties.py83
-rw-r--r--src/py/javatoolkit/parser/helpers.py61
-rw-r--r--src/py/javatoolkit/parser/manifest.py61
-rw-r--r--src/py/javatoolkit/parser/parser.py16
-rw-r--r--src/py/javatoolkit/parser/tree.py107
-rwxr-xr-xsrc/py/xml-rewrite-2.py321
-rwxr-xr-xsrc/py/xml-rewrite.py142
17 files changed, 1214 insertions, 367 deletions
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..be91a9a
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+
+# Copyright 1999-2008 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+# $Header: $
+
+from distutils.core import setup
+
+setup (
+ name = 'javatoolkit',
+ version = '0.2.1',
+ description = 'Collection of Gentoo-specific tools for Java.',
+ maintainer = 'Gentoo Java Team',
+ maintainer_email = 'java@gentoo.org',
+ url = 'html://www.gentoo.org',
+ packages = ["javatoolkit", "javatoolkit.maven", "javatoolkit.xml"],
+ package_dir = { 'javatoolkit' : 'src/py/javatoolkit' },
+ scripts = [
+ "src/py/maven-helper.py",
+ "src/py/xml-rewrite-3.py",
+ "src/py/findclass",
+ "src/py/xml-rewrite.py",
+ "src/py/buildparser"
+ ]
+)
+
+#set expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
diff --git a/src/man/findclass.1 b/src/man/findclass.1
new file mode 100644
index 0000000..7d2f959
--- /dev/null
+++ b/src/man/findclass.1
@@ -0,0 +1,26 @@
+.TH FINDCLASS "1" "May 2005" "javatollkit 0.1.0" "User Commands"
+.SH NAME
+findclass \- Searches for installed classes or packages
+.SH SYNOPSIS
+.B findclass
+\fI[options]\fB
+class.or.package.Name
+.SH DESCRIPTION
+.PP
+.I findclass
+Searches the system for the given package or class name, and prints
+its location to stdout.
+.SH OPTIONS
+.TP
+\fB-v --verbose\fI
+Generates a more verbose output.
+
+.SH AUTHORS
+Karl Trygve Kalleberg <karltk@gentoo.org>, 2004-2005
+.br
+Fabio Lessa <flessa@gmail.com>, 2005
+
+
+.SH "SEE ALSO"
+java-config(1)
+
diff --git a/src/maven/Makefile b/src/maven/Makefile
deleted file mode 100644
index 56a1acf..0000000
--- a/src/maven/Makefile
+++ /dev/null
@@ -1,21 +0,0 @@
-# Copyright 2004-2007 Gentoo Foundation
-# Distributed under the terms of the GNU General Public License v2
-#
-# $Header: $
-
-include ../../makedefs.mak
-
-all:
-
-dist:
- mkdir -p ../../$(distdir)/src/maven
- cp Makefile maven-helper.py ../../$(distdir)/src/maven
-
-install: all
- install -m 0755 maven-helper.py $(mavenhelperdir)/
-
-testdir:=tests
-
-test:
- echo "nothing atm"
-
diff --git a/src/maven/maven-helper.py b/src/maven/maven-helper.py
deleted file mode 100755
index b7acb6e..0000000
--- a/src/maven/maven-helper.py
+++ /dev/null
@@ -1,346 +0,0 @@
-#!/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
-
-
-
-__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__':
- 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()
-
diff --git a/src/py/buildparser b/src/py/buildparser
new file mode 100644
index 0000000..0cff6d3
--- /dev/null
+++ b/src/py/buildparser
@@ -0,0 +1,108 @@
+#! /usr/bin/python
+#
+# Copyright(c) 2006, James Le Cuirot <chewi@aura-online.co.uk>
+# Copyright(c) 2005, Karl Trygve Kalleberg <karltk@gentoo.org>
+#
+# Licensed under the GNU General Public License, v2
+#
+# $Header:$
+
+import os
+import sys
+from optparse import OptionParser
+
+sys.path.insert(0, "/usr/share/javatoolkit/pym")
+
+from parser.parser import Parser
+from parser.buildproperties import BuildPropertiesParser
+from parser.manifest import ManifestParser
+from parser.tree import Node, ParseError
+
+__author__ = ["James Le Cuirot <chewi@aura-online.co.uk>", "Karl Trygve Kalleberg <karltk@gentoo.org>"]
+__version__ = "0.2.0"
+__productname__ = "buildparser"
+__description__ = "A parser for build.properties and JAR manifest files."
+
+
+def parse_args():
+
+ usage = 'buildparser [options] [node name] [replacement] <filename>'
+ about = __productname__ + " : " + __description__ + "\n" + \
+ "Version : " + __version__ + "\n" \
+ "Authors : " + __author__[0]
+
+ for x in __author__[1:]:
+ about += "\n " + x
+
+ parser = OptionParser(usage, version=about)
+ parser.add_option('-t', '--type', action='store', type='choice',
+ dest='type', choices=['manifest', 'buildprops'],
+ help='Type of file to parse: manifest or buildprops')
+
+ opt, args = parser.parse_args()
+
+ if len(args) > 3:
+ parser.error("Too many arguments specified!")
+
+ elif len(args) == 0:
+ parser.error("A filename must be specified!")
+
+ elif not os.path.isfile(args[-1]):
+ parser.error(args[-1] + " does not exist!")
+
+ return opt, args
+
+def main():
+
+ opt, args = parse_args()
+
+ f = open(args[-1])
+
+ t = Node()
+ p = Parser()
+
+ try:
+ if opt.type == "manifest":
+ p = ManifestParser()
+
+ elif opt.type == "buildprops":
+ p = BuildPropertiesParser()
+
+ elif os.path.basename(f.name) == "MANIFEST.MF":
+ p = ManifestParser()
+
+ elif os.path.basename(f.name) == "build.properties":
+ p = BuildPropertiesParser()
+
+ else:
+ sys.exit(__productname__ + ": error: Unknown file type. Specify using the -t option.")
+
+ t = p.parse(f)
+
+ except ParseError:
+ sys.exit(__productname__ + ": error: Unable to parse file.")
+
+ if len(args) > 2:
+ n = t.find_node(args[0])
+
+ if n != None:
+ n.value = args[1]
+
+ p.output(t)
+
+ elif len(args) > 1:
+ n = t.find_node(args[0])
+
+ if n != None:
+ print n.value
+
+ else:
+ for x in t.node_names():
+ print x
+
+if __name__ == '__main__':
+ try:
+ main()
+ except KeyboardInterrupt:
+ print "Interrupted by user, aborting."
+
diff --git a/src/py/class-version-verify.py b/src/py/class-version-verify.py
new file mode 100755
index 0000000..b571c05
--- /dev/null
+++ b/src/py/class-version-verify.py
@@ -0,0 +1,108 @@
+#!/bin/env python
+#
+# Copyright(c) 2005, Thomas Matthijs <axxo@gentoo.org>
+# Copyright(c) 2005, Gentoo Foundation
+#
+# Licensed under the GNU General Public License, v2
+#
+# $Header: /var/cvsroot/gentoo-src/javatoolkit/src/bsfix/class-version-verify.py,v 1.2 2005/07/19 10:35:18 axxo Exp $
+
+import os,sys
+from os.path import join, getsize
+from struct import unpack
+from optparse import OptionParser, make_option
+from zipfile import ZipFile
+
+class cvv:
+ def __init__(self, target):
+ self.target = target
+ self.good = []
+ self.bad = []
+
+ def add(self, version, jar, file):
+ if version <= self.target:
+ self.good.append(("1."+str(version), jar, file))
+ else:
+ self.bad.append(("1."+str(version), jar, file))
+
+ def do_class(self,filename):
+ classFile = file(filename,"rb")
+ classFile.seek(4)
+
+ temp = classFile.read(4)
+ #(version,) = unpack('>i',temp)
+ (version,) = unpack('>xxh',temp)
+ version-=44
+
+ self.add(version, None, filename)
+
+ def do_jar(self, filename):
+ zipfile = ZipFile(filename, 'r')
+
+ for file in zipfile.namelist():
+ if file.endswith('class'):
+ classFile = zipfile.read(file)
+
+ (version,) = unpack('>h',classFile[6:8])
+ version-=44
+
+ self.add(version, filename, file)
+
+ def do_file(self, filename):
+ if not os.path.islink(filename):
+ if filename.endswith(".class"):
+ self.do_class(filename)
+ if filename.endswith(".jar"):
+ self.do_jar(filename)
+
+if __name__ == '__main__':
+
+ options_list = [
+ make_option ("-r", "--recurse", action="store_true", dest="deep", default=False, help="go into dirs"),
+ make_option ("-t", "--target", type="string", dest="version", help="target version that is valid"),
+
+ make_option ("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Print version of every calss"),
+ make_option ("-s", "--silent", action="store_true", dest="silent", default=False, help="No output"),
+ make_option ("-f", "--file-only", action="store_true", dest="file_only", default=False, help="Only output the files"),
+ ]
+
+ parser = OptionParser("%prog -t version [-r] [-v] [-s] <class/jar files or dir>", options_list)
+ (options, args) = parser.parse_args()
+
+ if not options.version:
+ print "-t is mandatory"
+ sys.exit(2)
+
+ options.version = int(options.version.split(".")[-1])
+
+ cvv = cvv(options.version)
+
+ for arg in args:
+ if os.path.isfile(arg):
+ cvv.do_file(arg)
+
+ if options.deep and os.path.isdir(arg):
+ for root, dirs, files in os.walk(arg):
+ for filename in files:
+ cvv.do_file("%s/%s" % (root, filename))
+
+ if options.file_only:
+ lst = set([set[1] for set in cvv.bad])
+ for i in lst:
+ print i
+ else:
+
+ if options.verbose:
+ for set in cvv.good:
+ print "Good: %s %s %s" % set
+
+ if not options.silent:
+ for set in cvv.bad:
+ print "Bad: %s %s %s" % set
+
+ print "CVV: %s\nChecked: %i Good: %i Bad: %i" % (options.version, len(cvv.good)+len(cvv.bad) , len(cvv.good), len(cvv.bad))
+
+ if len(cvv.bad) > 0:
+ sys.exit(1)
+ else:
+ sys.exit(0)
diff --git a/src/py/findclass b/src/py/findclass
new file mode 100644
index 0000000..15802c9
--- /dev/null
+++ b/src/py/findclass
@@ -0,0 +1,85 @@
+#!/usr/bin/env python
+#
+# Copyright (c) Karl Trygve Kalleberg <karltk@gentoo.org>
+# Copyright (c) Fabio Lessa <flessa@gmail.com>
+# Copyright (c) 2005, Gentoo Foundation
+#
+# Python rewrite from the bash findclass initially performed
+# by Fabio.
+#
+# Licensed under the GNU General Public License, v2.
+#
+
+import os
+import re
+import sys
+import glob
+from optparse import OptionParser
+from commands import getstatusoutput
+from java_config.jc_util import find_exec, collect_packages
+
+__author__ = "Karl Trygve Kalleberg <karltk@gentoo.org> and Fabio Lessa <flessa@gmail.com>"
+__version__ = "0.1.0"
+__productname__ = "findclass"
+__description__ = "Gentoo Java Class Query Tool"
+
+def parse_args():
+
+ usage = 'findclass [options] class.or.package.Name'
+ about = __productname__ + " : " + __description__ + "\n" + \
+ "Authors : " + __author__ + \
+ "Version : " + __version__
+
+ parser = OptionParser(usage, version=about)
+ parser.add_option('-v', '--verbose', action='store_true',
+ dest='verbose', help='generate verbose output')
+ opt, files = parser.parse_args()
+
+ if len(files) < 1:
+ parser.error("Must supply at least one class or package name")
+
+ return opt, files
+
+def main():
+
+ opt, files = parse_args()
+
+ jarcmd = find_exec('jar')
+
+ javapaths = [ f.replace('.', '/') for f in files ]
+ matchers = [ re.compile(p) for p in javapaths ]
+
+ for pkg in get_all_packages():
+ if opt.verbose: print "Searching package %s" % pkg
+
+ for jar in collect_packages(pkg).split(':'):
+ if opt.verbose: print "Searching jar %s" % jar
+
+ status, out = getstatusoutput("%s tvf %s" % (jarcmd, jar))
+
+ for m in matchers:
+ if m.search(out):
+ if opt.verbose: print "Found in %s" % pkg,
+ print jar
+
+def get_all_packages():
+
+ pkg = glob.glob('/usr/share/*/package.env')
+ pkg = [os.path.basename(os.path.dirname(i)) for i in pkg]
+
+ classpath = glob.glob('/usr/share/*/classpath.env')
+ classpath = [os.path.basename(os.path.dirname(i)) for i in classpath]
+
+ dir = glob.glob('/usr/share/java/packages/*')
+ dir = [os.path.basename(i) for i in dir]
+
+ pkg.extend(classpath)
+ pkg.extend(dir)
+ return pkg
+
+
+if __name__ == '__main__':
+ try:
+ main()
+ except KeyboardInterrupt:
+ print "Interrupted by user, aborting."
diff --git a/src/py/javatoolkit/cvv.py b/src/py/javatoolkit/cvv.py
new file mode 100644
index 0000000..20bd16c
--- /dev/null
+++ b/src/py/javatoolkit/cvv.py
@@ -0,0 +1,54 @@
+# Copyright 2005, Thomas Matthijs <axxo@gentoo.org>
+# Copyright 1999-2008 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+# $Header: $
+
+#import os,sys
+#from os.path import join, getsize
+#from struct import unpack
+#from optparse import OptionParser, make_option
+from zipfile import ZipFile
+
+class cvv:
+ def __init__(self, target):
+ self.target = target
+ self.good = []
+ self.bad = []
+
+ def add(self, version, jar, file):
+ if version <= self.target:
+ self.good.append(("1."+str(version), jar, file))
+ else:
+ self.bad.append(("1."+str(version), jar, file))
+
+ def do_class(self,filename):
+ classFile = file(filename,"rb")
+ classFile.seek(4)
+
+ temp = classFile.read(4)
+ #(version,) = unpack('>i',temp)
+ (version,) = unpack('>xxh',temp)
+ version-=44
+
+ self.add(version, None, filename)
+
+ def do_jar(self, filename):
+ zipfile = ZipFile(filename, 'r')
+
+ for file in zipfile.namelist():
+ if file.endswith('class'):
+ classFile = zipfile.read(file)
+
+ (version,) = unpack('>h',classFile[6:8])
+ version-=44
+
+ self.add(version, filename, file)
+
+ def do_file(self, filename):
+ if not os.path.islink(filename):
+ if filename.endswith(".class"):
+ self.do_class(filename)
+ if filename.endswith(".jar"):
+ self.do_jar(filename)
+
+#set expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
diff --git a/src/py/javatoolkit/parser/.cvsignore b/src/py/javatoolkit/parser/.cvsignore
new file mode 100644
index 0000000..0d20b64
--- /dev/null
+++ b/src/py/javatoolkit/parser/.cvsignore
@@ -0,0 +1 @@
+*.pyc
diff --git a/src/py/javatoolkit/parser/__init__.py b/src/py/javatoolkit/parser/__init__.py
new file mode 100644
index 0000000..d96be04
--- /dev/null
+++ b/src/py/javatoolkit/parser/__init__.py
@@ -0,0 +1,14 @@
+#! /usr/bin/python
+#
+# Copyright(c) 2004, Karl Trygve Kalleberg <karltk@gentoo.org>
+# Copyright(c) 2004, Gentoo Foundation
+#
+# Licensed under the GNU General Public License, v2
+#
+# $Header: $
+
+from helpers import *
+import buildproperties
+
+if __name__ == "__main__":
+ print "This is not an executable module"
diff --git a/src/py/javatoolkit/parser/buildproperties.py b/src/py/javatoolkit/parser/buildproperties.py
new file mode 100644
index 0000000..0c2ee53
--- /dev/null
+++ b/src/py/javatoolkit/parser/buildproperties.py
@@ -0,0 +1,83 @@
+#! /usr/bin/python
+#
+# Copyright(c) 2006, James Le Cuirot <chewi@aura-online.co.uk>
+# Copyright(c) 2004, Karl Trygve Kalleberg <karltk@gentoo.org>
+# Copyright(c) 2004, Gentoo Foundation
+#
+# Licensed under the GNU General Public License, v2
+#
+# $Header: $
+
+from tree import *
+import parser
+
+class BuildPropertiesParser(parser.Parser):
+
+ def parse(self, ins):
+ """ Parse an input stream containing an ant build.properties file. Return a
+ structured document represented by tree.Node
+
+ @param ins - input stream
+ @return tree.Node containing the structured representation
+ """
+
+ lineno = 0
+ continued_line = False
+ inside_html_comment = False
+ attrib = ""
+ value = ""
+ root = Node()
+
+ for x in ins.readlines():
+ lineno += 1
+ x = x.strip()
+
+ if inside_html_comment and x.find("-->") != -1:
+ inside_html_comment = False
+ x = x.split("-->", 1)[0]
+
+ if x.find("<!--") != -1:
+ inside_html_comment = True
+
+ if inside_html_comment:
+ continue
+
+ if continued_line:
+ continued_line = False
+ value += x.strip("\"")
+
+ if len(value) and value[-1] == "\\":
+ value = value[:-1]
+ continued_line = True
+ continue
+
+ root.add_kid(Node(attrib,value))
+ continue
+
+ if len(x) == 0 or x[:1] == "#":
+ continue
+
+ x = x.split("#", 1)[0]
+ xs = x.split("=", 2)
+
+ if len(xs) > 1:
+ attrib = xs[0].strip()
+ value = xs[1].strip().strip("\"")
+
+ if value != "" and value[-1] == "\\":
+ value = value[:-1]
+ continued_line = True
+ continue
+
+ root.add_kid(Node(attrib,value))
+
+ else:
+ raise ParseError("Malformed line " + str(lineno))
+
+ return root
+
+ def output(self, tree):
+ tree.output("", " = ", "")
+
+if __name__ == "__main__":
+ print "This is not an executable module"
diff --git a/src/py/javatoolkit/parser/helpers.py b/src/py/javatoolkit/parser/helpers.py
new file mode 100644
index 0000000..62815de
--- /dev/null
+++ b/src/py/javatoolkit/parser/helpers.py
@@ -0,0 +1,61 @@
+#! /usr/bin/python
+#
+# Copyright(c) 2006, James Le Cuirot <chewi@aura-online.co.uk>
+# Copyright(c) 2004, Karl Trygve Kalleberg <karltk@gentoo.org>
+# Copyright(c) 2004, Gentoo Foundation
+#
+# Licensed under the GNU General Public License, v2
+#
+# $Header: $
+
+
+def expand(root, expr, realroot = None):
+ """Evaluates a path expression on a given tree.
+
+ @param root - the root of the tree
+ @param expr - the expression to resolve
+
+ @return the expanded string
+ """
+
+ if realroot == None:
+ realroot = root
+
+ expanded = ""
+ in_varref = False
+ varname = ""
+
+ for i in range(len(expr)):
+ x = expr[i]
+
+ if in_varref:
+
+ if x == "}":
+ in_varref = False
+ expanded += expand(root, realroot.find_node(varname).value, realroot)
+ varname = ""
+ elif x != "{":
+ varname += expr[i]
+
+ elif x == "$" and i < len(expr) and expr[i + 1] == "{":
+ in_varref = True
+
+ else:
+ expanded += x
+
+ return expanded
+
+def strip_varmarker(s):
+ """Strips away ${ and } in a variable expression. Idempotent if marker not found.
+
+ Example: "${foo}" -> "foo"
+ Example: "foo" -> "foo"
+ """
+
+ if s.startswith("${") and s.endswith("}"):
+ return s[2:-1]
+
+ return s
+
+if __name__ == "__main__":
+ print "This is not an executable module"
diff --git a/src/py/javatoolkit/parser/manifest.py b/src/py/javatoolkit/parser/manifest.py
new file mode 100644
index 0000000..810914a
--- /dev/null
+++ b/src/py/javatoolkit/parser/manifest.py
@@ -0,0 +1,61 @@
+#! /usr/bin/python
+#
+# Copyright(c) 2006, James Le Cuirot <chewi@aura-online.co.uk>
+#
+# Licensed under the GNU General Public License, v2
+#
+# $Header: $
+
+from tree import *
+import parser
+
+class ManifestParser(parser.Parser):
+
+ def parse(self, ins):
+ """ Parse an input stream containing a MANIFEST.MF file. Return a
+ structured document represented by tree.Node
+
+ @param ins - input stream
+ @return tree.Node containing the structured representation
+ """
+
+ lineno = 0
+ attrib = ""
+ value = ""
+ root = Node()
+
+ for x in ins.readlines():
+ lineno += 1
+
+ if len(x.strip()) == 0:
+ continue
+
+ if x[:1] == " ":
+ if attrib == "":
+ raise ParseError("Malformed line " + str(lineno))
+
+ value += x.strip()
+ continue
+
+ xs = x.split(": ", 2)
+
+ if len(xs) > 1:
+ if attrib != "":
+ root.add_kid(Node(attrib,value))
+
+ attrib = xs[0]
+ value = xs[1].strip()
+
+ else:
+ raise ParseError("Malformed line " + str(lineno))
+
+ if attrib != "":
+ root.add_kid(Node(attrib,value))
+
+ return root
+
+ def output(self, tree):
+ tree.output("", ": ", "", ",", " ");
+
+if __name__ == "__main__":
+ print "This is not an executable module"
diff --git a/src/py/javatoolkit/parser/parser.py b/src/py/javatoolkit/parser/parser.py
new file mode 100644
index 0000000..e99a7a1
--- /dev/null
+++ b/src/py/javatoolkit/parser/parser.py
@@ -0,0 +1,16 @@
+#! /usr/bin/python
+#
+# Copyright(c) 2006, James Le Cuirot <chewi@aura-online.co.uk>
+#
+# Licensed under the GNU General Public License, v2
+#
+# $Header: $
+
+class Parser:
+ def parse(self, ins):
+ raise NotImplementedError
+ def output(self, tree):
+ raise NotImplementedError
+
+if __name__ == "__main__":
+ print "This is not an executable module"
diff --git a/src/py/javatoolkit/parser/tree.py b/src/py/javatoolkit/parser/tree.py
new file mode 100644
index 0000000..eb51874
--- /dev/null
+++ b/src/py/javatoolkit/parser/tree.py
@@ -0,0 +1,107 @@
+#! /usr/bin/python
+#
+# Copyright(c) 2006, James Le Cuirot <chewi@aura-online.co.uk>
+# Copyright(c) 2004, Karl Trygve Kalleberg <karltk@gentoo.org>
+# Copyright(c) 2004, Gentoo Foundation
+#
+# Licensed under the GNU General Public License, v2
+#
+# $Header: $
+
+import sys
+
+class ParseError:
+ def __init__(self, error):
+ self.error = error
+
+class NodeIter:
+ def __init__(self, node):
+ self._node = node
+ self._index = 0
+ def next(self):
+ self._index += 1
+ if self._index >= len(self._node._kids):
+ raise StopIteration
+ return self._node._kids[self._index]
+
+class Node:
+ def __init__(self, name = None, value = None):
+ self.name = name
+ self.value = value
+ self._kids = []
+
+ def __iter__(self):
+ return NodeIter(self)
+
+ def add_kid(self, kid):
+ for x in self._kids:
+ if x.name == kid.name:
+ return
+
+ self._kids.append(kid)
+
+ def _dump_kids(self, indent, ous):
+ for x in self._kids:
+ x.dump(indent + 1)
+
+ """
+ Dump self as text to stream.
+ """
+ def dump(self, indent = 0, ous = sys.stdout):
+ if self.name:
+ ous.write((" " * indent) + self.name + " = " + self.value + "\n")
+
+ self._dump_kids(indent, ous)
+
+ """
+ Output self as text to stream using the given format.
+ """
+ def output(self, before, between, after, wrap = None, indent = "", ous = sys.stdout):
+ if self.name:
+ outval = self.value
+
+ if wrap != None:
+ outval = outval.replace(wrap, wrap + "\n" + indent)
+
+ ous.write(before + self.name + between + outval + after + "\n")
+
+ for x in self._kids:
+ x.output(before, between, after, wrap, indent, ous)
+
+ """
+ Returns a lists of all the node names.
+ """
+ def node_names(self):
+ names = []
+
+ if self.name:
+ names.append(self.name)
+
+ for x in self._kids:
+ names.extend(x.node_names())
+
+ return names
+
+ """
+ Find a given node name in a tree.
+
+ @param tree - the tree to search in
+ @param nodename - the name of the node to search for
+
+ @return reference to the found node, if any
+ """
+ def find_node(self, nodename):
+ if self.name == nodename:
+ return self
+
+ else:
+ for x in self._kids:
+ y = x.find_node(nodename)
+
+ if y != None:
+ return y
+
+ return None
+
+if __name__ == "__main__":
+ print "This is not an executable module"
diff --git a/src/py/xml-rewrite-2.py b/src/py/xml-rewrite-2.py
new file mode 100755
index 0000000..a143ee3
--- /dev/null
+++ b/src/py/xml-rewrite-2.py
@@ -0,0 +1,321 @@
+#!/usr/bin/env python
+# -*- coding: UTF-8 -*-
+# vim: set ai ts=8 sts=0 sw=8 tw=0 noexpandtab:
+
+# Copyright 2004-2006 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>
+# Maintainer: Gentoo Java Herd <java@gentoo.org>
+# Python based XML modifier
+
+# ChangeLog
+# 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 sys
+import StringIO
+
+from xml.sax.saxutils import quoteattr,escape
+
+from optparse import OptionParser, make_option
+
+__version__ = "$Revision: 1.7 $"[11:-2]
+
+def add_gentoo_classpath(document):
+ matches = document.getElementsByTagName("classpath")
+ gcp = document.createElement("location")
+ gcp.setAttribute("path","${gentoo.classpath}")
+
+ handled_refs = set()
+ for match in matches:
+ if match.hasAttribute("refid"):
+ refid = match.getAttribute("refid")
+ for ref in document.getElementsByTagName("path"):
+ id = ref.getAttribute("id")
+ if id not in handled_refs and id == refid:
+ gcp = document.createElement("pathelement")
+ gcp.setAttribute("path","${gentoo.classpath}")
+ ref.appendChild(gcp)
+ handled_refs.add(id)
+ else:
+ match.appendChild(gcp)
+
+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, attributes, values=None, index=None):
+ self.modify = modifyElems
+ self.attributes = attributes
+ self.values = values
+ self.index = index
+
+ def change_elem(self, elem):
+ 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 process(self,in_stream,callback=None):
+ from xml.dom.minidom import parse
+
+ self.document = parse(in_stream);
+
+ if callback:
+ callback(self.document)
+
+ if not self.modify:
+ return
+
+ for tag in self.modify:
+ matches = self.document.getElementsByTagName(tag)
+ if matches:
+ if self.index == None:
+ for match in matches:
+ self.change_elem(match)
+ else:
+ self.change_elem(matches[self.index])
+
+ def write(self,stream):
+ stream.write(self.document.toxml())
+
+class StreamRewriterBase:
+
+ def __init__(self, elems, attributes, values, index,
+ sourceElems = [], sourceAttributes = [], sourceValues = [],
+ targetElems = [], targetAttributes = [], targetValues = [] ):
+ self.buffer = StringIO.StringIO()
+ self.__write = self.buffer.write
+ self.elems = elems or []
+ self.attributes = attributes or []
+ self.values = values or []
+ self.sourceElems = sourceElems or []
+ self.sourceAttributes = sourceAttributes or []
+ self.sourceValues = sourceValues or []
+ self.targetElems = targetElems or []
+ self.targetAttributes = targetAttributes or []
+ self.targetValues = targetValues or []
+
+ def p(self,str):
+ self.__write(str.encode('utf8'))
+
+ def write(self, out_stream):
+ value = self.buffer.getvalue()
+ out_stream.write(value)
+ self.buffer.truncate(0)
+
+ def write_attr(self,a,v):
+ self.p(u'%s=%s ' % (a,quoteattr(v, {u'©':'&#169;'})))
+
+ def start_element(self, name, attrs):
+ self.p(u'<%s ' % name)
+
+ match = ( name in self.elems )
+ matchSource = ( name in self.sourceElems )
+ matchTarget = ( name in self.targetElems )
+
+ for a,v in attrs:
+ if not (
+ (match and a in self.attributes)
+ or (matchSource and a in self.sourceAttributes)
+ or (matchTarget and a in self.targetAttributes)
+ ):
+ 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'>')
+
+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)
+
+ def char_data(self,data):
+ self.p(escape(data))
+
+from xml.sax.saxutils import XMLGenerator
+class SaxRewriter(XMLGenerator, StreamRewriterBase):
+ """
+ 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, elems, attributes, values, index,
+ sourceElems = [], sourceAttributes = [], sourceValues = [],
+ targetElems = [], targetAttributes = [], targetValues = []):
+ StreamRewriterBase.__init__(self, elems, attributes, values, index,
+ sourceElems, sourceAttributes, sourceValues,
+ targetElems, targetAttributes, targetValues)
+ XMLGenerator.__init__(self, self.buffer, 'UTF-8')
+
+ def process(self, in_stream):
+ from xml.sax import parse
+ parse(in_stream, self)
+ self.p(u'\n')
+
+ def startElement(self, name, attrs):
+ self.start_element(name, attrs.items())
+
+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 += " xml-rewrite.py [-f file] --delete [-g] -e tag [-e tag] -a attribute [-a attribute] [-i index]\n"
+ usage += " xml-rewrite.py [-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 += " xml-rewrite.py [-f file] -g\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)
+
+
+# if len(sys.argv) == 1:
+# usage(True)
+
+ options_list = [
+ 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 ("-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 ("-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 ("-v", "--value", action="append", dest="values", help="Value to set the attribute to."),
+ 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 ("-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 ("-y", "--source-value", action="append", dest="source_values", help="Value to set the attribute to. (sourceonly)"),
+ make_option ("-j", "--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 ("-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 ("-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.")
+ ]
+
+ 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:
+ print usage
+ print
+ error("No action was specified.")
+
+ if not options.gentoo_classpath:
+ if options.doAdd and options.doDelete:
+ error("Unable to perform multiple actions simultaneously.")
+
+ 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.")
+
+ # End Invalid Arguments Check
+
+ def get_rewriter(options):
+ if options.index or options.doDelete or options.gentoo_classpath:
+ # 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.
+ rewriter = DomRewriter(options.elements, options.attributes, options.values, options.index)
+ else:
+ rewriter = SaxRewriter(options.elements, options.attributes, options.values, options.index,
+ options.source_elements, options.source_attributes, options.source_values,
+ options.target_elements, options.target_attributes, options.target_values)
+
+ return rewriter
+
+ rewriter = get_rewriter(options)
+
+ if options.files:
+ import os
+ 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")
+ if options.gentoo_classpath:
+ rewriter.process(f,add_gentoo_classpath)
+ else:
+ rewriter.process(f)
+
+ os.chdir(cwd)
+ f.close()
+ # Then write it back to the file
+ f = open(file, "w")
+ rewriter.write(f)
+ f.close()
+
+ else:
+ if options.gentoo_classpath:
+ rewriter.process(sys.stdin,add_gentoo_classpath)
+ else:
+ rewriter.process(sys.stdin)
+
+ rewriter.write(sys.stdout)
+
diff --git a/src/py/xml-rewrite.py b/src/py/xml-rewrite.py
new file mode 100755
index 0000000..624fd77
--- /dev/null
+++ b/src/py/xml-rewrite.py
@@ -0,0 +1,142 @@
+#!/usr/bin/env python
+# -*- coding: UTF-8 -*-
+
+# Copyright 2004 Gentoo Foundation
+# Distributed under the terms of the GNU General Public Licence v2
+# $Header: /var/cvsroot/gentoo-src/javatoolkit/src/bsfix/xml-rewrite.py,v 1.6 2005/07/19 10:35:18 axxo Exp $
+
+# Author: Saleem Abdulrasool <compnerd@compnerd.org>
+# Maintainer: Gentoo Java Herd <java@gentoo.org>
+# Python based XML modifier
+
+# ChangeLog
+# Saleem A. <compnerd@compnerd.org>
+# December 23, 2004 - Initial Write
+# December 24, 2004 - Added usage information
+
+import sys
+from xml.dom.minidom import parse
+
+from optparse import OptionParser, make_option
+from xml.dom import NotFoundErr
+
+__version__ = "$Revision: 1.6 $"[11:-2]
+
+class IOWrapper:
+ def __init__(self, object):
+ self.stream = object
+
+ def stream(self):
+ return self.stream
+
+ def write(self, data):
+ if self.stream == sys.stdin:
+ sys.stdout.write(data.encode('utf-8'))
+ else:
+ file = open(self.stream, 'w')
+ file.write(data.encode('utf-8'))
+ file.close()
+
+class Rewriter:
+ def __init__(self, stream):
+ self.stream = stream
+ self.document = parse(stream.stream);
+
+ def modifyAttribute(self, elementTag, attribute, value, index=None):
+ matches = self.document.getElementsByTagName(elementTag)
+ if matches:
+ if index == None:
+ for match in matches:
+ match.setAttribute(attribute, value)
+ else:
+ matches[index].setAttribute(attribute, value)
+
+ def deleteAttribute(self, elementTag, attribute, index=None):
+ matches = self.document.getElementsByTagName(elementTag)
+ if matches:
+ if index == None:
+ for match in matches:
+ try:
+ match.removeAttribute(attribute)
+ except NotFoundErr:
+ continue
+ else:
+ try:
+ matches[index].removeAttribute(attribute)
+ except NotFoundErr:
+ return
+
+ def write(self):
+ self.stream.write(self.document.toxml())
+
+
+if __name__ == '__main__':
+ usage = "XML Rewrite Python Module Version " + __version__ + "\n"
+ usage += "Copyright 2004 Gentoo Foundation\n"
+ usage += "Distributed under the terms of the GNU General Public Lincense v2\n"
+ usage += "Please contact the Gentoo Java Herd <java@gentoo.org> with problems.\n"
+ usage += "\n"
+ usage += "Usage:\n"
+ usage += " xml-rewrite.py [-f] --delete -e tag [-e tag] -a attribute [-i index]\n"
+ usage += " xml-rewrite.py [-f] --change -e tag [-e tag] -a attribute -v value [-i index]\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)
+
+
+# if len(sys.argv) == 1:
+# usage(True)
+
+ options_list = [
+ make_option ("-f", "--file", type="string", dest="file", help="Read input from file instead of stdin"),
+ 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 ("-a", "--attribute", type="string", dest="attribute", help="Attribute of the matching elements to change."),
+ make_option ("-v", "--value", type="string", dest="value", help="Value to set the attribute to."),
+ 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.")
+ ]
+
+ parser = OptionParser(usage, options_list)
+ (options, args) = parser.parse_args()
+
+
+ # Invalid Arguments Must be smited!
+ if not options.doAdd and not options.doDelete:
+ print usage
+ print
+ error("No action was specified.")
+
+ if options.doAdd and options.doDelete:
+ error("Unable to perform multiple actions simultaneously.")
+
+ if not options.elements or not options.attribute:
+ error("At least one element and attribute must be specified.")
+
+ if options.doAdd and not options.value:
+ error("You must specify values for the attributes to be modified.")
+ # End Invalid Arguments Check
+
+
+ if options.file:
+ source = options.file
+ else:
+ source = sys.stdin
+
+ rewriter = Rewriter(IOWrapper(source))
+
+ if options.doDelete:
+ for element in options.elements:
+ rewriter.deleteAttribute(element, options.attribute, options.index)
+
+ if options.doAdd:
+ for element in options.elements:
+ rewriter.modifyAttribute(element, options.attribute, options.value, options.index)
+
+ rewriter.write()