42d1a9
#!/usr/bin/python3
42d1a9
# SPDX-License-Identifier: GPL-2.0
42d1a9
# Author: Clark Williams <williams@redhat.com>
42d1a9
# Copyright (C) 2022 Red Hat, Inc.
42d1a9
#
42d1a9
# merge.py - a direct replacement for merge.pl in the redhat/configs directory
42d1a9
#
42d1a9
# invocation:   python merge.py overrides baseconfig [arch]
42d1a9
#
42d1a9
# This script merges two kernel configuration files, an override file and a
42d1a9
# base config file and writes the results to stdout.
42d1a9
#
42d1a9
# The script reads the overrides into a dictionary, then reads the baseconfig
42d1a9
# file, looking for overrides and replacing any found, then printing the result
42d1a9
# to stdout. Finally any remaining (new) configs in the override are appended to the
42d1a9
# end of the output
42d1a9
42d1a9
import sys
42d1a9
import re
42d1a9
import os.path
42d1a9
42d1a9
def usage(msg):
42d1a9
    '''print a usage message and exit'''
42d1a9
    sys.stderr.write(msg + "\n")
42d1a9
    sys.stderr.write("usage: merge.py overrides baseconfig [arch]\n")
42d1a9
    sys.exit(1)
42d1a9
42d1a9
isset = re.compile(r'^(CONFIG_\w+)=')
42d1a9
notset = re.compile(r'^#\s+(CONFIG_\w+)\s+is not set')
42d1a9
42d1a9
# search an input line for a config (set or notset) pattern
42d1a9
# if we get a match return the config that is being changed
42d1a9
def find_config(line):
42d1a9
    '''find a configuration line in the input and return the config name'''
42d1a9
    m = isset.match(line)
42d1a9
    if (m is not None):
42d1a9
        return m.group(1)
42d1a9
42d1a9
    m = notset.match(line)
42d1a9
    if (m is not None):
42d1a9
        return m.group(1)
42d1a9
42d1a9
    return None
42d1a9
42d1a9
#########################################################
42d1a9
42d1a9
if len(sys.argv) < 3:
42d1a9
    usage("must have two input files")
42d1a9
42d1a9
override_file = sys.argv[1]
42d1a9
baseconfig_file = sys.argv[2]
42d1a9
42d1a9
if not os.path.exists(override_file):
42d1a9
    usage(f"overrides config file {override_file:s} does not exist!")
42d1a9
42d1a9
if not os.path.exists(baseconfig_file):
42d1a9
    usage(f"base configs file {baseconfig_file:s} does not exist")
42d1a9
42d1a9
if len(sys.argv) == 4:
42d1a9
    print(f"# {sys.argv[3]:s}")
42d1a9
42d1a9
# read each line of the override file and store any configuration values
42d1a9
# in the overrides dictionary, keyed by the configuration name.
42d1a9
overrides = {}
42d1a9
with open(override_file, "rt", encoding="utf-8") as f:
42d1a9
    for line in [l.strip() for l in f.readlines()]:
42d1a9
        c = find_config(line)
42d1a9
        if c and c not in overrides:
42d1a9
            overrides[c] = line
42d1a9
42d1a9
# now read and print the base config, checking each line
42d1a9
# that defines a config value and printing the override if
42d1a9
# it exists
42d1a9
with open(baseconfig_file, "rt", encoding="utf-8") as f:
42d1a9
    for line in [ l.strip() for l in f.readlines() ]:
42d1a9
        c = find_config(line)
42d1a9
        if c and c in overrides:
42d1a9
            print(overrides[c])
42d1a9
            del overrides[c]
42d1a9
        else:
42d1a9
            print(line)
42d1a9
42d1a9
# print out the remaining configs (new values)
42d1a9
# from the overrides file
42d1a9
for v in overrides.values():
42d1a9
    print (v)
42d1a9
42d1a9
sys.exit(0)