Skip to main content
Topic: config-diff Python script to compare linux kernel configs (Read 253 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

config-diff Python script to compare linux kernel configs

I use this script to compare decompressed kernel configs obtained from source or /proc/config.gz
Edit: allow for long values and treat is not set comments as missing
Code: [Select]
#!/bin/env python3
if __name__=='__main__':
import sys, os
maxw = int(os.environ.get('CONFIG_DIFF_MAXW','30'))
missing = int(os.environ.get('CONFIG_DIFF_MISSING','0'))
prog = os.path.basename(sys.argv[0])
stderr = sys.stderr
if len(sys.argv)!=3:
print(f'!!!!! {prog}: usage {prog} config1 config2',file=stderr)
sys.exit(1)
def getconfigs(fn):
configs = {}
bfn = os.path.basename(fn)
with open(fn,'r') as f:
L = f.readlines()
for i,line in enumerate(L):
line = line.strip()
if 'CONFIG_' not in line: continue
if line.startswith('#'):
if not missing: continue
line = line[1:].strip()
if 'is not set' in line:
name = line.split()[0]
value = 'missing' if missing else 'is not set'
else:
print(f'!!!!! {prog}:{bfn}:{i} unhandled comment: {L[i]}',file=stderr)
continue
else:
neq = line.count('=')
if neq!=1:
if '="' not in line or not line.endswith('"'):
error = 'multiple assignment' if neq else 'missing assignment'
print(f'!!!!! {prog}:{bfn}:{i} {error}: {L[i]}',file=stderr)
continue

name, value = line.split('=',1)
configs[name.strip()] = value.strip()
return bfn, configs

bfn1, configs1 = getconfigs(sys.argv[1])
bfn2, configs2 = getconfigs(sys.argv[2])

if configs1!=configs2:
rows = [('Name', bfn1, bfn2)]
N = set(configs1.keys()).union(configs2.keys())
W = list(map(len,rows[0]))
for name in sorted(N):
value1 = configs1[name] if name in configs1 else 'missing'
value2 = configs2[name] if name in configs2 else 'missing'
if value1!=value2:
row = (name,value1,value2)
for i in range(3):
W[i] = max(W[i],len(row[i]))
rows.append(row)
W = (min(W[0],50),)+tuple((min(maxw,_) for _ in W[1:]))
fmt0 = '%%-%ds %%%ds %%%ds' % W
fmt1 = '%%%ds %%-%ds %%-%ds' % W

def colSplit(s,w):
while len(s)>w:
yield s[:w]
s = s[w:]
yield s

for row in rows:
row = [tuple(colSplit(r,w)) for r,w in zip(row,W)]
n = max((len(r) for r in row))
fmt = fmt0
for i in range(n):
cols = tuple((r[i] if i<len(r) else '' for r in row))
#print(f'{n=} {i=} {row=} {cols=}')
print(fmt % cols)
fmt = fmt1