]> git.scottworley.com Git - nix-env-apps/commitdiff
mypy, pylint, autopep8, & pre-commit hook to run them
authorScott Worley <scottworley@scottworley.com>
Sat, 13 Sep 2025 17:41:42 +0000 (10:41 -0700)
committerScott Worley <scottworley@scottworley.com>
Sat, 13 Sep 2025 17:41:42 +0000 (10:41 -0700)
apps.py
default.nix
git-pre-commit-hook [new file with mode: 0755]
test.sh [new file with mode: 0755]

diff --git a/apps.py b/apps.py
index 4b0d0d581a5c917d442baf3f64432c9b1b37c36c..959c14893af48934260d651d2851b96c29083bdf 100644 (file)
--- 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)
index 17ddac762b2ccbe2e5eb2ae495c7610d2e7baa91..94bc300683180cd22c61882cf3696207c98893eb 100644 (file)
@@ -1,14 +1,19 @@
-{ pkgs ? import <nixpkgs> { } }:
-pkgs.python3Packages.callPackage ({ buildPythonPackage, gobject-introspection
-  , gtk4, lib, pygobject3, wrapGAppsHook4, }:
+{ pkgs ? import <nixpkgs> { }, 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 (executable)
index 0000000..ce47614
--- /dev/null
@@ -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 (executable)
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