From 6f6187bfb0a55352aa2d2d67b6bfda48c3414c9a Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Sat, 13 Sep 2025 10:41:42 -0700 Subject: [PATCH] mypy, pylint, autopep8, & pre-commit hook to run them --- apps.py | 19 +++++++++++-------- default.nix | 15 ++++++++++----- git-pre-commit-hook | 32 ++++++++++++++++++++++++++++++++ test.sh | 22 ++++++++++++++++++++++ 4 files changed, 75 insertions(+), 13 deletions(-) create mode 100755 git-pre-commit-hook create mode 100755 test.sh diff --git a/apps.py b/apps.py index 4b0d0d5..959c148 100644 --- a/apps.py +++ b/apps.py @@ -1,12 +1,13 @@ -from gi.repository import Gtk import os import subprocess +from typing import Any import gi gi.require_version("Gtk", "4.0") +from gi.repository import Gtk # nopep8 pylint: disable=wrong-import-position -def on_edit(_): +def on_edit(_: Any) -> None: config_dir = os.path.join( os.environ.get('XDG_CONFIG_HOME', os.path.expanduser('~/.config')), 'nixpkgs', @@ -14,7 +15,7 @@ def on_edit(_): os.makedirs(config_dir, exist_ok=True) config_file = os.path.join(config_dir, 'userPackages.nix') try: - with open(config_file, mode="x") as f: + with open(config_file, mode="x", encoding='utf-8') as f: f.write('''final: prev: { userPackages = final.buildEnv { name = "userPackages"; @@ -31,17 +32,19 @@ def on_edit(_): subprocess.run(['xdg-open', config_file], check=True) -def try_exec_terminal(terminal, args): +def try_exec_terminal(terminal: str, args: list[str]) -> None: try: os.execvp(terminal, [terminal] + args) except FileNotFoundError: pass -def on_apply(_): +def on_apply(_: Any) -> None: command = ['nix-env', '-riA', 'nixos.userPackages'] command_string = ' '.join(command) - close_string = ''' && read -p "SUCCESS: Press ENTER to close this window" || read -p "FAILURE: Press ENTER to close this window"''' + close_string = ( + ''' && read -p "SUCCESS: Press ENTER to close this window"''' + + ''' || read -p "FAILURE: Press ENTER to close this window"''') # This should be a simple `xdg-terminal` invocation, but as of 2025, # xdg-terminal is extremely broken in Gnome: # * It doesn't cause a terminal window to appear @@ -77,7 +80,7 @@ def on_apply(_): os.execvp(command[0], command) -def on_activate(app): +def on_activate(app: Gtk.Application) -> None: win = Gtk.ApplicationWindow(application=app) box = Gtk.Box() box.set_orientation(Gtk.Orientation.VERTICAL) @@ -91,7 +94,7 @@ def on_activate(app): win.present() -def main(): +def main() -> None: app = Gtk.Application(application_id='net.chkno.nix-env-apps') app.connect('activate', on_activate) app.run(None) diff --git a/default.nix b/default.nix index 17ddac7..94bc300 100644 --- a/default.nix +++ b/default.nix @@ -1,14 +1,19 @@ -{ pkgs ? import { } }: -pkgs.python3Packages.callPackage ({ buildPythonPackage, gobject-introspection - , gtk4, lib, pygobject3, wrapGAppsHook4, }: +{ pkgs ? import { }, lint ? false }: +pkgs.python3Packages.callPackage ({ autopep8, buildPythonPackage + , gobject-introspection, gtk4, lib, mypy, pylint, pygobject3, wrapGAppsHook4, + }: buildPythonPackage rec { pname = "apps"; version = "1.0"; + src = lib.cleanSource ./.; - nativeBuildInputs = [ gobject-introspection wrapGAppsHook4 ]; - buildInputs = [ gtk4 ]; + doCheck = true; + checkPhase = "./test.sh"; + nativeBuildInputs = [ gobject-introspection wrapGAppsHook4 ]; + nativeCheckInputs = [ mypy ] ++ lib.optionals lint [ autopep8 pylint ]; + buildInputs = [ gtk4 ]; pythonPath = [ pygobject3 ]; }) { } diff --git a/git-pre-commit-hook b/git-pre-commit-hook new file mode 100755 index 0000000..ce47614 --- /dev/null +++ b/git-pre-commit-hook @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +# Copy me to .git/hooks/pre-commit + +set -e + +cleanup() { + if [[ "$D" && -e "$D" ]];then + rm -rf "$D" + fi +} +trap cleanup EXIT + +D=$(mktemp -d) +[[ "$D" && -d "$D" ]] + +git checkout-index --prefix="$D/" -a +pushd "$D" + +# So we don't confuse the inner git invocations inside the tests with +# the outer git invocation that's trying to commit a change. +unset GIT_AUTHOR_DATE +unset GIT_AUTHOR_EMAIL +unset GIT_AUTHOR_NAME +unset GIT_CONFIG_PARAMETERS +unset GIT_EXEC_PATH +unset GIT_INDEX_FILE +unset GIT_PREFIX + +nix-shell --arg lint true --run './test.sh lint' + +popd diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..998b58e --- /dev/null +++ b/test.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +set -e + +find . -name build -prune -o -name dist -prune -o -name '*.py' -print0 | + xargs -0 mypy --strict --ignore-missing-imports --no-warn-unused-ignores + +if [ "$1" = lint ];then + + find . -name '*.py' -print0 | xargs -0 pylint --reports=n --persistent=n --ignore-imports=y -d fixme,invalid-name,missing-docstring,subprocess-run-check,too-few-public-methods + + formatting_needs_fixing=$( + find . -name '*.py' -print0 | + xargs -0 -n1 autopep8 --diff -aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ) + if [ -n "$formatting_needs_fixing" ];then + echo "Formatting needs fixing:" + echo "$formatting_needs_fixing" + exit 1 + fi + +fi -- 2.50.1