diffconfig 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. -EXT2_FS_XIP n
  25. CRAMFS n -> y
  26. EXT2_FS y -> n
  27. LOG_BUF_SHIFT 14 -> 16
  28. PRINTK_TIME n -> y
  29. """
  30. sys.exit(0)
  31. # returns a dictionary of name/value pairs for config items in the file
  32. def readconfig(config_file):
  33. d = {}
  34. for line in config_file:
  35. line = line[:-1]
  36. if line[:7] == "CONFIG_":
  37. name, val = line[7:].split("=", 1)
  38. d[name] = val
  39. if line[-11:] == " is not set":
  40. d[line[9:-11]] = "n"
  41. return d
  42. def print_config(op, config, value, new_value):
  43. global merge_style
  44. if merge_style:
  45. if new_value:
  46. if new_value=="n":
  47. print "# CONFIG_%s is not set" % config
  48. else:
  49. print "CONFIG_%s=%s" % (config, new_value)
  50. else:
  51. if op=="-":
  52. print "-%s %s" % (config, value)
  53. elif op=="+":
  54. print "+%s %s" % (config, new_value)
  55. else:
  56. print " %s %s -> %s" % (config, value, new_value)
  57. def main():
  58. global merge_style
  59. # parse command line args
  60. if ("-h" in sys.argv or "--help" in sys.argv):
  61. usage()
  62. merge_style = 0
  63. if "-m" in sys.argv:
  64. merge_style = 1
  65. sys.argv.remove("-m")
  66. argc = len(sys.argv)
  67. if not (argc==1 or argc == 3):
  68. print "Error: incorrect number of arguments or unrecognized option"
  69. usage()
  70. if argc == 1:
  71. # if no filenames given, assume .config and .config.old
  72. build_dir=""
  73. if os.environ.has_key("KBUILD_OUTPUT"):
  74. build_dir = os.environ["KBUILD_OUTPUT"]+"/"
  75. configa_filename = build_dir + ".config.old"
  76. configb_filename = build_dir + ".config"
  77. else:
  78. configa_filename = sys.argv[1]
  79. configb_filename = sys.argv[2]
  80. a = readconfig(file(configa_filename))
  81. b = readconfig(file(configb_filename))
  82. # print items in a but not b (accumulate, sort and print)
  83. old = []
  84. for config in a:
  85. if config not in b:
  86. old.append(config)
  87. old.sort()
  88. for config in old:
  89. print_config("-", config, a[config], None)
  90. del a[config]
  91. # print items that changed (accumulate, sort, and print)
  92. changed = []
  93. for config in a:
  94. if a[config] != b[config]:
  95. changed.append(config)
  96. else:
  97. del b[config]
  98. changed.sort()
  99. for config in changed:
  100. print_config("->", config, a[config], b[config])
  101. del b[config]
  102. # now print items in b but not in a
  103. # (items from b that were in a were removed above)
  104. new = b.keys()
  105. new.sort()
  106. for config in new:
  107. print_config("+", config, None, b[config])
  108. main()