Skip to content

Instantly share code, notes, and snippets.

@hanslovsky
Last active July 5, 2022 00:03
Show Gist options
  • Save hanslovsky/ed62974c6353eb360c793834381249fe to your computer and use it in GitHub Desktop.
Save hanslovsky/ed62974c6353eb360c793834381249fe to your computer and use it in GitHub Desktop.
Find duplicates in jars
#!/usr/bin/env python3.9
from functools import reduce
from pathlib import Path
from subprocess import check_output
import re
m2_repo = Path.home() / '.m2' / 'repository'
def gav(coordinate):
g, a, _, v, _ = coordinate.split(':', 4)
return g, a, v
def coordinate_to_path(coordinate):
g, a, v = gav(coordinate)
group_path = reduce(lambda a, b: a / b, g.split('.'), m2_repo)
return group_path / a / v / f'{a}-{v}.jar'
def list_jar_contents(path, include=re.compile('.*class$'), exclude=re.compile('.*module-info.class')):
lines = check_output(['jar', 'tf', str(path)]).decode('utf-8').split()
lines = map(lambda x: x.strip(), lines)
return {l for l in lines if include.match(l) and not exclude.match(l)}
coordinates = [
'org.scijava:scijava-common:jar:2.83.3:compile',
'org.scijava:parsington:jar:2.0.0:compile',
'org.bushe:eventbus:jar:1.4:compile',
'org.jetbrains.kotlin:kotlin-script-runtime:jar:1.4.30:runtime',
'org.jetbrains.kotlin:kotlin-stdlib:jar:1.4.30:compile',
'org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.4.30:compile',
'org.jetbrains:annotations:jar:13.0:compile',
'org.jetbrains.kotlin:kotlin-scripting-jsr223:jar:1.4.30:compile',
'org.jetbrains.kotlin:kotlin-scripting-common:jar:1.4.30:runtime',
'org.jetbrains.kotlinx:kotlinx-coroutines-core:jar:1.3.8:runtime',
'org.jetbrains.kotlin:kotlin-scripting-jvm:jar:1.4.30:runtime',
'org.jetbrains.kotlin:kotlin-scripting-jvm-host:jar:1.4.30:runtime',
'org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:jar:1.4.30:runtime',
'org.jetbrains.kotlin:kotlin-scripting-compiler-impl-embeddable:jar:1.4.30:runtime',
'junit:junit:jar:4.13:test',
'org.hamcrest:hamcrest-core:jar:1.3:test',
'org.jetbrains.kotlin:kotlin-stdlib-jdk8:jar:1.4.30:compile',
'org.jetbrains.kotlin:kotlin-stdlib-jdk7:jar:1.4.30:compile'
]
additional_jars = {'scripting-kotlin': Path.home() / 'workspace' / 'ntakt' / 'scripting-kotlin' / 'target' / 'scripting-kotlin-0.1.2-SNAPSHOT.jar'}
contents = {c: list_jar_contents(coordinate_to_path(c)) for c in coordinates} | {k: list_jar_contents(v) for k, v in additional_jars.items()}
duplicates = []
jars = tuple(contents.keys())
for idx, c1 in enumerate(jars):
for c2 in jars[idx+1:]:
intersection = contents[c1].intersection(contents[c2])
duplicates.append(((c1, c2), list(intersection)))
for duplicate in duplicates:
if len(duplicate[1]) > 0:
print(duplicate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment