diff options
author | 2007-03-13 18:09:19 +0000 | |
---|---|---|
committer | 2007-03-13 18:09:19 +0000 | |
commit | 04e40ce4fbeb29a3a02cc75dc38be05edda30f90 (patch) | |
tree | 9113af80bf685ca16b7fb9d4f88ec1836f42352d | |
parent | Fixing problems with encodings and relative paths. (diff) | |
download | javatoolkit-04e40ce4fbeb29a3a02cc75dc38be05edda30f90.tar.gz javatoolkit-04e40ce4fbeb29a3a02cc75dc38be05edda30f90.tar.bz2 javatoolkit-04e40ce4fbeb29a3a02cc75dc38be05edda30f90.zip |
Add a new --gentoo-classpath switch to xml-rewrite-2.py
svn path=/projects/javatoolkit/trunk/; revision=3994
-rw-r--r-- | ChangeLog | 5 | ||||
-rwxr-xr-x | src/bsfix/xml-rewrite-2.py | 73 |
2 files changed, 58 insertions, 20 deletions
@@ -1,3 +1,8 @@ +2007-13-03 20:07 Petteri Räty <betelgeuse@gentoo.org> + + * src/bsfix/xml-rewrite-2.py, ChangeLog: + Add a new --gentoo-classpath switch + 2006-12-29 14:17 Petteri Räty <betelgeuse@gentoo.org> * src/bsfix/xml-rewrite-2.py, ChangeLog: diff --git a/src/bsfix/xml-rewrite-2.py b/src/bsfix/xml-rewrite-2.py index 42ea690..f22b74d 100755 --- a/src/bsfix/xml-rewrite-2.py +++ b/src/bsfix/xml-rewrite-2.py @@ -27,6 +27,25 @@ 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 @@ -51,11 +70,17 @@ class DomRewriter: except DomRewriter.NotFoundErr: continue - def process(self,in_stream): + 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: @@ -147,13 +172,15 @@ class SaxRewriter(XMLGenerator, StreamRewriterBase): if __name__ == '__main__': usage = "XML Rewrite Python Module Version " + __version__ + "\n" - usage += "Copyright 2004 Gentoo Foundation\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 Herd <java@gentoo.org> with problems.\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 -e tag [-e tag] -a attribute [-a attribute] [-i index]\n" - usage += " xml-rewrite.py [-f file] --change -e tag [-e tag] -a attribute -v value [-a attribute -v value] [-i index]\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] [-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" @@ -170,6 +197,7 @@ if __name__ == '__main__': 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."), @@ -183,33 +211,32 @@ if __name__ == '__main__': # Invalid Arguments Must be smited! - if not options.doAdd and not options.doDelete: + if not options.doAdd and not options.doDelete and not options.gentoo_classpath: print usage print error("No action was specified.") - if options.doAdd and options.doDelete: - error("Unable to perform multiple actions simultaneously.") + if not options.gentoo_classpath: + if options.doAdd and options.doDelete: + error("Unable to perform multiple actions simultaneously.") - if not options.elements or not options.attributes: - error("At least one element and attribute must be specified.") + if not options.elements or not options.attributes: + error("At least one element and attribute must be specified.") - if options.doAdd and not options.values: - error("You must specify values for the attributes to be modified.") - - if options.doAdd and len(options.values) != len(options.attributes): - error("You must give value for every attribute you are changing.") + if options.doAdd and not options.values: + error("You must specify values for the attributes to be modified.") + + if options.doAdd and len(options.values) != len(options.attributes): + error("You must give value for every attribute you are changing.") # End Invalid Arguments Check def get_rewriter(options): - if options.index or options.doDelete: + 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) - print "Using DOM to rewrite the build.xml files" else: rewriter = SaxRewriter(options.elements, options.attributes, options.values, options.index) - print "Using Sax to rewrite the build.xml files" return rewriter @@ -226,7 +253,10 @@ if __name__ == '__main__': if dirname != '': # for file = build.xml comes out as '' os.chdir(os.path.dirname(file)) f = open(os.path.basename(file),"r") - rewriter.process(f) + 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 @@ -234,6 +264,9 @@ if __name__ == '__main__': rewriter.write(f) f.close() else: - rewriter.process(sys.stdin) + if options.gentoo_classpath: + rewriter.process(sys.stdin) + else: + rewriter.process(sys.stdin) rewriter.write(sys.stdout) |