check-config.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #! /usr/bin/env python
  2. # Copyright (c) 2015, The Linux Foundation. All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are met:
  6. # * Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # * Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. # * Neither the name of The Linux Foundation nor
  12. # the names of its contributors may be used to endorse or promote
  13. # products derived from this software without specific prior written
  14. # permission.
  15. #
  16. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. # IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. # NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  20. # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  23. # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  24. # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  25. # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. """
  28. Android kernel configuration validator.
  29. The Android kernel reference trees contain some config stubs of
  30. configuration options that are required for Android to function
  31. correctly, and additional ones that are recommended.
  32. This script can help compare these base configs with the ".config"
  33. output of the compiler to determine if the proper configs are defined.
  34. """
  35. from collections import namedtuple
  36. from optparse import OptionParser
  37. import re
  38. import sys
  39. version = "check-config.py, version 0.0.1"
  40. req_re = re.compile(r'''^CONFIG_(.*)=(.*)$''')
  41. forb_re = re.compile(r'''^# CONFIG_(.*) is not set$''')
  42. comment_re = re.compile(r'''^(#.*|)$''')
  43. Enabled = namedtuple('Enabled', ['name', 'value'])
  44. Disabled = namedtuple('Disabled', ['name'])
  45. def walk_config(name):
  46. with open(name, 'r') as fd:
  47. for line in fd:
  48. line = line.rstrip()
  49. m = req_re.match(line)
  50. if m:
  51. yield Enabled(m.group(1), m.group(2))
  52. continue
  53. m = forb_re.match(line)
  54. if m:
  55. yield Disabled(m.group(1))
  56. continue
  57. m = comment_re.match(line)
  58. if m:
  59. continue
  60. print "WARNING: Unknown .config line: ", line
  61. class Checker():
  62. def __init__(self):
  63. self.required = {}
  64. self.exempted = set()
  65. self.forbidden = set()
  66. def add_required(self, fname):
  67. for ent in walk_config(fname):
  68. if type(ent) is Enabled:
  69. self.required[ent.name] = ent.value
  70. elif type(ent) is Disabled:
  71. if ent.name in self.required:
  72. del self.required[ent.name]
  73. self.forbidden.add(ent.name)
  74. def add_exempted(self, fname):
  75. with open(fname, 'r') as fd:
  76. for line in fd:
  77. line = line.rstrip()
  78. self.exempted.add(line)
  79. def check(self, path):
  80. failure = False
  81. # Don't run this for mdm targets
  82. if re.search('mdm', path):
  83. print "Not applicable to mdm targets... bypassing"
  84. else:
  85. for ent in walk_config(path):
  86. # Go to the next iteration if this config is exempt
  87. if ent.name in self.exempted:
  88. continue
  89. if type(ent) is Enabled:
  90. if ent.name in self.forbidden:
  91. print "error: Config should not be present: %s" %ent.name
  92. failure = True
  93. if ent.name in self.required and ent.value != self.required[ent.name]:
  94. print "error: Config has wrong value: %s %s expecting: %s" \
  95. %(ent.name, ent.value, self.required[ent.name])
  96. failure = True
  97. elif type(ent) is Disabled:
  98. if ent.name in self.required:
  99. print "error: Config should be present, but is disabled: %s" %ent.name
  100. failure = True
  101. if failure:
  102. sys.exit(1)
  103. def main():
  104. usage = """%prog [options] path/to/.config"""
  105. parser = OptionParser(usage=usage, version=version)
  106. parser.add_option('-r', '--required', dest="required",
  107. action="append")
  108. parser.add_option('-e', '--exempted', dest="exempted",
  109. action="append")
  110. (options, args) = parser.parse_args()
  111. if len(args) != 1:
  112. parser.error("Expecting a single path argument to .config")
  113. elif options.required is None or options.exempted is None:
  114. parser.error("Expecting a file containing required configurations")
  115. ch = Checker()
  116. for r in options.required:
  117. ch.add_required(r)
  118. for e in options.exempted:
  119. ch.add_exempted(e)
  120. ch.check(args[0])
  121. if __name__ == '__main__':
  122. main()