Fix pylint & flake8 warnings

This commit is contained in:
Benjamin Renard 2022-05-01 12:59:51 +02:00
parent 57c93181b1
commit 4aaeabba6a

View file

@ -12,7 +12,7 @@ import sys
default_slapdd_path = '/etc/ldap/slapd.d' default_slapdd_path = '/etc/ldap/slapd.d'
### MAIN #### # Main
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument( parser.add_argument(
@ -53,7 +53,7 @@ parser.add_argument(
action='store', action='store',
type=str, type=str,
dest='slapdd_path', dest='slapdd_path',
help='Default slapd.d directory path (default: %s)' % default_slapdd_path, help=f'Default slapd.d directory path (default: {default_slapdd_path}',
default=default_slapdd_path default=default_slapdd_path
) )
@ -62,7 +62,9 @@ options = parser.parse_args()
# Initialize log # Initialize log
log = logging.getLogger() log = logging.getLogger()
logformat = logging.Formatter("%(asctime)s - " + os.path.basename(sys.argv[0]) + " - %(levelname)s - %(message)s") logformat = logging.Formatter(
f'%(asctime)s - {os.path.basename(sys.argv[0])} - %(levelname)s - '
'%(message)s')
if options.debug: if options.debug:
log.setLevel(logging.DEBUG) log.setLevel(logging.DEBUG)
@ -81,6 +83,7 @@ if not options.logfile or options.console:
logconsole.setFormatter(logformat) logconsole.setFormatter(logformat)
log.addHandler(logconsole) log.addHandler(logconsole)
def check_file(dir_path, file_name): def check_file(dir_path, file_name):
""" """
Check CRC32 of an LDIF file Check CRC32 of an LDIF file
@ -95,31 +98,52 @@ def check_file(dir_path, file_name):
try: try:
with open(path, 'rb') as fd: with open(path, 'rb') as fd:
for line in fd.readlines(): for line in fd.readlines():
if line.startswith(b'# AUTO-GENERATED FILE - DO NOT EDIT!! Use ldapmodify.'): if line.startswith(
logging.debug('%s: AUTO-GENERATED line detected, pass (%s)', path, line) b'# AUTO-GENERATED FILE - DO NOT EDIT!! Use ldapmodify.'
):
logging.debug(
'%s: AUTO-GENERATED line detected, pass (%s)',
path, line)
continue continue
if line.startswith(b'# CRC32 '): if line.startswith(b'# CRC32 '):
logging.debug('%s: CRC32 line detected, retreive current CRC32 value (%s)', path, line) logging.debug(
current_crc32 = re.match('^# CRC32 (.*)$', line.decode()).group(1) '%s: CRC32 line detected, retreive current CRC32 '
logging.debug('%s: current CRC32 found is "%s"', path, current_crc32) 'value (%s)',
path, line)
current_crc32 = re.match(
'^# CRC32 (.*)$', line.decode()
).group(1)
logging.debug(
'%s: current CRC32 found is "%s"',
path, current_crc32)
continue continue
lines.append(line) lines.append(line)
except IOError as err: except IOError as err:
logging.error('%s: fail to read file content (%s)', path, err) logging.error('%s: fail to read file content (%s)', path, err)
return False return False
crc32 = ("%08X" % ((binascii.crc32(b"".join(lines)) & 0xFFFFFFFF) % (1<<32))).lower() # pylint: disable=consider-using-f-string
crc32 = (
"%08X" % (
(binascii.crc32(b"".join(lines)) & 0xFFFFFFFF) % (1 << 32)
)
).lower()
if current_crc32: if current_crc32:
if current_crc32 == crc32: if current_crc32 == crc32:
log.info('%s: current CRC32 value is correct (%s)', path, crc32) log.info('%s: current CRC32 value is correct (%s)', path, crc32)
else: else:
log.warning('%s: invalid CRC32 value found (%s != %s)', path, current_crc32, crc32) log.warning(
'%s: invalid CRC32 value found (%s != %s)',
path, current_crc32, crc32)
fix_crc32(path, crc32, lines) fix_crc32(path, crc32, lines)
else: else:
log.warning('%s: no CRC32 value found. Correct CRC32 value is "%s".', path, crc32) log.warning(
'%s: no CRC32 value found. Correct CRC32 value is "%s".',
path, crc32)
fix_crc32(path, crc32, lines) fix_crc32(path, crc32, lines)
return True return True
def fix_crc32(path, crc32, lines): def fix_crc32(path, crc32, lines):
""" """
Fix CRC32 value of an LDIF file Fix CRC32 value of an LDIF file
@ -143,9 +167,12 @@ def fix_crc32(path, crc32, lines):
return False return False
return True return True
log.info('Checking CRC32 in slapd directory "%s"', options.slapdd_path) log.info('Checking CRC32 in slapd directory "%s"', options.slapdd_path)
for dirpath, dnames, fnames in os.walk(options.slapdd_path): for dirpath, dnames, fnames in os.walk(options.slapdd_path):
log.debug('%s: sub-dirs = "%s", files = "%s"', dirpath, '", "'.join(dnames), '", "'.join(fnames)) log.debug(
'%s: sub-dirs = "%s", files = "%s"',
dirpath, '", "'.join(dnames), '", "'.join(fnames))
for fname in fnames: for fname in fnames:
if fname.endswith('.ldif'): if fname.endswith('.ldif'):
check_file(dirpath, fname) check_file(dirpath, fname)