From 7b3d2f37c752ea178c565a29c557d917029ca768 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=BF=94=E5=AE=87-wxy?= Date: Fri, 30 Dec 2022 08:37:41 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20Extended=20json=20file=20display=20info?= =?UTF-8?q?.=201=E3=80=81=E4=BF=AE=E6=94=B9=E7=B3=BB=E7=BB=9F=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E8=8E=B7=E5=8F=96=E7=AD=96=E7=95=A5=EF=BC=9B=202?= =?UTF-8?q?=E3=80=81=E5=A2=9E=E5=8A=A0=E5=90=91json=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=86=99=E5=85=A5=E5=85=B1=E4=BA=AB=E5=BA=93=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=EF=BC=9B=203=E3=80=81=E5=A2=9E=E5=8A=A0=E5=90=91json=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=86=99=E5=85=A5=E5=BA=93=E5=8C=85=E7=BA=A7=E5=88=AB?= =?UTF-8?q?=EF=BC=9B=204=E3=80=81=E5=A2=9E=E5=8A=A0=E5=90=91json=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=86=99=E5=85=A5=E5=BA=93=E5=8C=85=E7=89=88=E6=9C=AC?= =?UTF-8?q?=EF=BC=9B=205=E3=80=81=E5=A2=9E=E5=8A=A0=E5=90=91json=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=86=99=E5=85=A5=E5=BA=93=E5=8C=85=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E7=89=88=E6=9C=AC=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王翔宇-wxy --- LibChecker/lib_checker.py | 111 +++++++++++++++++++++++++++++++++----- 1 file changed, 98 insertions(+), 13 deletions(-) diff --git a/LibChecker/lib_checker.py b/LibChecker/lib_checker.py index c75f76e..f01c072 100755 --- a/LibChecker/lib_checker.py +++ b/LibChecker/lib_checker.py @@ -89,6 +89,13 @@ def libchecker_over_jobs(): # os.system("cp Logs/libchecker-tmp.log Outputs/libchecker-output.txt") os.rename("Logs/libchecker-tmp.log", log_file_name) ## 0.3 platform info +def get_env_info(): + print("系统信息:") + str1 = os.popen("cat /etc/os-release").read() + str2 = str1.split("\n") + for s1 in str2: + print("\t", s1) + def get_platform_info(): # global g_inputstrategy = args.strategy # global g_inputlevel = args.level @@ -301,6 +308,9 @@ def libchecker_checking_loop(): global g_test_dict global g_pkgflag_dict + l_dict_to_json = {} + l_pkgresult_to_json = {} + del g_jsonfile_dict['libs']['category']['##章节名'] print("") print("开始检查: ",time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()),"") @@ -351,12 +361,15 @@ def libchecker_checking_loop(): for key4 in g_storejsondict: g_test_dict.update({key4 : "wwww"}) - g_genresults_to_json.update({ key4 : "gen" }) + l_dict_to_json={'Level': 'gen', 'Shared library':'gen', 'Required version': 'gen', 'Binary package':'gen'} + g_genresults_to_json.update({ key4 : l_dict_to_json }) with open("Outputs/libchecker-output.json","w") as f: json.dump(g_genresults_to_json,f) for last_key in g_storejsondict: + l_pkgresult_to_json.clear() + if (g_storejsondict[last_key]['necessity'][g_ostype]['level'] == "L1"): g_counter_flags['pkg_counter']['total']['l1'] += 1 if (g_storejsondict[last_key]['necessity'][g_ostype]['level'] == "L2"): @@ -389,27 +402,72 @@ def libchecker_checking_loop(): temp_libsoname = lib_result.split('/')[-1] if (lib_result == "not found"): print("\t\t\t\t\t检测结果 -> 未检测到存在") - g_subresults_to_json[list1_item] = "not found" + g_subresults_to_json[list1_item] = "not found","-" g_counter_flags['lib_counter']['failed'] += 1 else: print("\t\t\t\t\t检测结果 -> ", compare_library_version(temp_libsoname, str(list1_item))) - if (compare_library_version(temp_libsoname, list1_item == "equal" )): + if (compare_library_version(temp_libsoname, str(list1_item)) == "equal" ): g_counter_flags['lib_counter']['passed'] += 1 - g_subresults_to_json[list1_item] = "compatible" - elif (compare_library_version(temp_libsoname, list1_item == "smaller" )): + g_subresults_to_json[list1_item] = "compatible",g_lib_location_path + elif (compare_library_version(temp_libsoname, str(list1_item)) == "smaller" ): g_counter_flags['lib_counter']['failed'] += 1 g_subresults_to_json[list1_item] = "incompatible" else: g_counter_flags['lib_counter']['warning'] += 1 g_subresults_to_json[list1_item] = "compatible bigger" - g_genresults_to_json.update({last_key:g_subresults_to_json}) + #Traverse the binary package of the source package + if (g_inputpkgmngr == "yum-rpm"): + binary_list = get_rpmpkg_from_srcpkg(last_key) + else: + binary_list = get_debpkg_from_srcpkg(last_key) + + for binary_name in binary_list: + if (g_inputpkgmngr == "yum-rpm"): + pkg_install_status = os.system('rpm -qi %s' %(binary_name)) + if (pkg_install_status == 0): + ver_required = g_storejsondict[last_key]['version'][g_inputostype] #获取要求的库包版本 + ver_local = os.popen('rpm -qi %s | grep "Version\|Release" | awk -F" " \'{print $3}\' | sed \':label;N;s/\\n/-/;t label\'' %(binary_name)).read().rstrip('\n') #获取本地库包版本 + if (get_rpmpkg_ver_contrast(ver_local, ver_required) == "compatible"): + l_pkgresult_to_json[binary_name] = "compatible", ver_local + elif (get_rpmpkg_ver_contrast(ver_local, ver_required) == "incompatible"): + l_pkgresult_to_json[binary_name] = "incompatible", ver_local + else: + pkg_install_status = os.popen('dpkg -l %s 2>/dev/null| grep %s | gawk -F" " \'{print $1}\' | head -n 1' %(str(binary_name), str(binary_name))).read().rstrip('\n') + if (pkg_install_status == "ii"): + ver_required = g_storejsondict[last_key]['version'][g_inputostype] #获取要求的库包版本 + ver_local = os.popen('dpkg -l %s 2>/dev/null| grep %s | gawk -F" " \'{print $3}\' | head -n 1' %(binary_name, binary_name)).read().rstrip('\n') #获取本地库包版本 + if (get_debpkg_ver_contrast(ver_local, ver_required) == "compatible"): + l_pkgresult_to_json[binary_name] = "compatible", ver_local + elif (get_debpkg_ver_contrast(ver_local, ver_required) == "incompatible"): + l_pkgresult_to_json[binary_name] = "incompatible", ver_local + else: + continue + #向json文件写入库包级别 + with open("Outputs/libchecker-output.json", 'r') as fr: + json_level = json.load(fr) + json_level[last_key]['Level'] = g_storejsondict[last_key]['necessity'][g_inputostype]['level'] + with open("Outputs/libchecker-output.json", 'w+') as fw: + json.dump(json_level,fw,ensure_ascii=False,indent=4) + #向json文件写入库包需求版本 + with open("Outputs/libchecker-output.json", 'r') as fr: + json_required_ver = json.load(fr) + json_required_ver[last_key]['Required version'] = g_storejsondict[last_key]['version'][g_inputostype] + with open("Outputs/libchecker-output.json", 'w+') as fw: + json.dump(json_required_ver,fw,ensure_ascii=False,indent=4) + #向json文件写入共享库兼容信息 with open("Outputs/libchecker-output.json", 'r') as fr: - json_all = json.load(fr) - json_all[last_key]=g_subresults_to_json + json_so = json.load(fr) + json_so[last_key]['Shared library'] = g_subresults_to_json with open("Outputs/libchecker-output.json", 'w+') as fw: - json.dump(json_all,fw,ensure_ascii=False,indent=4) + json.dump(json_so,fw,ensure_ascii=False,indent=4) + #向json文件写入库包本地版本 + with open("Outputs/libchecker-output.json", 'r') as fr: + json_local_ver = json.load(fr) + json_local_ver[last_key]['Binary package'] = l_pkgresult_to_json + with open("Outputs/libchecker-output.json", 'w+') as fw: + json.dump(json_local_ver,fw,ensure_ascii=False,indent=4) print("=============================================================================================================") @@ -433,9 +491,22 @@ def libchecker_checking_loop(): print("\t\t报错:", g_counter_flags['lib_counter']['failed']) print("=============================================================================================================") - #print(g_genresults_to_json) - #print(g_test_list) - # print(g_genresults_to_json) +def get_debpkg_ver_contrast(ver_local, ver_required): + # --compare-version ver_local op ver_required + # op: lt le eq ne ge gt + # sn: < <= == != >= > + compare_result = os.system('dpkg --compare-versions %s ge %s' %(str(ver_local), str(ver_required))) + if(compare_result == 0): + return "compatible" + else: + return "incompatible" + +def get_rpmpkg_ver_contrast(ver_local, ver_required): + # compare ver_local op ver_required + if ver_local < ver_required: + return "incompatible" + else: + return "compatible" def read_pkginfo_from_stdjson(json_file_path): # this function read package info from json file @@ -738,12 +809,26 @@ def get_debpkg_from_srcpkg(src_pkgname): # @ # return: # @ debpkgs - print("Enter function: get_debpkg_from_srcpkg(%s)" %(src_pkgname)) p_debpkgs = os.popen('apt-cache showsrc %s | grep Binary | cut -d '"\:"' -f 2- | cut -d '"\ "' -f 2- ' %(src_pkgname)) debpkgs = p_debpkgs.read().split("\n")[0].split(", ") p_debpkgs.close() return debpkgs + +def get_rpmpkg_from_srcpkg(src_pkgname): + # this function for get rpm pacgakes from package name in current os + # input: + # @ src_pkgname + # output: + # @ + # return: + # @ debpkgs + + p_rpmpkgs = os.popen('dnf info | grep -B 5 -E "%s.*.src.rpm" | grep "名称" | awk -F" " \'{ print $3 }\' | sort -n | uniq | sed \':label;N;s/\\n/ /;t label\'' %(src_pkgname)) + debpkgs = p_rpmpkgs.read().split("\n")[0].split(" ") + p_rpmpkgs.close() + + return debpkgs ## 3.3 get libraries info ################################################################################ -- Gitee