This commit is contained in:
Klaus Zerwes 2022-08-05 19:18:40 +02:00
parent c545058986
commit 0e39bc48a9

View File

@ -1,28 +1,30 @@
#! /usr/bin/env python3 #! /usr/bin/env python3
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 smartindent # vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 smartindent
# pylint: disable=invalid-name
import sys import sys
import os import os
import subprocess import subprocess
import argparse import argparse
import json import json
import yaml
import re import re
import fileinput import fileinput
import difflib import difflib
import fnmatch import fnmatch
import yaml
__doc__ = """ __doc__ = """
simple script to fix the fqcn module names simple script to fix the fqcn module names
""" """
def isexcluded(path, exclude_paths): def isexcluded(path, _exclude_paths):
"""check if a path element should be excluded"""
path = os.path.abspath(path) path = os.path.abspath(path)
return any( return any(
path.startswith(ep) path.startswith(ep)
or or
fnmatch.fnmatch(path, ep) fnmatch.fnmatch(path, ep)
for ep in exclude_paths for ep in _exclude_paths
) )
argparser = argparse.ArgumentParser(description=__doc__) argparser = argparse.ArgumentParser(description=__doc__)
@ -141,9 +143,9 @@ for f in parsefiles:
originallines.append(line) originallines.append(line)
for s, r in fqdndict.items(): for s, r in fqdndict.items():
if not startingwhitespaces: if not startingwhitespaces:
nline = re.sub('^(\s*)%s:' % s, '\\1%s:' % r, nline) nline = re.sub(r'^(\s*)%s:' % s, '\\1%s:' % r, nline)
if nline != line: if nline != line:
startingwhitespaces = re.search('^\s*', nline).group() startingwhitespaces = re.search(r'^\s*', nline).group()
else: else:
nline = re.sub('^(%s)%s:' % (startingwhitespaces, s), '\\1%s:' % r, nline) nline = re.sub('^(%s)%s:' % (startingwhitespaces, s), '\\1%s:' % r, nline)
if args.writefiles: if args.writefiles:
@ -152,6 +154,11 @@ for f in parsefiles:
changedlines.append(nline) changedlines.append(nline)
print('', file=sys.stderr) print('', file=sys.stderr)
if args.printdiff: if args.printdiff:
diff = difflib.unified_diff(originallines, changedlines, fromfile='a/%s' % f, tofile='b/%s' % f) diff = difflib.unified_diff(
if diff: originallines,
changedlines,
fromfile='a/%s' % f,
tofile='b/%s' % f
)
if len(diff):
sys.stderr.writelines(diff) sys.stderr.writelines(diff)