diffconfig 4.0 KB

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