Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility Taskfile for checksumming, text replacement, and Python venv creation. #2

Merged
merged 2 commits into from
Jun 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions taskfiles/utils.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
version: "3"

tasks:
# ===
# CHECKSUM UTILS
# ===
compute-checksum:
desc: "Tries to compute a checksum for the given directory and output it to a file."
internal: true
silent: true
requires:
vars: ["DATA_DIR", "OUTPUT_FILE"]
cmds:
- >-
tar cf -
--directory "{{.DATA_DIR}}"
--group 0
--mtime "UTC 1970-01-01"
--numeric-owner
--owner 0
--sort name
. 2> /dev/null
| md5sum > {{.OUTPUT_FILE}}
# Ignore errors so that dependent tasks don't fail
ignore_error: true

validate-checksum:
desc: "Validates the checksum of the given directory matches the checksum in the given file, or
deletes the checksum file otherwise."
internal: true
silent: true
vars:
TMP_CHECKSUM_FILE: "{{.CHECKSUM_FILE}}.tmp"
requires:
vars: ["CHECKSUM_FILE", "DATA_DIR"]
cmds:
- task: "compute-checksum"
vars:
DATA_DIR: "{{.DATA_DIR}}"
OUTPUT_FILE: "{{.TMP_CHECKSUM_FILE}}"
- defer: "rm -f '{{.TMP_CHECKSUM_FILE}}'"
# Check that the directory exists and the checksum matches; otherwise delete the checksum file
- >-
(
test -d "{{.DATA_DIR}}"
&& diff -q '{{.TMP_CHECKSUM_FILE}}' '{{.CHECKSUM_FILE}}' 2> /dev/null
) || rm -f '{{.CHECKSUM_FILE}}'

# ===
# STRING UTILS
# ===
replace-text:
desc: "Replaces some text in a file using sed."
internal: true
requires:
vars: ["FILE_PATH", "SED_EXP"]
cmds:
- |-
# NOTE:
# 1. We can't use `sed -i` since `-i` has different syntax on Linux and macOS
# 2. We can't use `--regexp` instead of `-E` since `--regexp` is not supported on macOS
src="{{.FILE_PATH}}"
dst="{{.FILE_PATH}}.tmp"
sed -E '{{.SED_EXP}}' "${src}" > "${dst}"
mv "${dst}" "${src}"

# ===
# VENV UTILS
# ===
create-venv:
desc: "Creates a Python venv using the given requirements file."
internal: true
label: "create-venv-{{.LABEL}}"
requires:
vars: ["LABEL", "OUTPUT_DIR", "REQUIREMENTS_FILE"]
cmds:
- "rm -rf '{{.OUTPUT_DIR}}'"
- "python3 -m venv '{{.OUTPUT_DIR}}'"
# Remove calls to `hash` from the venv activation script since Task uses `gosh` rather than
# `bash`.
# NOTE: Older versions of Python's venv would only call `hash` if they detected the running
# shell was one that had the command, but that's not the case in newer versions.
- task: "replace-text"
vars:
FILE_PATH: "{{.OUTPUT_DIR}}/bin/activate"
SED_EXP: >-
s/^([[:space:]]*)hash[[:space:]]+.*/\1true/g
- |-
. "{{.OUTPUT_DIR}}/bin/activate"
pip3 install --upgrade pip
pip3 install --upgrade -r "{{.REQUIREMENTS_FILE}}"