diffconfig 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/python
  2. #
  3. # diffconfig - a tool to compare .config files.
  4. #
  5. # originally written in 2006 by Matt Mackall
  6. # (at least, this was in his bloatwatch source code)
  7. # last worked on 2008 by Tim Bird
  8. #
  9. import sys, os
  10. def usage():
  11. print("""Usage: diffconfig [-h] [-m] [<config1> <config2>]
  12. Diffconfig is a simple utility for comparing two .config files.
  13. Using standard diff to compare .config files often includes extraneous and
  14. distracting information. This utility produces sorted output with only the
  15. changes in configuration values between the two files.
  16. Added and removed items are shown with a leading plus or minus, respectively.
  17. Changed items show the old and new values on a single line.
  18. If -m is specified, then output will be in "merge" style, which has the
  19. changed and new values in kernel config option format.
  20. If no config files are specified, .config and .config.old are used.
  21. Example usage:
  22. $ diffconfig .config config-with-some-changes
  23. -EXT2_FS_XATTR n
  24. CRAMFS n -> y
  25. EXT2_FS y -> n
  26. LOG_BUF_SHIFT 14 -> 16
  27. PRINTK_TIME n -> y
  28. """)
  29. sys.exit(0)
  30. # returns a dictionary of name/value pairs for config items in the file
  31. def readconfig(config_file):
  32. d = {}
  33. for line in config_file:
  34. line = line[:-1]
  35. if line[:7] == "CONFIG_":
  36. name, val = line[7:].split("=", 1)
  37. d[name] = val
  38. if line[-11:] == " is not set":
  39. d[line[9:-11]] = "n"
  40. return d
  41. def print_config(op, config, value, new_value):
  42. global merge_style
  43. if merge_style:
  44. if new_value:
  45. if new_value=="n":
  46. print("# CONFIG_%s is not set" % config)
  47. else:
  48. print("CONFIG_%s=%s" % (config, new_value))
  49. else:
  50. if op=="-":
  51. print("-%s %s" % (config, value))
  52. elif op=="+":
  53. print("+%s %s" % (config, new_value))
  54. else:
  55. print(" %s %s -> %s" % (config, value, new_value))
  56. def main():
  57. global merge_style
  58. # parse command line args
  59. if ("-h" in sys.argv or "--help" in sys.argv):
  60. usage()
  61. merge_style = 0
  62. if "-m" in sys.argv:
  63. merge_style = 1
  64. sys.argv.remove("-m")
  65. argc = len(sys.argv)
  66. if not (argc==1 or argc == 3):
  67. print("Error: incorrect number of arguments or unrecognized option")
  68. usage()
  69. if argc == 1:
  70. # if no filenames given, assume .config and .config.old
  71. build_dir=""
  72. if "KBUILD_OUTPUT" in os.environ:
  73. build_dir = os.environ["KBUILD_OUTPUT"]+"/"
  74. configa_filename = build_dir + ".config.old"
  75. configb_filename = build_dir + ".config"
  76. else:
  77. configa_filename = sys.argv[1]
  78. configb_filename = sys.argv[2]
  79. try:
  80. a = readconfig(open(configa_filename))
  81. b = readconfig(open(configb_filename))
  82. except (IOError):
  83. e = sys.exc_info()[1]
  84. print("I/O error[%s]: %s\n" % (e.args[0],e.args[1]))
  85. usage()
  86. # print items in a but not b (accumulate, sort and print)
  87. old = []
  88. for config in a:
  89. if config not in b:
  90. old.append(config)
  91. old.sort()
  92. for config in old:
  93. print_config("-", config, a[config], None)
  94. del a[config]
  95. # print items that changed (accumulate, sort, and print)
  96. changed = []
  97. for config in a:
  98. if a[config] != b[config]:
  99. changed.append(config)
  100. else:
  101. del b[config]
  102. changed.sort()
  103. for config in changed:
  104. print_config("->", config, a[config], b[config])
  105. del b[config]
  106. # now print items in b but not in a
  107. # (items from b that were in a were removed above)
  108. new = sorted(b.keys())
  109. for config in new:
  110. print_config("+", config, None, b[config])
  111. main()