-
Notifications
You must be signed in to change notification settings - Fork 16
/
find-duplicate-classes.py
executable file
·47 lines (41 loc) · 1.21 KB
/
find-duplicate-classes.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
#!/bin/env python
import os, subprocess
def extract_classes(f):
lines = subprocess.check_output(['jar', 'tf', f]).split()
result = set()
for line in lines:
v = line.decode('utf-8').strip()
if v.endswith('.class') and not v.endswith('module-info.class'):
result.add(v)
return result
print('Reading JAR files... ', end='', flush=True)
paths = []
for root, dirs, files in os.walk('.'):
for name in files:
if not name.lower().endswith('.jar'): continue
paths.append(os.path.join(root, name))
paths.sort()
classes = {}
count = 0
perc = ''
for path in paths:
count += 1
classes[path] = extract_classes(path)
print('\b' * len(perc), end='')
perc = str(round(100 * count / len(paths))) + '% '
print(perc, end='', flush=True)
print()
print('Scanning for duplicate classes...')
for i1 in range(len(paths)):
p1 = paths[i1]
duplist = []
for i2 in range(i1 + 1, len(paths)):
p2 = paths[i2]
dups = classes[p1].intersection(classes[p2])
if len(dups) > 0:
duplist.append(f'==> {p2} (e.g. {next(iter(dups))})')
if len(duplist) > 0:
print(p1)
for line in duplist:
print(line)
print('Done!')