aboutsummaryrefslogtreecommitdiff
blob: b571c05b8728c1bd9806bc060f5061c0b8687219 (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
#!/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)