Back to home page

LXR

 
 

    


0001 #!/usr/bin/python
0002 #
0003 # diffconfig - a tool to compare .config files.
0004 #
0005 # originally written in 2006 by Matt Mackall
0006 #  (at least, this was in his bloatwatch source code)
0007 # last worked on 2008 by Tim Bird
0008 #
0009 
0010 import sys, os
0011 
0012 def usage():
0013     print("""Usage: diffconfig [-h] [-m] [<config1> <config2>]
0014 
0015 Diffconfig is a simple utility for comparing two .config files.
0016 Using standard diff to compare .config files often includes extraneous and
0017 distracting information.  This utility produces sorted output with only the
0018 changes in configuration values between the two files.
0019 
0020 Added and removed items are shown with a leading plus or minus, respectively.
0021 Changed items show the old and new values on a single line.
0022 
0023 If -m is specified, then output will be in "merge" style, which has the
0024 changed and new values in kernel config option format.
0025 
0026 If no config files are specified, .config and .config.old are used.
0027 
0028 Example usage:
0029  $ diffconfig .config config-with-some-changes
0030 -EXT2_FS_XATTR  n
0031  CRAMFS  n -> y
0032  EXT2_FS  y -> n
0033  LOG_BUF_SHIFT  14 -> 16
0034  PRINTK_TIME  n -> y
0035 """)
0036     sys.exit(0)
0037 
0038 # returns a dictionary of name/value pairs for config items in the file
0039 def readconfig(config_file):
0040     d = {}
0041     for line in config_file:
0042         line = line[:-1]
0043         if line[:7] == "CONFIG_":
0044             name, val = line[7:].split("=", 1)
0045             d[name] = val
0046         if line[-11:] == " is not set":
0047             d[line[9:-11]] = "n"
0048     return d
0049 
0050 def print_config(op, config, value, new_value):
0051     global merge_style
0052 
0053     if merge_style:
0054         if new_value:
0055             if new_value=="n":
0056                 print("# CONFIG_%s is not set" % config)
0057             else:
0058                 print("CONFIG_%s=%s" % (config, new_value))
0059     else:
0060         if op=="-":
0061             print("-%s %s" % (config, value))
0062         elif op=="+":
0063             print("+%s %s" % (config, new_value))
0064         else:
0065             print(" %s %s -> %s" % (config, value, new_value))
0066 
0067 def main():
0068     global merge_style
0069 
0070     # parse command line args
0071     if ("-h" in sys.argv or "--help" in sys.argv):
0072         usage()
0073 
0074     merge_style = 0
0075     if "-m" in sys.argv:
0076         merge_style = 1
0077         sys.argv.remove("-m")
0078 
0079     argc = len(sys.argv)
0080     if not (argc==1 or argc == 3):
0081         print("Error: incorrect number of arguments or unrecognized option")
0082         usage()
0083 
0084     if argc == 1:
0085         # if no filenames given, assume .config and .config.old
0086         build_dir=""
0087         if "KBUILD_OUTPUT" in os.environ:
0088             build_dir = os.environ["KBUILD_OUTPUT"]+"/"
0089         configa_filename = build_dir + ".config.old"
0090         configb_filename = build_dir + ".config"
0091     else:
0092         configa_filename = sys.argv[1]
0093         configb_filename = sys.argv[2]
0094 
0095     try:
0096         a = readconfig(open(configa_filename))
0097         b = readconfig(open(configb_filename))
0098     except (IOError):
0099         e = sys.exc_info()[1]
0100         print("I/O error[%s]: %s\n" % (e.args[0],e.args[1]))
0101         usage()
0102 
0103     # print items in a but not b (accumulate, sort and print)
0104     old = []
0105     for config in a:
0106         if config not in b:
0107             old.append(config)
0108     old.sort()
0109     for config in old:
0110         print_config("-", config, a[config], None)
0111         del a[config]
0112 
0113     # print items that changed (accumulate, sort, and print)
0114     changed = []
0115     for config in a:
0116         if a[config] != b[config]:
0117             changed.append(config)
0118         else:
0119             del b[config]
0120     changed.sort()
0121     for config in changed:
0122         print_config("->", config, a[config], b[config])
0123         del b[config]
0124 
0125     # now print items in b but not in a
0126     # (items from b that were in a were removed above)
0127     new = sorted(b.keys())
0128     for config in new:
0129         print_config("+", config, None, b[config])
0130 
0131 main()