|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from zipfile import ZipFile |
| 4 | +from glob import glob |
| 5 | +from itertools import groupby |
| 6 | +from optparse import OptionParser |
| 7 | +from fnmatch import fnmatchcase |
| 8 | + |
| 9 | +class Printer: |
| 10 | + def before(self): |
| 11 | + pass |
| 12 | + |
| 13 | + def put(self, clzname, jars): |
| 14 | + pass |
| 15 | + |
| 16 | + def after(self): |
| 17 | + pass |
| 18 | + |
| 19 | +class JarOnlyPrinter(Printer): |
| 20 | + bucket = [] |
| 21 | + |
| 22 | + def put(self, clzname, jars): |
| 23 | + if jars not in self.bucket: |
| 24 | + print jars |
| 25 | + self.bucket.append(jars) |
| 26 | + |
| 27 | +class AllPrinter(Printer): |
| 28 | + def put(self, clzname, jars): |
| 29 | + print clzname,jars |
| 30 | + |
| 31 | +class HTMLPrinter(Printer): |
| 32 | + bucket = {} |
| 33 | + |
| 34 | + def before(self): |
| 35 | + print '''<html> |
| 36 | + <head> |
| 37 | + <style> |
| 38 | + .odd {background-color:#ffddee;} |
| 39 | + ul {display:none} |
| 40 | + </style> |
| 41 | +
|
| 42 | + <script> |
| 43 | + var opentab = function(target){ |
| 44 | + while(target.nodeName != 'DIV') target = target.parentNode; |
| 45 | + var ul = target.getElementsByTagName('UL')[0]; |
| 46 | + ul.style.display = ul.style.display == 'block' ? 'none' : 'block'; |
| 47 | + } |
| 48 | + </script> |
| 49 | + </head> |
| 50 | + <body> |
| 51 | + ''' |
| 52 | + |
| 53 | + def put(self, clzname, jars): |
| 54 | + jars = ' '.join(['<span class="%s">%s</span>' % ('odd' if jars.index(j) % 2 == 0 else 'even' ,j.split('/')[-1]) for j in jars]) |
| 55 | + if self.bucket.has_key(jars): |
| 56 | + self.bucket[jars] += [clzname] |
| 57 | + else: |
| 58 | + self.bucket[jars] = [clzname] |
| 59 | + |
| 60 | + def after(self): |
| 61 | + for jars,clznames in self.bucket.items(): |
| 62 | + print '''<div><h3><a href="javascript:;" onclick="javascript:opentab(this);">%s</a></h3><ul>''' % jars |
| 63 | + for clzname in clznames: |
| 64 | + print '''<li>%s <a href="javascript:;" onclick="javascript:opentab(this);">CLOSE</a></li> ''' % clzname.rstrip('.class').replace('/','.') |
| 65 | + print '''</ul></div>''' |
| 66 | + |
| 67 | + print '''</body> |
| 68 | + </html> |
| 69 | + ''' |
| 70 | + |
| 71 | + |
| 72 | +walkzip = lambda f : [(cf,f) for cf in ZipFile(f).namelist() if cf.lower().endswith('.class')] |
| 73 | + |
| 74 | +def matchexclude(z, excludes): |
| 75 | + if isinstance(excludes,str): |
| 76 | + excludes = excludes.split(',') |
| 77 | + |
| 78 | + for patten in excludes: |
| 79 | + if fnmatchcase(z.split('/')[-1], patten): |
| 80 | + return True |
| 81 | + |
| 82 | + return False |
| 83 | + |
| 84 | +def zfilter(clzname, jars): |
| 85 | + jars = tuple(jars) |
| 86 | + if len(jars) > 1: |
| 87 | + yield (clzname,[j[1] for j in jars ]) |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + op = OptionParser('usage: %prog [options] dir1 dir2 ...') |
| 91 | + |
| 92 | + op.add_option("--html", dest="html", default=False, action="store_true") |
| 93 | + op.add_option("-r", dest="recursive", default=False, action="store_true") # not impl |
| 94 | + op.add_option("-v", dest="verbose", default=False, action="store_true") |
| 95 | + op.add_option("-e", "--excludes", dest="excludes", default=[]) |
| 96 | + |
| 97 | + options,args = op.parse_args() |
| 98 | + if len(args) == 0 : |
| 99 | + args = [ '.' ] |
| 100 | + |
| 101 | + printer = HTMLPrinter() if options.html else ( AllPrinter() if options.verbose else JarOnlyPrinter() ) |
| 102 | + |
| 103 | + printer.before() |
| 104 | + for t in groupby(sorted([c for p in args for z in glob(p + '/*.jar') if not matchexclude(z, options.excludes) for c in walkzip(z)]),lambda x : x[0]): |
| 105 | + for clzname, jars in zfilter(*t): |
| 106 | + printer.put(clzname,jars) |
| 107 | + |
| 108 | + printer.after() |
0 commit comments