From nobody Mon Jun 22 19:22:00 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D499FC433EF for ; Fri, 18 Mar 2022 07:55:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233386AbiCRH40 (ORCPT ); Fri, 18 Mar 2022 03:56:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231319AbiCRH4Y (ORCPT ); Fri, 18 Mar 2022 03:56:24 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81E501770B8 for ; Fri, 18 Mar 2022 00:55:04 -0700 (PDT) Received: from dggpemm500022.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4KKbqK2NnmzfZ4G for ; Fri, 18 Mar 2022 15:53:33 +0800 (CST) Received: from dggpemm500001.china.huawei.com (7.185.36.107) by dggpemm500022.china.huawei.com (7.185.36.162) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Fri, 18 Mar 2022 15:55:02 +0800 Received: from huawei.com (10.67.174.169) by dggpemm500001.china.huawei.com (7.185.36.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Fri, 18 Mar 2022 15:55:02 +0800 From: Chen Lifu To: , , CC: , , , , , , , , , , , , , , Subject: [PATCH -next] scripts: add compare-config utility Date: Fri, 18 Mar 2022 15:54:21 +0800 Message-ID: <20220318075421.606533-1-chenlifu@huawei.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.67.174.169] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500001.china.huawei.com (7.185.36.107) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This is an alternative utility to compare two Linux .config files. Unlike existing utilities such as "diffconfig" in the Linux kernel tree, it prints detailed results in table style. It is useful sometimes, e.g. for those who need to analyze .config files through tables. Signed-off-by: Chen Lifu --- scripts/compare-config | 199 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100755 scripts/compare-config diff --git a/scripts/compare-config b/scripts/compare-config new file mode 100755 index 000000000000..6da7c844b89c --- /dev/null +++ b/scripts/compare-config @@ -0,0 +1,199 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 +# +# An utility to compare two .config files and print the results in table s= tyle. +# + +import sys +import argparse +import traceback + +def args_parser(): + comment =3D ("An utility to compare two .config files and " + "print the results in table style.") + parser =3D argparse.ArgumentParser(description =3D comment, + formatter_class =3D + argparse.RawTextHelpFormatter) + parser.add_argument(dest =3D "old_file", nargs =3D "?", + metavar =3D "old-file", + default =3D ".config.old", + help =3D "specify old .config file " + "(default: .config.old)") + parser.add_argument(dest =3D "new_file", nargs =3D "?", + metavar =3D "new-file", + default =3D ".config", + help =3D "specify new .config file " + "(default: .config)") + parser.add_argument("-S", dest =3D "S", action =3D "store_true", + help =3D "print configs that exist in both files " + "and are equal") + parser.add_argument("-C", dest =3D "C", action =3D "store_true", + help =3D "print configs that exist in both files " + "but are not equal") + parser.add_argument("-O", dest =3D "O", action =3D "store_true", + help =3D "print configs that only exist in old-fil= e") + parser.add_argument("-N", dest =3D "N", action =3D "store_true", + help =3D "print configs that only exist in new-fil= e") + parser.add_argument("-y", dest =3D "y", action =3D "store_true", + help =3D "print configs that are y") + parser.add_argument("-n", dest =3D "n", action =3D "store_true", + help =3D "print configs that are n (not set)") + parser.add_argument("-m", dest =3D "m", action =3D "store_true", + help =3D "print configs that are m") + parser.add_argument("-v", dest =3D "v", action =3D "store_true", + help =3D "print configs that are " + "string/hex/int value") + parser.add_argument("--old", dest =3D "old", action =3D "store_true", + help =3D "filter configs base on old-file") + parser.add_argument("--new", dest =3D "new", action =3D "store_true", + help =3D "filter configs base on new-file") + return parser + +def usage(): + args_parser().parse_args(["-h"]) + +def parse_args(): + args =3D args_parser().parse_args() + setattr(args, "doptions", diff_options(args)) + setattr(args, "voptions", value_options(args)) + old =3D args.old or not args.new + new =3D args.new or not args.old + args.old =3D old + args.new =3D new + return args + +def diff_options(args): + doptions =3D [] + if args.S: doptions.append("S") + if args.C: doptions.append("C") + if args.O: doptions.append("O") + if args.N: doptions.append("N") + if len(doptions) =3D=3D 0: + doptions =3D ["S", "C", "O", "N"] + return doptions + +def value_options(args): + voptions =3D set() + if args.y: voptions.add("y") + if args.n: voptions.add("n") + if args.m: voptions.add("m") + if args.v: voptions.add("v") + if len(voptions) =3D=3D 0: + voptions =3D {"y", "n", "m", "v"} + return voptions + +def test_value(val, voptions): + if val is None: return False + if val in voptions: return True + return (not val in {"y", "n", "m"}) and ("v" in voptions) + +def format_exception(): + es =3D "" + exc_type, exc_value, exc_traceback =3D sys.exc_info() + exc_str =3D traceback.format_exception(exc_type, exc_value, exc_traceb= ack) + for s in exc_str: + es +=3D s + return es + +def read_line(line): + prefix =3D "CONFIG_" + line =3D line.strip() + if line.startswith(prefix): + if line.find("=3D") =3D=3D -1: return None, None + name, val =3D line.split("=3D", 1) + return name.strip(), val.strip() + if line.endswith(" is not set"): + beg =3D line.find(prefix) + if beg =3D=3D -1: return None, None + name, val =3D line[beg:].split(" ", 1) + return name, "n" + return None, None + +def read_file(filename): + configs =3D {} + with open(filename, "r", encoding =3D "utf-8") as f: + for line in f: + name, val =3D read_line(line) + if not name is None: configs[name] =3D val + return configs + +def compare_files(old_file, new_file): + result =3D {"S": {}, "C": {}, "O": {}, "N": {}} + try: + old_configs =3D read_file(old_file) + new_configs =3D read_file(new_file) + while len(old_configs) > 0: + name, old_val =3D old_configs.popitem() + new_val =3D new_configs.pop(name, None) + if new_val is None: + result["O"][name] =3D (old_val, None) + elif old_val =3D=3D new_val: + result["S"][name] =3D (old_val, new_val) + else: + result["C"][name] =3D (old_val, new_val) + while len(new_configs) > 0: + name, new_val =3D new_configs.popitem() + result["N"][name] =3D (None, new_val) + except: + print(format_exception()) + usage() + return result + +def filter_output(result, args): + output =3D {"S": {}, "C": {}, "O": {}, "N": {}} + for opt in args.doptions: + for name, val in result[opt].items(): + if (args.old and test_value(val[0], args.voptions) or + args.new and test_value(val[1], args.voptions)): + old_val =3D "-" if val[0] is None else val[0] + new_val =3D "-" if val[1] is None else val[1] + output[opt][name] =3D (old_val, new_val) + return output + +def print_table(output, args): + name_max_len =3D 8 + old_max_len =3D 8 + new_max_len =3D 8 + name_list =3D sum([list(output[opt].keys()) for opt in args.doptions],= []) + if len(name_list) > 0: + name_max_len =3D len(max(name_list, key =3D len)) + val_list =3D sum([list(output[opt].values()) for opt in args.doptions]= , []) + if len(val_list) > 0: + old_max_len =3D len(max([val[0] for val in val_list], key =3D len)) + new_max_len =3D len(max([val[1] for val in val_list], key =3D len)) + diff_max_len =3D len(max([diff_types[opt] for opt in args.doptions], k= ey =3D len)) + header =3D ["NAME", "DIFF", "OLD", "NEW"] + # table row format + row =3D ("{{:{}}}\t{{:{}}}\t{{:{}}}\t{{:{}}}" + .format(min(max(name_max_len, len(header[0])), 40), + min(max(diff_max_len, len(header[1])), 40), + min(max(old_max_len, len(header[2])), 40), + min(max(new_max_len, len(header[3])), 40))) + print(row.format(header[0], header[1], header[2], header[3])) + for opt in args.doptions: + for name, val in sorted(output[opt].items()): + print(row.format(name, diff_types[opt], val[0], val[1])) + +def print_summary(output, args): + diff_max_len =3D len(max([diff_types[opt] for opt in args.doptions], k= ey =3D len)) + # summary line format + line =3D "{{:{}}}: {{}}".format(max(diff_max_len, 8)) + print("\nSummary:") + print(line.format("Old-file", args.old_file)) + print(line.format("New-file", args.new_file)) + total =3D 0 + for opt in args.doptions: + count =3D len(output[opt]) + print(line.format(diff_types[opt], count)) + total +=3D count + print(line.format("Total", total)) + +def print_result(result, args): + output =3D filter_output(result, args) + print_table(output, args) + print_summary(output, args) + +diff_types =3D {"S": "Same", "C": "Changed", "O": "Old-only", "N": "New-on= ly"} +args =3D parse_args() +result =3D compare_files(args.old_file, args.new_file) +print_result(result, args) --=20 2.35.1