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