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