Introduce pre-commit hooks

This commit is contained in:
Benjamin Renard 2024-03-13 23:37:29 +01:00
parent d7d54cb25f
commit f4f87fccd3
5 changed files with 112 additions and 83 deletions

64
.pre-commit-config.yaml Normal file
View file

@ -0,0 +1,64 @@
# Pre-commit hooks to run tests and ensure code is cleaned.
# See https://pre-commit.com for more information
---
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.6
hooks:
- id: ruff
args: ["--fix"]
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
hooks:
- id: pyupgrade
args: ["--keep-percent-format", "--py37-plus"]
- repo: https://github.com/psf/black
rev: 23.11.0
hooks:
- id: black
args: ["--target-version", "py37", "--line-length", "100"]
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
args: ["--profile", "black", "--line-length", "100"]
- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
hooks:
- id: flake8
args: ["--max-line-length=100"]
- repo: https://github.com/codespell-project/codespell
rev: v2.2.2
hooks:
- id: codespell
args:
- --ignore-words-list=exten
- --skip="./.*,*.csv,*.json,*.ini,*.subject,*.txt,*.html,*.log,*.conf"
- --quiet-level=2
- --ignore-regex=.*codespell-ignore$
# - --write-changes # Uncomment to write changes
exclude_types: [csv, json]
- repo: https://github.com/adrienverge/yamllint
rev: v1.32.0
hooks:
- id: yamllint
ignore: .github/
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.7.1
hooks:
- id: prettier
args: ["--print-width", "100"]
- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint
language: system
types: [python]
require_serial: true
- repo: https://github.com/PyCQA/bandit
rev: 1.7.5
hooks:
- id: bandit
args: [--skip, "B101", --recursive]
minimum_pre_commit_version: 3.2.0

View file

@ -4,7 +4,7 @@ This script permit to check (and eventually fix) CRC32 value of the LDIF files o
## Requirements
This script only used common __python3__ modules _(no additionnal package to install on Debian based systems)_.
This script only used common **python3** modules _(no additional package to install on Debian based systems)_.
## Installation
@ -43,4 +43,3 @@ This program is free software; you can redistribute it and/or modify it under th
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

View file

@ -24,7 +24,7 @@ sed -i "s/^version *=.*$/version = '$VERSION'/" $BDIR/check_slapdd_crc32
if [ -z "$DEBIAN_CODENAME" ]
then
echo "Retreive debian codename using lsb_release..."
echo "Retrieve debian codename using lsb_release..."
DEBIAN_CODENAME=$( lsb_release -c -s )
else
echo "Use debian codename from environment ($DEBIAN_CODENAME)"

View file

@ -10,54 +10,37 @@ import os
import re
import sys
version = '0.0'
default_slapdd_path = '/etc/ldap/slapd.d'
version = "0.0"
default_slapdd_path = "/etc/ldap/slapd.d"
# Main
parser = argparse.ArgumentParser(
description=f'{__doc__} (version: {version})'
parser = argparse.ArgumentParser(description=f"{__doc__} (version: {version})")
parser.add_argument("-d", "--debug", action="store_true", help="Show debug messages")
parser.add_argument("-v", "--verbose", action="store_true", help="Show verbose messages")
parser.add_argument(
"-l", "--log-file", action="store", type=str, dest="logfile", help="Log file path"
)
parser.add_argument(
'-d', '--debug',
action='store_true',
help='Show debug messages'
"-C",
"--console",
action="store_true",
help="Also log on console (even if log file is provided)",
)
parser.add_argument(
'-v', '--verbose',
action='store_true',
help='Show verbose messages'
)
parser.add_argument("-f", "--fix", action="store_true", help="Fix CRC32 value in LDIF files")
parser.add_argument(
'-l',
'--log-file',
"-p",
"--path",
action="store",
type=str,
dest="logfile",
help="Log file path"
)
parser.add_argument(
'-C', '--console',
action='store_true',
help='Also log on console (even if log file is provided)'
)
parser.add_argument(
'-f', '--fix',
action='store_true',
help='Fix CRC32 value in LDIF files'
)
parser.add_argument(
'-p', '--path',
action='store',
type=str,
dest='slapdd_path',
help=f'Default slapd.d directory path (default: {default_slapdd_path}',
default=default_slapdd_path
dest="slapdd_path",
help=f"Default slapd.d directory path (default: {default_slapdd_path}",
default=default_slapdd_path,
)
@ -66,8 +49,8 @@ options = parser.parse_args()
# Initialize log
log = logging.getLogger()
logformat = logging.Formatter(
f'%(asctime)s - {os.path.basename(sys.argv[0])} - %(levelname)s - '
'%(message)s')
f"%(asctime)s - {os.path.basename(sys.argv[0])} - %(levelname)s - " "%(message)s"
)
if options.debug:
log.setLevel(logging.DEBUG)
@ -99,50 +82,33 @@ def check_file(dir_path, file_name):
lines = []
current_crc32 = None
try:
with open(path, 'rb') as fd:
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 '):
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)
"%s: CRC32 line detected, retrieve 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)
except OSError as err:
logging.error("%s: fail to read file content (%s)", path, err)
return False
# pylint: disable=consider-using-f-string
crc32 = (
"%08X" % (
(binascii.crc32(b"".join(lines)) & 0xFFFFFFFF) % (1 << 32)
)
).lower()
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)
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
@ -159,14 +125,14 @@ def fix_crc32(path, crc32, lines):
return True
try:
headers_lines = [
b'# AUTO-GENERATED FILE - DO NOT EDIT!! Use ldapmodify.\n',
b'# CRC32 ' + crc32.encode() + b'\n',
b"# AUTO-GENERATED FILE - DO NOT EDIT!! Use ldapmodify.\n",
b"# CRC32 " + crc32.encode() + b"\n",
]
with open(path, 'wb') as fd:
with open(path, "wb") as fd:
for line in headers_lines + lines:
fd.write(line)
except IOError as err:
logging.error('%s: fail to write new file content (%s)', path, err)
except OSError as err:
logging.error("%s: fail to write new file content (%s)", path, err)
return False
return True
@ -174,8 +140,8 @@ def fix_crc32(path, crc32, lines):
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))
'%s: sub-dirs = "%s", files = "%s"', dirpath, '", "'.join(dnames), '", "'.join(fnames)
)
for fname in fnames:
if fname.endswith('.ldif'):
if fname.endswith(".ldif"):
check_file(dirpath, fname)