From 9a3bd7eb616549c229f4c55ca7aad3235f92a019 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Capypara=20K=C3=B6pcke?= Date: Sun, 29 Oct 2023 17:03:57 +0100 Subject: [PATCH 1/4] ci: Add black to Flatpak, run as pre-commit hook --- black.sh | 20 +++++ build-aux/README.md | 27 ++++++ build-aux/modules/python-black.json | 104 ++++++++++++++++++++++++ build-aux/re.sonny.Workbench.Devel.json | 1 + build-aux/re.sonny.Workbench.json | 1 + package.json | 1 + src/Library/demos/Welcome/main.py | 11 +-- 7 files changed, 156 insertions(+), 9 deletions(-) create mode 100755 black.sh create mode 100644 build-aux/README.md create mode 100644 build-aux/modules/python-black.json diff --git a/black.sh b/black.sh new file mode 100755 index 000000000..f621c1538 --- /dev/null +++ b/black.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +# This is a small wrapper around black - the Python code formatter. +# The script will setup and activate a virtual env in .venv if black is not +# found in PATH. +# +# Still requires Python 3. +set -e + +if ! which black &>/dev/null; then + if [ ! -d ".venv" ]; then + echo "Note: black was not found on your system. A virtualenv is created in .venv." + python3 -m venv .venv + fi + source .venv/bin/activate + if ! which black &>/dev/null; then + pip3 install black + fi +fi + +exec black "$@" diff --git a/build-aux/README.md b/build-aux/README.md new file mode 100644 index 000000000..daf99b70e --- /dev/null +++ b/build-aux/README.md @@ -0,0 +1,27 @@ +# Build Utilities + +This directory contains the Flatpak Manifest and other build utilities. + +## Workbench Flatpak Manifests + +### Python Black Dependency + +`modules/python-black.json` contains the Flatpak modules to install [https://github.com/psf/black](black), the +uncompromising Python code formatter. + +This file is partially auto-generated. Here is how to generate it: + +1. Obtain an up-to-date copy of [flatpak-builder-tools](https://github.com/flatpak/flatpak-builder-tools). +2. Make sure you have a Python virtualenv activated with the dependencies for the `pip` generator from the repo above. +3. Run `python3 <...>/flatpak-builder-tools/pip/flatpak-pip-generator black -o modules/python-black --build-isolation` + +You will notice that Workbench will now not build. This is due to these issues: + +- https://github.com/flatpak/flatpak-builder-tools/issues/380 +- https://github.com/pypa/pip/issues/7863 + +This means that the generated JSON file now needs its build dependencies manually added. Check the build dependencies +of black and their dependencies and add them to the JSON as well. Brute-forcing the build to obtain missing packages +may help as does referencing old commits of the file. You can also use the generator command to generate dependencies +for them and then merge it into the file by hand, but note that you will also need to manually collect their build +dependencies too. diff --git a/build-aux/modules/python-black.json b/build-aux/modules/python-black.json new file mode 100644 index 000000000..1611d749f --- /dev/null +++ b/build-aux/modules/python-black.json @@ -0,0 +1,104 @@ +{ + "name": "python3-black", + "buildsystem": "simple", + "build-commands": [ + "pip3 install --verbose --exists-action=i --no-index --find-links=\"file://${PWD}\" --prefix=${FLATPAK_DEST} \"black\"" + ], + "sources": [ + { + "type": "file", + "url": "https://files.pythonhosted.org/packages/36/bf/a462f36723824c60dc3db10528c95656755964279a6a5c287b4f9fd0fa84/black-23.10.1.tar.gz", + "sha256": "1f8ce316753428ff68749c65a5f7844631aa18c8679dfd3ca9dc1a289979c258" + }, + { + "type": "file", + "url": "https://files.pythonhosted.org/packages/00/2e/d53fa4befbf2cfa713304affc7ca780ce4fc1fd8710527771b58311a3229/click-8.1.7-py3-none-any.whl", + "sha256": "ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28" + }, + { + "type": "file", + "url": "https://files.pythonhosted.org/packages/2a/e2/5d3f6ada4297caebe1a2add3b126fe800c96f56dbe5d1988a2cbe0b267aa/mypy_extensions-1.0.0-py3-none-any.whl", + "sha256": "4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d" + }, + { + "type": "file", + "url": "https://files.pythonhosted.org/packages/ec/1a/610693ac4ee14fcdf2d9bf3c493370e4f2ef7ae2e19217d7a237ff42367d/packaging-23.2-py3-none-any.whl", + "sha256": "8c491190033a9af7e1d931d0b5dacc2ef47509b34dd0de67ed209b5203fc88c7" + }, + { + "type": "file", + "url": "https://files.pythonhosted.org/packages/b4/2a/9b1be29146139ef459188f5e420a66e835dda921208db600b7037093891f/pathspec-0.11.2-py3-none-any.whl", + "sha256": "1d6ed233af05e679efb96b1851550ea95bbb64b7c490b0f5aa52996c11e92a20" + }, + { + "type": "file", + "url": "https://files.pythonhosted.org/packages/56/29/3ec311dc18804409ecf0d2b09caa976f3ae6215559306b5b530004e11156/platformdirs-3.11.0-py3-none-any.whl", + "sha256": "e9d171d00af68be50e9202731309c4e658fd8bc76f55c11c7dd760d023bda68e" + }, + { + "type": "file", + "url": "https://files.pythonhosted.org/packages/6b/be/0f2f4a5e8adc114a02b63d92bf8edbfa24db6fc602fca83c885af2479e0e/editables-0.5-py3-none-any.whl", + "sha256": "61e5ffa82629e0d8bfe09bc44a07db3c1ab8ed1ce78a6980732870f19b5e7d4c" + }, + { + "type": "file", + "url": "https://files.pythonhosted.org/packages/76/56/8ccca673e2c896931722f876bf040c0b6a7d8c1a128be60516a8a55bb27a/hatchling-1.18.0-py3-none-any.whl", + "sha256": "b66dc254931ec42aa68b5febd1d342c58142cc5267b7ff3b12ba3fa5b4900c93" + }, + { + "type": "file", + "url": "https://files.pythonhosted.org/packages/ec/1a/610693ac4ee14fcdf2d9bf3c493370e4f2ef7ae2e19217d7a237ff42367d/packaging-23.2-py3-none-any.whl", + "sha256": "8c491190033a9af7e1d931d0b5dacc2ef47509b34dd0de67ed209b5203fc88c7" + }, + { + "type": "file", + "url": "https://files.pythonhosted.org/packages/b4/2a/9b1be29146139ef459188f5e420a66e835dda921208db600b7037093891f/pathspec-0.11.2-py3-none-any.whl", + "sha256": "1d6ed233af05e679efb96b1851550ea95bbb64b7c490b0f5aa52996c11e92a20" + }, + { + "type": "file", + "url": "https://files.pythonhosted.org/packages/05/b8/42ed91898d4784546c5f06c60506400548db3f7a4b3fb441cba4e5c17952/pluggy-1.3.0-py3-none-any.whl", + "sha256": "d89c696a773f8bd377d18e5ecda92b7a3793cbe66c87060a6fb58c7b6e1061f7" + }, + { + "type": "file", + "url": "https://files.pythonhosted.org/packages/ec/40/05cb2725ca7e6c844c66af626c5749efd254ec4506f17a1d01ba79ae9da6/trove_classifiers-2023.10.18-py3-none-any.whl", + "sha256": "20a3da8e3cb65587cc9f5d5b837bf74edeb480bba9bd8cd4f03ab056d6b06c4c" + }, + { + "type": "file", + "url": "https://files.pythonhosted.org/packages/00/b1/90cc7881c2e870333eeafa6afb7b27de53418aad1ba5409ad331c96608a1/hatch_vcs-0.3.0-py3-none-any.whl", + "sha256": "60ce59a3fa4664057e4a858b6a96ab0b9dec21bf8f562f836139315bb361be8c" + }, + { + "type": "file", + "url": "https://files.pythonhosted.org/packages/0e/a3/b9a8b0adfe672bf0df5901707aa929d30a97ee390ba651910186776746d2/setuptools_scm-8.0.4-py3-none-any.whl", + "sha256": "b47844cd2a84b83b3187a5782c71128c28b4c94cad8bfb871da2784a5cb54c4f" + }, + { + "type": "file", + "url": "https://files.pythonhosted.org/packages/24/21/7d397a4b7934ff4028987914ac1044d3b7d52712f30e2ac7a2ae5bc86dd0/typing_extensions-4.8.0-py3-none-any.whl", + "sha256": "8f92fc8806f9a6b641eaa5318da32b44d401efaac0f6678c9bc448ba3605faa0" + }, + { + "type": "file", + "url": "https://files.pythonhosted.org/packages/6b/74/74a15619861065fa75f6170c649cd2fd5467726bb407ec2017d4a3f92d28/hatch_fancy_pypi_readme-23.1.0-py3-none-any.whl", + "sha256": "9a2c0e5d527611701858083bfb355876f26e04a075342b7d4da725ba5a6dd6ea" + }, + { + "type": "file", + "url": "https://files.pythonhosted.org/packages/ef/cc/93f7213b2ab5ed383f98ce8020e632ef256b406b8569606c3f160ed8e1c9/setuptools-68.2.2.tar.gz", + "sha256": "4ac1475276d2f1c48684874089fefcd83bd7162ddaafb81fac866ba0db282a87" + }, + { + "type": "file", + "url": "https://files.pythonhosted.org/packages/a4/99/78c4f3bd50619d772168bec6a0f34379b02c19c9cced0ed833ecd021fd0d/wheel-0.41.2.tar.gz", + "sha256": "0c5ac5ff2afb79ac23ab82bab027a0be7b5dbcf2e54dc50efe4bf507de1f7985" + }, + { + "type": "file", + "url": "https://files.pythonhosted.org/packages/c4/e6/c1ac50fe3eebb38a155155711e6e864e254ce4b6e17fe2429b4c4d5b9e80/flit_core-3.9.0.tar.gz", + "sha256": "72ad266176c4a3fcfab5f2930d76896059851240570ce9a98733b658cb786eba" + } + ] +} diff --git a/build-aux/re.sonny.Workbench.Devel.json b/build-aux/re.sonny.Workbench.Devel.json index efc52fce0..3a942f9d7 100644 --- a/build-aux/re.sonny.Workbench.Devel.json +++ b/build-aux/re.sonny.Workbench.Devel.json @@ -45,6 +45,7 @@ "modules/blueprint-compiler.json", "modules/biome.json", "modules/icon-development-kit.json", + "modules/python-black.json", { "name": "Workbench", "buildsystem": "meson", diff --git a/build-aux/re.sonny.Workbench.json b/build-aux/re.sonny.Workbench.json index 55e5fea8d..2f272e78c 100644 --- a/build-aux/re.sonny.Workbench.json +++ b/build-aux/re.sonny.Workbench.json @@ -45,6 +45,7 @@ "modules/blueprint-compiler.json", "modules/biome.json", "modules/icon-development-kit.json", + "modules/python-black.json", { "name": "Workbench", "buildsystem": "meson", diff --git a/package.json b/package.json index a1fe8e148..0ba067830 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "lint-staged": { "*.{css,json,md,yaml,yml}": "prettier --write", "*.{js,cjs,mjs}": "eslint --fix", + "*.{py}": "./black.sh", "*.rs": "flatpak run --user --command=/usr/lib/sdk/rust-stable/bin/rustfmt --filesystem=host org.gnome.Sdk//45 --edition 2021" } } diff --git a/src/Library/demos/Welcome/main.py b/src/Library/demos/Welcome/main.py index 6daa21fc7..222b6e62e 100644 --- a/src/Library/demos/Welcome/main.py +++ b/src/Library/demos/Welcome/main.py @@ -7,10 +7,7 @@ def greet(_widget): - dialog = Adw.MessageDialog( - body="Hello World!", - transient_for=workbench.window - ) + dialog = Adw.MessageDialog(body="Hello World!", transient_for=workbench.window) dialog.add_response("ok", "Ok") dialog.connect("response", dialog_response) @@ -24,11 +21,7 @@ def dialog_response(dialog: Adw.MessageDialog, response: str): subtitle_box: Gtk.Box = workbench.builder.get_object("subtitle") -button = Gtk.Button( - label="Press me", - margin_top=6, - css_classes=["suggested-action"] -) +button = Gtk.Button(label="Press me", margin_top=6, css_classes=["suggested-action"]) button.connect("clicked", greet) subtitle_box.append(button) From 1f004df108076327097bb2318414cc740f101f31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Capypara=20K=C3=B6pcke?= Date: Sun, 29 Oct 2023 17:10:01 +0100 Subject: [PATCH 2/4] ci: Run black to format Python code --- src/window.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/window.js b/src/window.js index ccd64b514..ea3b0d7ca 100644 --- a/src/window.js +++ b/src/window.js @@ -242,6 +242,25 @@ export default function Window({ application, session }) { return stdout; } + function formatPythonCode(text) { + const blackLauncher = Gio.SubprocessLauncher.new( + Gio.SubprocessFlags.STDIN_PIPE | + Gio.SubprocessFlags.STDOUT_PIPE | + Gio.SubprocessFlags.STDERR_PIPE, + ); + + const blackProcess = blackLauncher.spawnv(["black", "--quiet", "-"]); + + const [success, stdout, stderr] = blackProcess.communicate_utf8(text, null); + + if (!success || stderr !== "") { + console.error(`Error running black: ${stderr}`); + return text; + } + + return stdout; + } + async function formatCode() { if (panel_code.panel.visible) { if (panel_code.language === "JavaScript") { @@ -255,6 +274,10 @@ export default function Window({ application, session }) { await format(langs.rust.document.code_view, (text) => { return formatRustCode(text); }); + } else if (panel_code.language === "Python") { + await format(langs.python.document.code_view, (text) => { + return formatPythonCode(text); + }); } } From 635813e09baf02eceff12076446f74f90c03deb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Capypara=20K=C3=B6pcke?= Date: Sun, 29 Oct 2023 17:16:55 +0100 Subject: [PATCH 3/4] ci: remove unnesecary brackets --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0ba067830..d5ebd6d6d 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "lint-staged": { "*.{css,json,md,yaml,yml}": "prettier --write", "*.{js,cjs,mjs}": "eslint --fix", - "*.{py}": "./black.sh", + "*.py": "./black.sh", "*.rs": "flatpak run --user --command=/usr/lib/sdk/rust-stable/bin/rustfmt --filesystem=host org.gnome.Sdk//45 --edition 2021" } } From 477993effc6a09541cfa44c072f02287c9ff50b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Capypara=20K=C3=B6pcke?= Date: Sun, 29 Oct 2023 17:26:46 +0100 Subject: [PATCH 4/4] ci: Add black to ci --- Makefile | 2 ++ src/langs/python/python-previewer.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index b0f96812e..b9b9722b6 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,8 @@ lint: flatpak run --user --command=/usr/lib/sdk/node18/bin/node --filesystem=host:ro org.gnome.Sdk//45 node_modules/.bin/eslint --max-warnings=0 src # rustfmt flatpak run --user --command=/usr/lib/sdk/rust-stable/bin/rustfmt --filesystem=host:ro org.gnome.Sdk//45 --check --edition 2021 src/**/*.rs +# black + ./black.sh --check src/**/*.py # gettext find po/ -type f -name "*po" -print0 | xargs -0 -n1 msgfmt -o /dev/null --check # Flatpak manifests diff --git a/src/langs/python/python-previewer.py b/src/langs/python/python-previewer.py index 0475f7a57..70a732de6 100644 --- a/src/langs/python/python-previewer.py +++ b/src/langs/python/python-previewer.py @@ -22,7 +22,7 @@ gi.require_version("Gtk", "4.0") gi.require_version("Adw", "1") gi.require_version("Graphene", "1.0") -gi.require_version('Gsk', '4.0') +gi.require_version("Gsk", "4.0") from gi.repository import GLib, Gdk, Gtk, Adw, Graphene, Gio, Gsk from gi.repository.Gio import DBusConnection, DBusConnectionFlags