aboutsummaryrefslogtreecommitdiff
blob: 0cff6d3464fc9e209518fa997cb8eed4e801f248 (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
#! /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."