Skip to content

Instantly share code, notes, and snippets.

@ilyaevseev
Created June 23, 2021 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilyaevseev/2c8884e8e0e6968aa79bc7b7fb6e98ef to your computer and use it in GitHub Desktop.
Save ilyaevseev/2c8884e8e0e6968aa79bc7b7fb6e98ef to your computer and use it in GitHub Desktop.
Quick-n-dirty finder for duplicated classes in JAR-files
#!/usr/bin/python
# Usage sample:
# python find-jdups.py dir1 dir2 ... | grep -v = | sort | uniq | awk -F/ '{ print $NF }'
import os
import sys
import zipfile
pathlist = dict()
def store_path(key, val):
if not key in pathlist:
pathlist[key] = [val]
return
pl = pathlist[key]
if pl[0] != val:
pl.append(val);
def read_jar(jarpath):
jar = zipfile.ZipFile(jarpath)
dir = jar.namelist()
for n in dir:
if '.class' in n:
store_path(n, jarpath)
def read_dir(dir):
for root, dirs, files in os.walk(dir):
for name in files:
if '.jar' in name:
read_jar(os.path.join(root, name))
def print_dups():
for cls in pathlist:
jars = pathlist[cls]
if len(jars) > 1:
print("%s = %d" % (cls, len(jars)))
for j in jars:
print("\t%s" % (j))
for n in range(1, len(sys.argv)):
read_dir(sys.argv[n])
print_dups()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment