diff --git a/check_slapdd_crc32 b/check_slapdd_crc32 index 3426b53..0d6e83b 100755 --- a/check_slapdd_crc32 +++ b/check_slapdd_crc32 @@ -12,7 +12,7 @@ import sys default_slapdd_path = '/etc/ldap/slapd.d' -### MAIN #### +# Main parser = argparse.ArgumentParser() parser.add_argument( @@ -53,7 +53,7 @@ parser.add_argument( action='store', type=str, 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 ) @@ -62,7 +62,9 @@ options = parser.parse_args() # Initialize log 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: log.setLevel(logging.DEBUG) @@ -81,6 +83,7 @@ if not options.logfile or options.console: logconsole.setFormatter(logformat) log.addHandler(logconsole) + def check_file(dir_path, file_name): """ Check CRC32 of an LDIF file @@ -95,31 +98,52 @@ def check_file(dir_path, file_name): try: with open(path, 'rb') as fd: for line in fd.readlines(): - if line.startswith(b'# AUTO-GENERATED FILE - DO NOT EDIT!! Use ldapmodify.'): - logging.debug('%s: AUTO-GENERATED line detected, pass (%s)', path, line) + if line.startswith( + b'# AUTO-GENERATED FILE - DO NOT EDIT!! Use ldapmodify.' + ): + logging.debug( + '%s: AUTO-GENERATED line detected, pass (%s)', + path, line) continue if line.startswith(b'# CRC32 '): - logging.debug('%s: CRC32 line detected, retreive 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) + logging.debug( + '%s: CRC32 line detected, retreive 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 lines.append(line) except IOError as err: logging.error('%s: fail to read file content (%s)', path, err) 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 == crc32: log.info('%s: current CRC32 value is correct (%s)', path, crc32) 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) 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) return True + def fix_crc32(path, crc32, lines): """ Fix CRC32 value of an LDIF file @@ -143,9 +167,12 @@ def fix_crc32(path, crc32, lines): return False return True + log.info('Checking CRC32 in slapd directory "%s"', 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: if fname.endswith('.ldif'): check_file(dirpath, fname)