~lifeeth/sahana-eden/mobile

« back to all changes in this revision

Viewing changes to j2meapp/org.javarosa.sahana/tools/jar_usage.py

  • Committer: Praneeth Bodduluri
  • Date: 2009-07-05 22:18:06 UTC
  • Revision ID: lifeeth@gmail.com-20090705221806-ff63lagjkevf9n7t
JavaRosa demo based app added in j2meapp

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009 JavaRosa
 
2
 
3
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
 
4
# use this file except in compliance with the License. You may obtain a copy of
 
5
# the License at
 
6
 
7
# http://www.apache.org/licenses/LICENSE-2.0
 
8
 
9
# Unless required by applicable law or agreed to in writing, software
 
10
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
11
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
12
# License for the specific language governing permissions and limitations under
 
13
# the License.
 
14
 
 
15
import sys
 
16
import re
 
17
import zipfile
 
18
 
 
19
BYTE_KB = 1024.0
 
20
default_jarfile = 'JavaRosaDemo.jar'
 
21
default_obfusmapfile = 'obfuscation_mapping.txt'
 
22
default_outfile = 'jar_contents.csv'
 
23
 
 
24
#determine sort order of a file
 
25
suffix_order = ['class', 'xhtml']
 
26
def sort_filename(s):
 
27
  suffix = s.rsplit('.', 1)[-1]
 
28
  try:
 
29
    ordinal = suffix_order.index(suffix)
 
30
  except ValueError:
 
31
    ordinal = len(suffix_order)
 
32
  return (ordinal, s)
 
33
 
 
34
#initialize and parse parameters
 
35
if len(sys.argv) == 1: #no args, use defaults
 
36
  jarfile = default_jarfile
 
37
  obfusmapfile = default_obfusmapfile
 
38
elif len(sys.argv) == 2: #one arg, assume path
 
39
  path = sys.argv[1]
 
40
  if path[-1] != '\\':
 
41
    path = path + '\\'
 
42
  jarfile = path + default_jarfile
 
43
  obfusmapfile = path + default_obfusmapfile
 
44
else: #assume arg1 = jar file, arg2 = obfuscation mapping
 
45
  jarfile = sys.argv[1]
 
46
  obfusmapfile = sys.argv[2]
 
47
outfile = default_outfile
 
48
 
 
49
print 'JAR file: %s' % jarfile
 
50
print 'Obfuscation mapping: %s' % obfusmapfile
 
51
print 'Output written to: %s' % outfile
 
52
 
 
53
#read jar info
 
54
zipf = zipfile.ZipFile(jarfile, 'r')
 
55
contents = dict()
 
56
for i in zipf.infolist():
 
57
  contents[i.filename] = (i.file_size, i.compress_size)
 
58
 
 
59
#build de-obfuscation mapping
 
60
maptxt = open(obfusmapfile, 'r').readlines()
 
61
obfusmap = dict()
 
62
for line in filter(lambda s: re.compile('^ ').match(s) == None, maptxt): #classname mappings have no leading whitespace
 
63
  mapmatch = re.compile('^(?P<class>[A-Za-z0-9._$]+) -> (?P<tag>[a-z]+):$').search(line)
 
64
  if mapmatch != None: #None means class was not obfuscated
 
65
    mapentry = mapmatch.groupdict()
 
66
    obfusmap['%s.class' % mapentry['tag']] = '%s.class' % mapentry['class'].replace('.', '/')
 
67
 
 
68
#de-obfuscate
 
69
for file, info in contents.copy().iteritems():
 
70
  if file in obfusmap:
 
71
    del contents[file]
 
72
    contents[obfusmap[file]] = info
 
73
 
 
74
#write output
 
75
fout = open(outfile, 'w')
 
76
fout.write('File,Uncompressed Size (KB),Size in JAR (KB)\n')
 
77
for _, file in sorted(map(sort_filename, contents.iterkeys())):
 
78
  (size, compr_size) = contents[file]
 
79
  fout.write('%s,%s,%s\n' % (file, size/BYTE_KB, compr_size/BYTE_KB))
 
80
fout.close()
 
 
b'\\ No newline at end of file'