Skip to main content

Simplify the way I run scripts from the venv

ID
a8d3cf2
date
2023-11-26 02:25:15+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
e9dc23c
message
Simplify the way I run scripts from the venv
changed files
10 files, 29 additions, 91 deletions

Changed files

.with-venv-python/README.md (1554) → .with-venv-python/README.md (0)

diff --git a/.with-venv-python/README.md b/.with-venv-python/README.md
deleted file mode 100644
index d9dd8e5..0000000
--- a/.with-venv-python/README.md
+++ /dev/null
@@ -1,43 +0,0 @@
-This folder contains aliases for my Python scripts that causes them to run in a scripts-repo virtual environment, rather than my global Python.
-
-There's unlikely to be anything here which is useful to anyone but me.
-
-## Why?
-
-I try to run everything inside virtual environments, and not install any dependencies globally.
-I used to install my dependencies with the `--user` flag, but [Glyph's article](https://blog.glyph.im/2023/08/get-your-mac-python-from-python-dot-org.html) persuaded me to change my ways.
-For several reasons:
-
-*   Don't pollute my global Python installation
-*   Make it easier to track which library versions I'm using (there are some scripts I go for years on end without using, and it's annoying if I don't have a record of what library version I originally wrote them with)
-*   Better isolation of different projects
-
-I have a single virtualenv on my Mac for my scripts (`~/repos/scripts/.venv`).
-
-I could point to that Python in the shebang at the top of my scripts:
-
-```console
-$ cat myscript
-#!/Users/alexwlchan/repos/scripts/.venv/bin/python3
-"""
-This script is for ...
-```
-
-but that's quite fragile and makes the scripts less portable.
-
-So instead, I save the scripts as `[scriptname].py`, then create an alias `[scriptname]` in this folder which runs my script inside the virtual environment:
-
-```console
-$ cat myscript.py
-#!/usr/bin/env python3
-"""
-This script is for ...
-
-$ cat myscript
-#!/usr/bin/env bash
-
-set -o errexit
-set -o nounset
-
-~/repos/scripts/.venv/bin/python3 "~/repos/scripts/images/[myscript].py" "$@"
-```

.with-venv-python/emptydir (139) → .with-venv-python/emptydir (0)

diff --git a/.with-venv-python/emptydir b/.with-venv-python/emptydir
deleted file mode 100755
index 40cef13..0000000
--- a/.with-venv-python/emptydir
+++ /dev/null
@@ -1,8 +0,0 @@
-#!/usr/bin/env bash
-
-set -o errexit
-set -o nounset
-
-source ~/repos/scripts/.venv/bin/activate
-
-python3 ~/repos/scripts/fs/emptydir.py "$@"

.with-venv-python/flapi (137) → .with-venv-python/flapi (0)

diff --git a/.with-venv-python/flapi b/.with-venv-python/flapi
deleted file mode 100755
index 749c6b0..0000000
--- a/.with-venv-python/flapi
+++ /dev/null
@@ -1,8 +0,0 @@
-#!/usr/bin/env bash
-
-set -o errexit
-set -o nounset
-
-source ~/repos/scripts/.venv/bin/activate
-
-bash ~/repos/scripts/flickr/flapi.sh "$@"

.with-venv-python/flphoto (139) → .with-venv-python/flphoto (0)

diff --git a/.with-venv-python/flphoto b/.with-venv-python/flphoto
deleted file mode 100755
index 580adb9..0000000
--- a/.with-venv-python/flphoto
+++ /dev/null
@@ -1,8 +0,0 @@
-#!/usr/bin/env bash
-
-set -o errexit
-set -o nounset
-
-source ~/repos/scripts/.venv/bin/activate
-
-bash ~/repos/scripts/flickr/flphoto.sh "$@"

.with-venv-python/kn_cover_image (132) → .with-venv-python/kn_cover_image (0)

diff --git a/.with-venv-python/kn_cover_image b/.with-venv-python/kn_cover_image
deleted file mode 100755
index f631654..0000000
--- a/.with-venv-python/kn_cover_image
+++ /dev/null
@@ -1,6 +0,0 @@
-#!/usr/bin/env bash
-
-set -o errexit
-set -o nounset
-
-~/repos/scripts/.venv/bin/python3 ~/repos/scripts/images/kn_cover_image.py "$@"

.with-venv-python/noplaylist (126) → .with-venv-python/noplaylist (0)

diff --git a/.with-venv-python/noplaylist b/.with-venv-python/noplaylist
deleted file mode 100755
index ab0c3ca..0000000
--- a/.with-venv-python/noplaylist
+++ /dev/null
@@ -1,6 +0,0 @@
-#!/usr/bin/env bash
-
-set -o errexit
-set -o nounset
-
-~/repos/scripts/.venv/bin/python3 ~/repos/scripts/text/noplaylist.py "$@"

.with-venv-python/reborder (126) → .with-venv-python/reborder (0)

diff --git a/.with-venv-python/reborder b/.with-venv-python/reborder
deleted file mode 100755
index d2885ef..0000000
--- a/.with-venv-python/reborder
+++ /dev/null
@@ -1,6 +0,0 @@
-#!/usr/bin/env bash
-
-set -o errexit
-set -o nounset
-
-~/repos/scripts/.venv/bin/python3 ~/repos/scripts/images/reborder.py "$@"

.with-venv-python/srgbify (125) → .with-venv-python/srgbify (0)

diff --git a/.with-venv-python/srgbify b/.with-venv-python/srgbify
deleted file mode 100755
index d3a5cd4..0000000
--- a/.with-venv-python/srgbify
+++ /dev/null
@@ -1,6 +0,0 @@
-#!/usr/bin/env bash
-
-set -o errexit
-set -o nounset
-
-~/repos/scripts/.venv/bin/python3 ~/repos/scripts/images/srgbify.py "$@"

config.fish (2305) → config.fish (3299)

diff --git a/config.fish b/config.fish
index 76cd3e8..3aca2b6 100644
--- a/config.fish
+++ b/config.fish
@@ -6,9 +6,16 @@
 #
 set -g -x fish_greeting ''
 
+
+
 # This tells fish to find functions in my "fish_functions" directory.
 #
+# Note that we have to *prepend* the directory in this repo, so we
+# can override the built-in functions.  e.g. I want to use the definition
+# of `fish_prompt` from this repo, rather than the default versions
+#
 # See https://fishshell.com/docs/current/language.html#autoloading-functions
+#
 set -x fish_function_path ~/repos/scripts/fish_functions $fish_function_path
 
 
@@ -62,3 +69,23 @@ function __auto_source_venv --on-variable PWD --description "Activate/Deactivate
         deactivate
     end
 end
+
+
+
+# These aliases run scripts in this repo using the virtualenv, rather
+# than running them with system Python.
+#
+# e.g. emptydir.py relies on the `humanize` library.  That isn't installed
+# in my system Python, but it is installed in my `scripts` virtualenv.
+#
+function __run_in_scripts_venv
+    ~/repos/scripts/.venv/bin/python3 ~/repos/scripts/$argv[1] $argv[2..]
+end
+
+alias emptydir="__run_in_scripts_venv fs/emptydir.py"
+alias flapi="__run_in_scripts_venv flickr/flapi.sh"
+alias flphoto="__run_in_scripts_venv flickr/flphoto.sh"
+alias kn_cover_image="__run_in_scripts_venv images/kn_cover_image.py"
+alias noplaylist="__run_in_scripts_venv text/noplaylist.py"
+alias reborder="__run_in_scripts_venv images/reborder.py"
+alias srgbify="__run_in_scripts_venv images/srgbify.py"

fs/emptydir.py (1476) → fs/emptydir.py (1557)

diff --git a/fs/emptydir.py b/fs/emptydir.py
index 45f62a9..2854425 100755
--- a/fs/emptydir.py
+++ b/fs/emptydir.py
@@ -59,3 +59,5 @@ if __name__ == "__main__":
                 f"{humanize.intcomma(total_deleted)} directories deleted", "green"
             )
         )
+    else:
+        print(termcolor.colored("No empty directories found", "blue"))