Convert pyfmt to being a shell script written in bash
- ID
86d0fde- date
2025-04-18 11:32:16+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
96a0e68- message
Convert `pyfmt` to being a shell script written in bash- changed files
5 files, 62 additions, 30 deletions
Changed files
config.fish (7003) → config.fish (7042)
diff --git a/config.fish b/config.fish
index b97f102..36a8aa5 100644
--- a/config.fish
+++ b/config.fish
@@ -55,6 +55,7 @@ prepend_to_path ~/repos/scripts/git
prepend_to_path ~/repos/scripts/images
prepend_to_path ~/repos/scripts/installers
prepend_to_path ~/repos/scripts/macos
+prepend_to_path ~/repos/scripts/python
prepend_to_path ~/repos/scripts/terraform
prepend_to_path ~/repos/scripts/text
prepend_to_path ~/repos/scripts/web
fish_functions/README.md (4537) → fish_functions/README.md (4335)
diff --git a/fish_functions/README.md b/fish_functions/README.md
index 936d60d..fe58cb9 100644
--- a/fish_functions/README.md
+++ b/fish_functions/README.md
@@ -132,15 +132,6 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=functions)
</dd>
<dt>
- <a href="https://github.com/alexwlchan/scripts/blob/main/fish_functions/pyfmt.fish">
- <code>pyfmt.fish</code>
- </a>
- </dt>
- <dd>
- Run Python formatting over a directory
- </dd>
-
- <dt>
<a href="https://github.com/alexwlchan/scripts/blob/main/fish_functions/reload_fish_config.fish">
<code>reload_fish_config.fish</code>
</a>
@@ -167,4 +158,4 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=functions)
Create and activate a new virtual environment
</dd>
</dl>
-<!-- [[[end]]] (checksum: 2019ea5d74d78cc1b4f84da5403e077d) -->
\ No newline at end of file
+<!-- [[[end]]] (checksum: 67f00a2925833d4359f4ab8087eab838) -->
\ No newline at end of file
fish_functions/pyfmt.fish (363) → fish_functions/pyfmt.fish (0)
diff --git a/fish_functions/pyfmt.fish b/fish_functions/pyfmt.fish
deleted file mode 100644
index e7306f7..0000000
--- a/fish_functions/pyfmt.fish
+++ /dev/null
@@ -1,18 +0,0 @@
-function _run_ruff
- if which ruff >/dev/null
- ruff $argv
- else
- ~/repos/scripts/.venv/bin/ruff $argv
- end
-end
-
-function pyfmt --description "Run Python formatting over a directory"
- if test (count $argv) -eq 0
- set root $PWD
- else
- set root $argv[1]
- end
-
- _run_ruff check "$root"
- _run_ruff format "$root"
-end
python/README.md (487) → python/README.md (778)
diff --git a/python/README.md b/python/README.md
index 3967a17..91a4865 100644
--- a/python/README.md
+++ b/python/README.md
@@ -16,11 +16,24 @@ import cog_helpers
folder_name = "python"
-scripts = []
+scripts = [
+ {
+ "name": "pyfmt [...PATH]",
+ "description": "Format Python files with ruff.",
+ },
+]
cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
]]]-->
<dl>
+ <dt>
+ <a href="https://github.com/alexwlchan/scripts/blob/main/python/pyfmt">
+ <code>pyfmt [...PATH]</code>
+ </a>
+ </dt>
+ <dd>
+ Format Python files with ruff.
+ </dd>
</dl>
-<!-- [[[end]]] (checksum: ba71f9e78718a8f6739d2b4bef0f1d58) -->
+<!-- [[[end]]] (checksum: 4c51b7cdb9a034e48bee58b82c75d99c) -->
python/pyfmt (0) → python/pyfmt (875)
diff --git a/python/pyfmt b/python/pyfmt
new file mode 100755
index 0000000..8b1cc75
--- /dev/null
+++ b/python/pyfmt
@@ -0,0 +1,45 @@
+#!/usr/bin/env bash
+# Lint and format Python files with `ruff`.
+#
+# This will format files in the working directory by default, or you
+# can supply one or more paths for it to check.
+
+set -o errexit
+set -o nounset
+
+
+# Work out which directory to run in:
+#
+# * if no argument is specified, use the current working directory
+# * if one or more directories are specified, use those
+#
+if (( $# == 0 ))
+then
+ ROOT="$(pwd)"
+else
+ ROOT="$@"
+fi
+
+
+
+# Work out which instance of ruff to use
+#
+# If we're in a virtualenv which has ruff installed, we use the locally
+# installed copy.
+#
+# If not, we use the `ruff` from my ~/repos/scripts virtualenv.
+if which ruff
+then
+ RUFF="$(which ruff)"
+else
+ RUFF=~/repos/scripts/.venv/bin/ruff
+fi
+
+
+echo -e "\033[34mruff check\033[0m"
+bash -c "$RUFF check $ROOT"
+
+echo ""
+
+echo -e "\033[34mruff format\033[0m"
+bash -c "$RUFF format $ROOT"