diff options
author | Karl Trygve Kalleberg <karltk@gentoo.org> | 2005-09-09 22:58:58 +0000 |
---|---|---|
committer | Karl Trygve Kalleberg <karltk@gentoo.org> | 2005-09-09 22:58:58 +0000 |
commit | 4668ed8a0e3a8350ca54fea7b00920ee3af7c1a2 (patch) | |
tree | 969ce9891aa5130d64f39f6ac7160bf3e50749cb | |
parent | no dont install/compile jxslt, cant depend on a jdk, cleaner xslt (diff) | |
download | javatoolkit-4668ed8a0e3a8350ca54fea7b00920ee3af7c1a2.tar.gz javatoolkit-4668ed8a0e3a8350ca54fea7b00920ee3af7c1a2.tar.bz2 javatoolkit-4668ed8a0e3a8350ca54fea7b00920ee3af7c1a2.zip |
Imported work by Chewi
svn path=/javatoolkit/; revision=780
-rw-r--r-- | src/buildtool/buildtool | 94 | ||||
-rw-r--r-- | src/javatoolkit/parser/buildproperties.py | 7 | ||||
-rw-r--r-- | src/javatoolkit/parser/helpers.py | 1 | ||||
-rw-r--r-- | src/javatoolkit/parser/manifest.py | 55 | ||||
-rw-r--r-- | src/javatoolkit/parser/tree.py | 14 |
5 files changed, 165 insertions, 6 deletions
diff --git a/src/buildtool/buildtool b/src/buildtool/buildtool new file mode 100644 index 0000000..9118e6f --- /dev/null +++ b/src/buildtool/buildtool @@ -0,0 +1,94 @@ +#! /usr/bin/python +# +# Copyright(c) 2005, James Le Cuirot <chewi@ffaura.com> +# Copyright(c) 2005, Karl Trygve Kalleberg <karltk@gentoo.org> +# +# Licensed under the GNU General Public License, v2 +# +# $Header:$ + +from javatoolkit.parser import buildproperties +from javatoolkit.parser import manifest +from javatoolkit.parser.tree import Node, ParseError + +import os +import sys +from optparse import OptionParser + +__author__ = ["James Le Cuirot <chewi@ffaura.com>", "Karl Trygve Kalleberg <karltk@gentoo.org>"] +__version__ = "0.1.0" +__productname__ = "buildtool" +__description__ = "A parser for build.properties and JAR manifest files." + +def parse_args(): + + usage = 'buildtool [options] [node name] <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) > 2: + 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() + + try: + if opt.type == "manifest": + t = manifest.parse(f) + + elif opt.type == "buildprops": + t = buildproperties.parse(f) + + elif os.path.basename(f.name) == "MANIFEST.MF": + t = manifest.parse(f) + + elif os.path.basename(f.name) == "build.properties": + t = buildproperties.parse(f) + + else: + print __productname__ + ": error: Unknown file type. Specify using the -t option." + sys.exit() + + except ParseError: + print __productname__ + ": error: Unable to parse file." + sys.exit() + + if 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/javatoolkit/parser/buildproperties.py b/src/javatoolkit/parser/buildproperties.py index 7062e0c..2f299f5 100644 --- a/src/javatoolkit/parser/buildproperties.py +++ b/src/javatoolkit/parser/buildproperties.py @@ -1,5 +1,6 @@ #! /usr/bin/python2 # +# Copyright(c) 2005, James Le Cuirot <chewi@ffaura.com> # Copyright(c) 2004, Karl Trygve Kalleberg <karltk@gentoo.org> # Copyright(c) 2004, Gentoo Foundation # @@ -46,10 +47,10 @@ def parse(ins): xs = x.split("=", 2) if len(xs) > 1: - attrib = xs[0] - value = (xs[1].strip().strip("\"")) + attrib = xs[0].strip() + value = xs[1].strip().strip("\"") - if value[-1] == "\\": + if value != "" and value[-1] == "\\": value = value[:-1] continued_line = True continue diff --git a/src/javatoolkit/parser/helpers.py b/src/javatoolkit/parser/helpers.py index a0a301c..7468d8b 100644 --- a/src/javatoolkit/parser/helpers.py +++ b/src/javatoolkit/parser/helpers.py @@ -1,5 +1,6 @@ #! /usr/bin/python2 # +# Copyright(c) 2005, James Le Cuirot <chewi@ffaura.com> # Copyright(c) 2004, Karl Trygve Kalleberg <karltk@gentoo.org> # Copyright(c) 2004, Gentoo Foundation # diff --git a/src/javatoolkit/parser/manifest.py b/src/javatoolkit/parser/manifest.py new file mode 100644 index 0000000..e4a2d0e --- /dev/null +++ b/src/javatoolkit/parser/manifest.py @@ -0,0 +1,55 @@ +#! /usr/bin/python2 +# +# Copyright(c) 2005, James Le Cuirot <chewi@ffaura.com> +# +# Licensed under the GNU General Public License, v2 +# +# $Header: + +from tree import * + +def parse(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 + +if __name__ == "__main__": + print "This is not an executable module" diff --git a/src/javatoolkit/parser/tree.py b/src/javatoolkit/parser/tree.py index 8a55052..aeea3d5 100644 --- a/src/javatoolkit/parser/tree.py +++ b/src/javatoolkit/parser/tree.py @@ -75,9 +75,17 @@ class Node: @return reference to the found node, if any """ def find_node(self, nodename): - for x in self: - if x.name == nodename: - return x + 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" |