merge.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/python3
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Author: Clark Williams <[email protected]>
  4. # Copyright (C) 2022 Red Hat, Inc.
  5. #
  6. # merge.py - a direct replacement for merge.pl in the redhat/configs directory
  7. #
  8. # invocation: python merge.py overrides baseconfig [arch]
  9. #
  10. # This script merges two kernel configuration files, an override file and a
  11. # base config file and writes the results to stdout.
  12. #
  13. # The script reads the overrides into a dictionary, then reads the baseconfig
  14. # file, looking for overrides and replacing any found, then printing the result
  15. # to stdout. Finally any remaining (new) configs in the override are appended to the
  16. # end of the output
  17. import sys
  18. import re
  19. import os.path
  20. def usage(msg):
  21. '''print a usage message and exit'''
  22. sys.stderr.write(msg + "\n")
  23. sys.stderr.write("usage: merge.py overrides baseconfig [arch]\n")
  24. sys.exit(1)
  25. isset = re.compile(r'^(CONFIG_\w+)=')
  26. notset = re.compile(r'^#\s+(CONFIG_\w+)\s+is not set')
  27. # search an input line for a config (set or notset) pattern
  28. # if we get a match return the config that is being changed
  29. def find_config(line):
  30. '''find a configuration line in the input and return the config name'''
  31. m = isset.match(line)
  32. if (m is not None):
  33. return m.group(1)
  34. m = notset.match(line)
  35. if (m is not None):
  36. return m.group(1)
  37. return None
  38. #########################################################
  39. if len(sys.argv) < 3:
  40. usage("must have two input files")
  41. override_file = sys.argv[1]
  42. baseconfig_file = sys.argv[2]
  43. if not os.path.exists(override_file):
  44. usage(f"overrides config file {override_file:s} does not exist!")
  45. if not os.path.exists(baseconfig_file):
  46. usage(f"base configs file {baseconfig_file:s} does not exist")
  47. if len(sys.argv) == 4:
  48. print(f"# {sys.argv[3]:s}")
  49. # read each line of the override file and store any configuration values
  50. # in the overrides dictionary, keyed by the configuration name.
  51. overrides = {}
  52. with open(override_file, "rt", encoding="utf-8") as f:
  53. for line in [l.strip() for l in f.readlines()]:
  54. c = find_config(line)
  55. if c and c not in overrides:
  56. overrides[c] = line
  57. # now read and print the base config, checking each line
  58. # that defines a config value and printing the override if
  59. # it exists
  60. with open(baseconfig_file, "rt", encoding="utf-8") as f:
  61. for line in [ l.strip() for l in f.readlines() ]:
  62. c = find_config(line)
  63. if c and c in overrides:
  64. print(overrides[c])
  65. del overrides[c]
  66. else:
  67. print(line)
  68. # print out the remaining configs (new values)
  69. # from the overrides file
  70. for v in overrides.values():
  71. print (v)
  72. sys.exit(0)