jar 冲突、class 冲突的检测脚本

思路很简单,解开war包,解开 jar 包,发现有同名 package.class 就报出来,不管是否“兼容” ..

 

import zipfile
import io
from collections import defaultdict
import sys

if len(sys.argv) < 2:
    print("{0} /path/xxx.war".format(sys.argv[0]))
    exit(1)

exit_code = 0
jar_classes = defaultdict(set)
war_file = zipfile.ZipFile(sys.argv[1], "r")

for jar_file in war_file.infolist():
    if jar_file.filename.endswith(".jar"):
        jar_content = io.BytesIO(war_file.read(jar_file))
        jar_zip_file = zipfile.ZipFile(jar_content, "r")
        for jar_item in jar_zip_file.infolist():
            if jar_item.filename.endswith(".class"):
                class_full_name = jar_item.filename
                jar_classes[class_full_name].add(jar_file.filename.split("/")[-1].strip())

for class_full_name in sorted(jar_classes.keys()):
    jar_filename_set = jar_classes[class_full_name]
    if len(jar_filename_set) > 1:
        exit_code = 1
        class_full_name = ".".join(class_full_name.replace("/", ".").split(".")[:-1])
        print("{1}\t{0}".format(class_full_name, ", ".join(jar_filename_set)))

war_file.close()
exit(exit_code)

 

 

 

posted @ 2019-11-28 11:46  killkill  阅读(688)  评论(0编辑  收藏  举报