aboutsummaryrefslogtreecommitdiff
blob: 7af980d15ca2be16c84b3eefe9fd684639c89c1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3
#
# Copyright(c) 2006, 2008 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
#
import os
import sys
from optparse import OptionParser

sys.path.insert(0, "/usr/share/javatoolkit/pym")

from javatoolkit.parser.parser import Parser
from javatoolkit.parser.buildproperties import BuildPropertiesParser
from javatoolkit.parser.manifest import ManifestParser
from javatoolkit.parser.tree import Node, ParseError

__author__ = ["James Le Cuirot <chewi@aura-online.co.uk>", "Karl Trygve Kalleberg <karltk@gentoo.org>"]
__version__ = "0.3.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')

    parser.add_option('-i', '--in-place', action='store_true', dest='in_place',
                    help='Edit file in place when replacing')

    parser.add_option('-w', '--wrap', action='store_true', dest='wrap',
                    help='Wrap when returning singular values')

    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)
        f.close()

    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]
        else:
            t.add_kid(Node(args[0], args[1]))

        if opt.in_place:
            f = open(args[-1], "w+")
            p.output(f, t)
            f.close()

        else:
            p.output(sys.stdout, t)

    elif len(args) > 1:
        n = t.find_node(args[0])

        if n != None:
            if opt.wrap:
                print(p.wrapped_value(n))
            else:
                print(n.value)

    else:
        for x in t.node_names():
            print(x)

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print("Interrupted by user, aborting.")

#set expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap