/
jarinfo.py
352 lines (245 loc) · 9.12 KB
/
jarinfo.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# This library is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, see
# <http://www.gnu.org/licenses/>.
"""
Module and utility for fetching information out of a JAR file, and
printing it out.
:author: Christopher O'Brien <obriencj@gmail.com>
:license: LGPL
"""
from __future__ import print_function
import sys
from argparse import ArgumentParser
from json import dump
from . import unpack_class
from .classinfo import cli_print_classinfo, add_classinfo_optgroup
from .dirutils import fnmatches
from .manifest import Manifest
from .ziputils import open_zip_entry, zip_file, zip_entry_rollup
__all__ = (
"JAR_PATTERNS", "JarInfo",
"main", "cli", "add_jarinfo_optgroup",
"cli_jar_classes", "cli_jar_manifest_info",
"cli_jar_provides", "cli_jar_requires",
"cli_jar_zip_info", "cli_jarinfo",
"cli_jarinfo_json",
)
# for reference by other modules
JAR_PATTERNS = (
"*.ear",
"*.jar",
"*.rar",
"*.sar",
"*.war",
)
REQ_BY_CLASS = "class.requires"
PROV_BY_CLASS = "class.provides"
class JarInfo(object):
def __init__(self, filename=None, zipfile=None):
if not (filename or zipfile):
raise TypeError("one of pathname or zipinfo must be specified")
self.filename = filename
self.zipfile = zipfile
self._requires = None
self._provides = None
def __del__(self):
self.close()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
return (exc_type is None)
def open(self, entry, mode='r'):
return open_zip_entry(self.get_zipfile(), entry, mode)
def _collect_requires_provides(self):
req = {}
prov = {}
# we need to collect private provides in order to satisfy deps
# for things like anonymous inner classes, which have access
# to private members of their parents. This is only used to
# filter out false-positive requirements.
p = set()
for entry in self.get_classes():
ci = self.get_classinfo(entry)
for sym in ci.get_requires():
req.setdefault(sym, list()).append((REQ_BY_CLASS, entry))
for sym in ci.get_provides(private=False):
prov.setdefault(sym, list()).append((PROV_BY_CLASS, entry))
for sym in ci.get_provides(private=True):
p.add(sym)
req = dict((k, v) for k, v in req.items() if k not in p)
self._requires = req
self._provides = prov
def get_requires(self, ignored=tuple()):
if self._requires is None:
self._collect_requires_provides()
d = self._requires
if ignored:
d = dict((k, v) for k, v in d.items()
if not fnmatches(k, *ignored))
return d
def get_provides(self, ignored=tuple()):
if self._provides is None:
self._collect_requires_provides()
d = self._provides
if ignored:
d = dict((k, v) for k, v in d.items()
if not fnmatches(k, *ignored))
return d
def get_classes(self):
"""
sequence of .class files in the underlying zip
"""
for n in self.get_zipfile().namelist():
if fnmatches(n, "*.class"):
yield n
def get_classinfo(self, entry):
"""
fetch a class entry as a JavaClassInfo instance
"""
with self.open(entry) as cfd:
return unpack_class(cfd)
def get_manifest(self):
""" fetch the sections from the MANIFEST.MF file as a Manifest
instance, or None if no MANIFEST.MF entry present """
mf_entry = "META-INF/MANIFEST.MF"
zipfile = self.get_zipfile()
if not zipfile.getinfo(mf_entry):
return None
mf = Manifest()
with self.open(mf_entry) as data:
mf.parse(data.read())
return mf
def get_zipfile(self):
if self.zipfile is None:
self.zipfile = zip_file(self.filename)
return self.zipfile
def close(self):
if self.zipfile:
self.zipfile.close()
self.zipfile = None
def cli_jar_manifest_info(options, jarinfo):
mf = jarinfo.get_manifest()
if not mf:
print("Manifest not present.")
print()
return
print("Manifest main section:")
for k, v in sorted(mf.items()):
print(" %s: %s" % (k, v))
for _name, sect in sorted(mf.sub_sections.items()):
print()
print("Manifest sub-section:")
for k, v in sorted(sect.items()):
print(" %s: %s" % (k, v))
print()
def cli_jar_zip_info(options, jarinfo):
zipfile = jarinfo.get_zipfile()
files, dirs, comp, uncomp = zip_entry_rollup(zipfile)
prcnt = (float(comp) / float(uncomp)) * 100
print("Contains %i files, %i directories" % (files, dirs))
print("Uncompressed size is %i" % uncomp)
print("Compressed size is %i (%0.1f%%)" % (comp, prcnt))
print()
def cli_jar_classes(options, jarinfo):
for entry in jarinfo.get_classes():
ci = jarinfo.get_classinfo(entry)
print("Entry: ", entry)
cli_print_classinfo(options, ci)
print()
def cli_jar_provides(options, jarinfo):
print("jar provides:")
for provided in sorted(jarinfo.get_provides().keys()):
if not fnmatches(provided, *options.api_ignore):
print(" ", provided)
print()
def cli_jar_requires(options, jarinfo):
print("jar requires:")
for required in sorted(jarinfo.get_requires().keys()):
if not fnmatches(required, *options.api_ignore):
print(" ", required)
print()
def cli_jarinfo(options, info):
if options.zip:
cli_jar_zip_info(options, info)
if options.manifest:
cli_jar_manifest_info(options, info)
if options.jar_provides:
cli_jar_provides(options, info)
if options.jar_requires:
cli_jar_requires(options, info)
if options.jar_classes:
cli_jar_classes(options, info)
def cli_jarinfo_json(options, info):
data = {}
if options.jar_provides:
data["jar.provides"] = info.get_provides(options.api_ignore)
if options.jar_requires:
data["jar.requires"] = info.get_requires(options.api_ignore)
if options.zip:
zipfile = info.get_zipfile()
filec, dirc, totalc, totalu = zip_entry_rollup(zipfile)
prcnt = (float(totalc) / float(totalu)) * 100
data["zip.type"] = zipfile.__class__.__name__
data["zip.file_count"] = filec
data["zip.dir_count"] = dirc
data["zip.uncompressed_size"] = totalu
data["zip.compressed_size"] = totalc
data["zip.compress_percent"] = prcnt
dump(data, sys.stdout, sort_keys=True, indent=2)
def cli(options):
if options.verbose:
options.zip = True
options.lines = True
options.locals = True
options.disassemble = True
options.sigs = True
options.constpool = True
options.indent = not(options.lines or
options.disassemble or
options.sigs)
for fn in options.jarfiles:
with JarInfo(filename=fn) as ji:
if options.json:
cli_jarinfo_json(options, ji)
else:
cli_jarinfo(options, ji)
return 0
def add_jarinfo_optgroup(parser):
g = parser.add_argument_group("JAR Info Options")
g.add_argument("--zip", action="store_true", default=False,
help="print zip information")
g.add_argument("--manifest", action="store_true", default=False,
help="print manifest information")
g.add_argument("--jar-classes", action="store_true", default=False,
help="print information about contained classes")
g.add_argument("--jar-provides", dest="jar_provides",
action="store_true", default=False,
help="API provides information at the JAR level")
g.add_argument("--jar-requires", dest="jar_requires",
action="store_true", default=False,
help="API requires information at the JAR level")
def create_optparser(progname):
parser = ArgumentParser(progname)
parser.add_argument("jarfiles", nargs="+",
help="JAR files to inspect")
parser.add_argument("--json", dest="json", action="store_true",
help="output in JSON mode")
add_jarinfo_optgroup(parser)
add_classinfo_optgroup(parser)
return parser
def main(args=sys.argv):
parser = create_optparser(args[0])
return cli(parser.parse_args(args[1:]))
#
# The end.