Skip to content

Instantly share code, notes, and snippets.

@xiangaoole
Last active May 9, 2023 04:28
Show Gist options
  • Save xiangaoole/802e56ed9af0b4359e3fb5cdc9153419 to your computer and use it in GitHub Desktop.
Save xiangaoole/802e56ed9af0b4359e3fb5cdc9153419 to your computer and use it in GitHub Desktop.
How to compare two android projects' dependencies
#!/usr/bin/python3
# -*- coding: utf-8 -*-
'''
@author: harold.gao
@contact: xiangaoole@gmail.com
@file: compareDeps.py
@time: 2021-03-29 15:16
@desc:
a. pip3 install packaging
b. touch attention_libs.csv
c. python3 compareDeps.py -p ./ --app ~/app_deps_tree.log --sdk ~/sdk_deps_tree.log
@link: https://medium.com/@xiangaoole/how-to-compare-two-android-projects-dependencies-7827e915b81d
'''
import re
import csv
from packaging.version import parse as parse_version
import argparse
from argparse import RawTextHelpFormatter
class BgColors:
HEADER = '\033[95m'
OK_BLUE = '\033[34m'
OK_CYAN = '\033[36m'
OK_GREEN = '\033[32m'
WARNING = '\033[33m'
FAIL = '\033[31m'
END = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def sortLibs(res, lib_dict):
file = open(res, 'r')
for line in file.readlines():
str = line.strip()
# deps line must starts with "|" or "+"
if not (str.startswith('| ') or str.startswith('+--- ')):
continue
# format 'group:module:version'
group_module_pattern = " [a-z0-9.\-_]{1,}:[a-z0-9.\-_]{1,}:"
if len(re.findall(group_module_pattern, str, re.IGNORECASE)) <= 0:
continue
module = getModule(str)
ver = getVer(str)
# contain
if module in lib_dict:
oldVer = lib_dict[module]
# compare and refresh
if ver > oldVer:
lib_dict[module] = ver
else:
lib_dict[module] = ver
def getModule(str):
'''
get 'group:module' from deps line
'''
group_module_pattern = " [a-z0-9.\-_]{1,}:[a-z0-9.\-_]{1,}:"
group_module=re.findall(group_module_pattern, str, re.IGNORECASE)[0][1:-1]
return group_module
def getVer(str):
'''
get 'version' from deps line
'''
# --- com.android.support:recyclerview-v7:28.0.0 -> androidx.recyclerview:recyclerview:1.2.1 (*) 这种目前暂时处理不好
# --- com.getkeepsafe.relinker:relinker:1.4.1 -> 1.4.5.2-master_test-32ca4f0-beta (*)
ver_override_pattern = "> [0-9a-zA-Z-_.:{[]{1,}"
ver_pattern = ":[0-9a-zA-Z-_.{[]{1,}"
if 0 != len(re.findall(ver_override_pattern, str, re.IGNORECASE)):
ver = re.findall(ver_override_pattern, str)[0][2:]
else:
ver = re.findall(ver_pattern, str, re.IGNORECASE)[-1][1:]
return ver
def isVersionCompareBigger(v1:str, v2:str):
is_bigger = parse_version(sdk_lib_dict[module]) > parse_version(app_lib_dict[module])
# Fix parse_version compare failed when maven version containing letters
return is_bigger and parse_version(v1.split('-')[0]) >= parse_version(v2.split('-')[0])
parser = argparse.ArgumentParser(formatter_class=RawTextHelpFormatter)
parser.add_argument('--path','-p',type=str,required=True,help='the root path of workspace')
parser.add_argument('--app',type=str,required=True,help='app depends file')
parser.add_argument('--sdk',type=str,required=True,help='sdk depends file')
args = parser.parse_args()
cur_path=args.path
app_deps_file=args.app
sdk_deps_file=args.sdk
app_list_file=cur_path + '/app_deps_list.log'
sdk_list_file=cur_path + '/sdk_deps_list.log'
if __name__ == '__main__':
app_lib_dict = dict()
sortLibs(app_deps_file, app_lib_dict)
f_app_list=open(app_list_file, 'w')
for module, ver in app_lib_dict.items():
f_app_list.write(f"{module}:{ver}\n")
sdk_lib_dict = dict()
sortLibs(sdk_deps_file, sdk_lib_dict)
f_sdk_list=open(sdk_list_file, 'w')
for module, ver in sdk_lib_dict.items():
f_sdk_list.write(f"{module}:{ver}\n")
with open('attention_libs.csv', newline='') as f:
reader = csv.reader(f)
attention_libs = []
for row in reader:
if row and row[0] and row[0].strip():
attention_libs.append(row[0].strip())
print("+ means this module is newly added by yours(SDK)")
print("! means your module's version is higher than app's")
print("O means host app module version is higher than yours(SDK)")
attention_lines = []
for module in sdk_lib_dict:
line=''
if not module in app_lib_dict:
line += BgColors.OK_GREEN + f"+ {module}:{sdk_lib_dict[module]}"
elif isVersionCompareBigger(sdk_lib_dict[module], app_lib_dict[module]):
line += BgColors.FAIL + f"! {module}:{sdk_lib_dict[module]} <- {app_lib_dict[module]}"
else:
line += f"O {module}:{sdk_lib_dict[module]} -> {app_lib_dict[module]}"
line += BgColors.END
if module in attention_libs:
attention_lines.append(line)
print(line)
print("\n --- Highlight libs ---")
for line in attention_lines:
print(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment