From d457302fc36c32106610f4fb49420d401fa99101 Mon Sep 17 00:00:00 2001 From: Ben Grande Date: Fri, 5 Jul 2024 12:24:24 +0200 Subject: [PATCH] feat: lint python files --- .pre-commit-config.yaml | 33 +++++++++------ docs/CONTRIBUTE.md | 5 ++- salt/sys-cacher/files/admin/list_extra_tag.py | 14 +++---- salt/sys-cacher/files/admin/tag.py | 14 +++---- scripts/python-lint.sh | 42 +++++++++++++++++++ 5 files changed, 75 insertions(+), 33 deletions(-) create mode 100755 scripts/python-lint.sh diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4608e6a..cb45260 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,8 +14,8 @@ repos: pass_filenames: true description: Prohibit Unicode - - id: reuse - name: reuse + - id: reuse-lint + name: reuse-lint entry: reuse args: [lint] language: python @@ -29,6 +29,13 @@ repos: pass_filenames: true description: Lint markdown files + - id: python-lint + name: python-lint + entry: scripts/python-lint.sh + language: script + pass_filenames: true + description: Lint python files + - id: salt-lint name: salt-lint entry: scripts/salt-lint.sh @@ -36,22 +43,13 @@ repos: pass_filenames: true description: Lint Salt files - - id: shellcheck - name: shellcheck + - id: shell-lint + name: shell-lint entry: scripts/shell-lint.sh language: script pass_filenames: true description: Lint Shellscripts - - id: gitlint - name: gitlint - language: python - entry: gitlint - args: [--staged, --msg-filename] - stages: [commit-msg] - pass_filenames: true - description: Lint Git commits - - id: qubesbuilder-gen name: qubesbuilder-gen entry: scripts/qubesbuilder-gen.sh @@ -68,3 +66,12 @@ repos: # args: [test] # # pass_filenames: true # description: Check if RPM SPEC files are up to date + + - id: git-lint + name: git-lint + language: python + entry: gitlint + args: [--staged, --msg-filename] + stages: [commit-msg] + pass_filenames: true + description: Lint Git commits diff --git a/docs/CONTRIBUTE.md b/docs/CONTRIBUTE.md index 99ac57a..1ddb3e3 100644 --- a/docs/CONTRIBUTE.md +++ b/docs/CONTRIBUTE.md @@ -61,10 +61,11 @@ For writing: For linting: -* gitlint -* markdownlint (ruby-mdl) * pre-commit +* gitlint +* pylint * reuse +* ruby-mdl (markdownlint) * salt-lint * shellcheck diff --git a/salt/sys-cacher/files/admin/list_extra_tag.py b/salt/sys-cacher/files/admin/list_extra_tag.py index 72693ff..f49f23a 100755 --- a/salt/sys-cacher/files/admin/list_extra_tag.py +++ b/salt/sys-cacher/files/admin/list_extra_tag.py @@ -6,15 +6,11 @@ """list-extra-tag - List qubes tagged for cacher incorrectly""" -import qubesadmin -import qubesadmin.vm +import qubesadmin # pylint: disable=import-error +import qubesadmin.vm # pylint: disable=import-error -def main(): - """main""" - wanted_domains = ['debian', 'fedora', 'arch'] - ## TODO: remove after https://github.com/QubesOS/qubes-core-agent-linux/pull/504 - wanted_domains_extra = wanted_domains + ['kali', 'kicksecure', 'parrot', - 'ubuntu', 'linuxmint', 'blackarch'] +def main(): # pylint: disable=missing-function-docstring + wanted_domains = ['debian', 'fedora', 'arch', 'ubuntu', 'kicksecure'] domains = [ vm.name for vm in qubesadmin.Qubes().domains @@ -22,7 +18,7 @@ def main(): and vm.features.check_with_template("os-distribution-like") not in wanted_domains and vm.features.check_with_template("os-distribution") - not in wanted_domains_extra + not in wanted_domains ] print("\n".join(domains)) diff --git a/salt/sys-cacher/files/admin/tag.py b/salt/sys-cacher/files/admin/tag.py index d234eb0..086f5b1 100755 --- a/salt/sys-cacher/files/admin/tag.py +++ b/salt/sys-cacher/files/admin/tag.py @@ -6,22 +6,18 @@ """list-extra-tag - List qubes that can be tagged for cacher""" -import qubesadmin -import qubesadmin.vm +import qubesadmin # pylint: disable=import-error +import qubesadmin.vm # pylint: disable=import-error -def main(): - """main""" - wanted_domains = ['debian', 'fedora', 'arch'] - ## TODO: remove after https://github.com/QubesOS/qubes-core-agent-linux/pull/504 - wanted_domains_extra = wanted_domains + ['kali', 'kicksecure', 'parrot', - 'ubuntu', 'linuxmint', 'blackarch'] +def main(): # pylint: disable=missing-function-docstring + wanted_domains = ['debian', 'fedora', 'arch', 'ubuntu', 'kicksecure'] domains = [ vm.name for vm in qubesadmin.Qubes().domains if vm.klass == "TemplateVM" and "whonix-updatevm" not in vm.tags and (vm.features.get("os-distribution-like") in wanted_domains - or vm.features.get("os-distribution") in wanted_domains_extra) + or vm.features.get("os-distribution") in wanted_domains) ] print("\n".join(domains)) diff --git a/scripts/python-lint.sh b/scripts/python-lint.sh new file mode 100755 index 0000000..5de7b88 --- /dev/null +++ b/scripts/python-lint.sh @@ -0,0 +1,42 @@ +#!/bin/sh + +## SPDX-FileCopyrightText: 2024 Benjamin Grande M. S. +## +## SPDX-License-Identifier: AGPL-3.0-or-later + +# shellcheck disable=SC2086 +set -eu + +command -v git >/dev/null || + { printf "Missing program: git\n" >&2; exit 1; } +cd "$(git rev-parse --show-toplevel)" || exit 1 +./scripts/requires-program.sh pylint + +find_tool="find" +if command -v fd; then + find_tool="fd" +elif command -v fdfind >/dev/null; then + find_tool="fdfind" +fi + +if test -n "${1-}"; then + files="" + for f in "$@"; do + test -f "$f" || continue + extension="${f##*.}" + case "$extension" in + py) files="$files $f";; + *) continue + ;; + esac + done + test -n "$files" || exit 0 + exec pylint ${files} +fi + +case "${find_tool}" in + fd|fdfind) files="$(${find_tool} . -H -t f -e py)";; + find) files="$(find . -type f -name "*.py")";; +esac + +exec pylint ${files}