From 6245fbcba0bbfc3be123bf50703e96a73d19f076 Mon Sep 17 00:00:00 2001 From: Q Date: Tue, 26 Nov 2024 20:09:34 +0200 Subject: [PATCH] copying from private repo --- av/jhead-process | 148 ++++++++++++++++++ bin/archive-to-datefolders | 1 + bin/cron-user-run | 1 + bin/gpg-decrypt | 1 + bin/gpg-encrypt | 1 + bin/jhead-process | 1 + bin/useve-runner | 1 + bin/zip2tar | 1 + files/archive-to-datefolders | 88 +++++++++++ files/gpg-decrypt | 31 ++++ files/gpg-encrypt | 31 ++++ files/zip2tar | 79 ++++++++++ shell/cron-user-run | 120 +++++++++++++++ shell/useve-runner | 280 +++++++++++++++++++++++++++++++++++ 14 files changed, 784 insertions(+) create mode 100755 av/jhead-process create mode 120000 bin/archive-to-datefolders create mode 120000 bin/cron-user-run create mode 120000 bin/gpg-decrypt create mode 120000 bin/gpg-encrypt create mode 120000 bin/jhead-process create mode 120000 bin/useve-runner create mode 120000 bin/zip2tar create mode 100755 files/archive-to-datefolders create mode 100755 files/gpg-decrypt create mode 100755 files/gpg-encrypt create mode 100755 files/zip2tar create mode 100755 shell/cron-user-run create mode 100755 shell/useve-runner diff --git a/av/jhead-process b/av/jhead-process new file mode 100755 index 0000000..ae57c7e --- /dev/null +++ b/av/jhead-process @@ -0,0 +1,148 @@ +#!/bin/bash +set -e + + +_help() { + self=$( readlink -f "$0" ) + grep " # " "$self" | grep -v [\)] + exit + # jhead processor: renames images and videos with dates, and moves to subfolders + # Give filenames as arguments e.x.: * + # +} + + +which jhead &> /dev/null || { + echo "jhead missing" + exit 1 +} + +function is_image() { + jhead "$1" &>/dev/null +} + +function gen_unique() { + if [[ ! -e "$1" ]]; then + echo "$1" + return + fi + pathdir=$( dirname "$1" ) + fullname="${1##*/}" + extension="${fullname##*.}" + filename="${fullname%.*}" + aZ=( {a..z} {A..Z} ) + idx=0 + while true; do + newname="${filename}.${aZ[$idx]}.${extension}" + idx=$(( idx + 1 )) + if [[ ! -e "$pathdir/$newname" ]]; then + echo "$pathdir/$newname" + return + fi + if [[ -z "${aZ[$idx]}" ]]; then + echo "Cannot find unique filename for: $1" >&2 + exit 1 + fi + done +} + +function image() { + newname=$( jhead -n%Y-%m-%d_%H%M%S "$1" ) + if [[ -z "$newname" ]]; then + newname="$1" + else + newname=${newname##*--> } + fi + if [[ ! -e "$newname" ]]; then + echo Unexpected filename "$newname" >&2 + exit 1 + fi + jhead -c -ft -autorot -cl "-" "$newname" + move_to_dir "$newname" +} + + +function move_to_dir() { + if [[ $DIRS -eq 0 ]]; then + return + fi + pathdir=$( dirname "$1" ) + fullname="${1##*/}" + date=$( date '+%Y-%m-%d' -d "$( date '+%Y/%m/%d %H:%M:%S' -r "$1" ) + 4" ) + mkdir -p "$pathdir/$date" + tgt_name=$( gen_unique "$pathdir/$date/$fullname" ) + mv -vi "$1" "$tgt_name" +} + + +function other() { + pathdir=$( dirname "$1" ) + fullname="${1##*/}" + try_date=$( video_date "$1" ) + [[ -n "$try_date" ]] && touch -d "$try_date" "$1" + dname=$( date '+%Y-%m-%d_%H%M%S' -d "$( date '+%Y/%m/%d %H:%M:%S' -r "$1" )" ) + tgt=$( gen_unique "$pathdir/${dname}.$fullname" ) + mv -v "$1" "$tgt" + move_to_dir "$tgt" +} + + +function video_date() { + ffprobe -hide_banner "$1" 2>&1 | grep creation_time | \ + head -n 1 | cut -d: -f2- | xargs -II date -d I 2>/dev/null +} + + +if [[ -z "$1" ]]; then + _help +fi + +DIRS=1 +ONLYDIRS=0 +for arg in "$@"; do +case "$arg" in + + --nodir) + # --nodir Do not move files to subfolders + DIRS=0 + ;; + --onlydir) + # --onlydir Move files to subfolders, but do not rename + ONLYDIRS=1 + DIRS=1 + ;; + --debug) + # --debug turn on set -x + set -x + ;; + --help) + # --help This help message + _help + ;; + -h) # This help message + _help + ;; +esac +done + +for file in "$@"; do + if [[ "$file" = "-"* ]]; then + continue + fi + if [[ -d "$file" ]]; then + continue + fi + if [[ $ONLYDIRS -eq 1 ]]; then + move_to_dir "$file" + continue + fi + if is_image "$file"; then + image "$file" + else + other "$file" + fi + +done + + + diff --git a/bin/archive-to-datefolders b/bin/archive-to-datefolders new file mode 120000 index 0000000..0874ee2 --- /dev/null +++ b/bin/archive-to-datefolders @@ -0,0 +1 @@ +../files/archive-to-datefolders \ No newline at end of file diff --git a/bin/cron-user-run b/bin/cron-user-run new file mode 120000 index 0000000..c11ec6a --- /dev/null +++ b/bin/cron-user-run @@ -0,0 +1 @@ +../shell/cron-user-run \ No newline at end of file diff --git a/bin/gpg-decrypt b/bin/gpg-decrypt new file mode 120000 index 0000000..c7bac99 --- /dev/null +++ b/bin/gpg-decrypt @@ -0,0 +1 @@ +../files/gpg-decrypt \ No newline at end of file diff --git a/bin/gpg-encrypt b/bin/gpg-encrypt new file mode 120000 index 0000000..117a7f3 --- /dev/null +++ b/bin/gpg-encrypt @@ -0,0 +1 @@ +../files/gpg-encrypt \ No newline at end of file diff --git a/bin/jhead-process b/bin/jhead-process new file mode 120000 index 0000000..c08fecf --- /dev/null +++ b/bin/jhead-process @@ -0,0 +1 @@ +../av/jhead-process \ No newline at end of file diff --git a/bin/useve-runner b/bin/useve-runner new file mode 120000 index 0000000..d8b7b44 --- /dev/null +++ b/bin/useve-runner @@ -0,0 +1 @@ +../shell/useve-runner \ No newline at end of file diff --git a/bin/zip2tar b/bin/zip2tar new file mode 120000 index 0000000..0b9a321 --- /dev/null +++ b/bin/zip2tar @@ -0,0 +1 @@ +../files/zip2tar \ No newline at end of file diff --git a/files/archive-to-datefolders b/files/archive-to-datefolders new file mode 100755 index 0000000..5bf6c89 --- /dev/null +++ b/files/archive-to-datefolders @@ -0,0 +1,88 @@ +#!/bin/bash +set -e + + +_help() { + self=$( readlink -f "$0" ) + grep " # " "$self" | grep -v [\)] + exit + # Moves files to subfolders YYYY-MM-DD/ + # Give filenames as arguments e.x.: * + # +} + + +function gen_unique() { + if [[ ! -e "$1" ]]; then + echo "$1" + return + fi + pathdir=$( dirname "$1" ) + fullname="${1##*/}" + extension="${fullname##*.}" + filename="${fullname%.*}" + aZ=( {a..z} {A..Z} ) + idx=0 + while true; do + newname="${filename}.${aZ[$idx]}.${extension}" + idx=$(( idx + 1 )) + if [[ ! -e "$pathdir/$newname" ]]; then + echo "$pathdir/$newname" + return + fi + if [[ -z "${aZ[$idx]}" ]]; then + echo "Cannot find unique filename for: $1" >&2 + exit 1 + fi + done +} + + +function move_to_dir() { + pathdir=$( dirname "$1" ) + fullname="${1##*/}" + date=$( find "$1" -printf '%TY-%Tm-%Td' ) + #date '+%Y-%m-%d' -d "$( date '+%Y/%m/%d %H:%M:%S' -r "$1" ) + 4" ) + mkdir -p "$pathdir/$date" + tgt_name=$( gen_unique "$pathdir/$date/$fullname" ) + mv -vi "$1" "$tgt_name" +} + + +if [[ -z "$1" ]]; then + _help +fi + +for arg in "$@"; do +case "$arg" in + --debug) + # --debug turn on set -x + set -x + ;; + --help) + # --help This help message + _help + ;; + -h) # This help message + _help + ;; +esac +done + +shopt -s nullglob + +for file in "$@"; do + if [[ "$file" = "-"* ]]; then + continue + fi + if [[ -d "$file" ]]; then + continue + fi + if [[ "$file" = "*" ]]; then + continue + fi + move_to_dir "$file" +done + + + diff --git a/files/gpg-decrypt b/files/gpg-decrypt new file mode 100755 index 0000000..b9c8e09 --- /dev/null +++ b/files/gpg-decrypt @@ -0,0 +1,31 @@ +#!/bin/bash + +if [[ -z "$1" ]]; then + echo "File missing or use -" + exit 1 +fi + +if [[ "$1" = "-" ]]; then + pv "$1" | gpg -d + exit $? +fi + +if [[ -e "${1%.gpg}" ]]; then + echo "Target already exists" + exit 1 +fi + +echo Creating "${1%.gpg}" +if [[ -z "$GPGPASS" ]]; then + pv "$1" | gpg -d -o "${1%.gpg}" || { + echo "" + echo "If agent doesnt work, then:" + echo "Give password with env GPGPASS ( GPGPASS=secret gpg-decrypt $1 )" + } +else + { + echo "$GPGPASS"; + pv "$1"; + } | \ + gpg -o "${1%.gpg}" -d --passphrase-fd 0 --batch --yes +fi diff --git a/files/gpg-encrypt b/files/gpg-encrypt new file mode 100755 index 0000000..2fabb35 --- /dev/null +++ b/files/gpg-encrypt @@ -0,0 +1,31 @@ +#!/bin/bash + +if [[ "$1" = "-" ]]; then + pv "$1" | gpg -c + exit $? +fi + +if [[ -z "$1" ]]; then + echo "File missing, or use -" + exit 1 +fi + +if [[ -e "$1".gpg ]]; then + echo "Target already exists" + exit 1 +fi + +echo Creating "$1".gpg +if [[ -z "$GPGPASS" ]]; then + pv "$1" | gpg --symmetric -o "$1".gpg || { + echo "" + echo "If agent doesnt work, then:" + echo "Give password with env GPGPASS ( GPGPASS=secret gpg-encrypt $1 )" + } +else + { + echo "$GPGPASS"; + pv "$1"; + } | \ + gpg -o "$1".gpg --passphrase-fd 0 --batch --yes --symmetric +fi diff --git a/files/zip2tar b/files/zip2tar new file mode 100755 index 0000000..a2ca0b2 --- /dev/null +++ b/files/zip2tar @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 + +"""zip2tar """ + +import argparse + +import sys +import os +from zipfile import ZipFile +import tarfile +import time + + +def get_opts(): + parser = argparse.ArgumentParser() + parser.add_argument( + "-q", + dest="quiet", + action="store_true", + default=False, + help="Quiet operation", + ) + parser.add_argument( + "-f", + dest="force", + action="store_true", + default=False, + help="Overwrite target", + ) + parser.add_argument( + "zip", + action="store", + help="Zip to convert", + ) + parser.add_argument( + "tar", + action="store", + help="Output filename, defaults to [zip].tgz. Recognizes extensios: .tar, .tgz, .gz, .bz2", + default=None, + nargs="?", + ) + + parsed = parser.parse_args() + if parsed.tar == None: + parsed.tar = os.path.splitext(parsed.zip)[0] + ".tgz" + return parsed + + +def main(): + opts = get_opts() + + if not opts.force and os.path.exists(opts.tar): + raise FileExistsError(opts.tar) + + mode = "w" + if opts.tar.endswith(".tgz") or opts.tar.endswith(".gz"): + mode = "w:gz" + if opts.tar.endswith(".bz2"): + mode = "w:bz2" + + with ZipFile(opts.zip) as zipf: + with tarfile.open(opts.tar, mode) as tarf: + for zip_info in zipf.infolist(): + if not opts.quiet: + print(zip_info.filename, zip_info.file_size) + + tar_info = tarfile.TarInfo(name=zip_info.filename) + tar_info.size = zip_info.file_size + tar_info.mtime = time.mktime(zip_info.date_time + (-1, -1, -1)) + if zip_info.is_dir(): + tar_info.type = tarfile.DIRTYPE + tarf.addfile(tarinfo=tar_info) + else: + tarf.addfile(tarinfo=tar_info, fileobj=zipf.open(zip_info.filename)) + + +if __name__ == "__main__": + + main() diff --git a/shell/cron-user-run b/shell/cron-user-run new file mode 100755 index 0000000..3a846ea --- /dev/null +++ b/shell/cron-user-run @@ -0,0 +1,120 @@ +#!/bin/bash + +_helpexit() { + echo "# Example cron:" + _get_cron_template + echo " +# Cron run, arg is one of: + reboot hourly daily weekly monthly all list help + all runs all targets but not reboot + " + exit 1 +} + +_get_cron_template() { + min=$(( $RANDOM % 20 )) + echo " +### CRON user run ### +@reboot $0 reboot +$min * * * * $0 hourly +$(( $min + 10 )) 4 * * * $0 daily +$(( $min + 20 )) 4 * * 1 $0 weekly +$(( $min + 30 )) 4 1 * * $0 monthly +### CRON user run END ###" +} + +_install() { + crontab -l 2>/dev/null | grep -q cron-user-run && { + echo "Already installed" + return + } + echo "Installing" + crontab -l 2>/dev/null + tmpfile=$( mktemp ) + crontab -l > "$tmpfile" 2>/dev/null + set -e + _get_cron_template >> "$tmpfile" + crontab "$tmpfile" + rm "$tmpfile" + crontab -l + return +} + +if [ -z "$1" ];then + _helpexit +fi +# SETUP +cd $HOME +LOG="$HOME/.log/cron.log" +TSF="%Y-%m-%d %H:%M:%S [$1]" +CRONBASE="$HOME/.local/etc/cron" +mkdir -p "$HOME/.log" +for task in hourly daily weekly monthly reboot; do + if [[ ! -e "$CRONBASE"/"$task" ]]; then + mkdir -p "$CRONBASE"/"$task" + fi +done +if [[ ! -e ~/.log/logrotate.conf ]]; then + cat < ~/.log/logrotate.conf +$HOME/.log/*.log { + monthly + missingok + rotate 12 + compress + delaycompress + notifempty +} +EOF +fi +if [[ ! -e "$CRONBASE"/daily/000-logrotate ]]; then + cat < "$CRONBASE"/daily/000-logrotate +#!/bin/bash + +/usr/sbin/logrotate -s ~/.log/logrotate.state ~/.log/logrotate.conf +EOF + chmod +x "$CRONBASE"/daily/000-logrotate +fi + +case "$1" in + help|-h|--help) + _helpexit + ;; + all) + for task in hourly daily weekly monthly; do + "$0" $task + done + exit $? + ;; + list) + if [[ -e "$LOG" ]]; then + echo "====================" + echo "Log: $LOG" + tail -n 10 "$LOG" + echo "====================" + fi + for task in hourly daily weekly monthly reboot; do + if [[ -e "$CRONBASE"."$task" ]]; then + echo "$CRONBASE"."$task" + ls -1 "$CRONBASE"."$task" | sed 's/^/ /' + fi + if [[ -e "$CRONBASE"/"$task" ]]; then + echo "$CRONBASE"/"$task" + ls -1 "$CRONBASE"/"$task" | sed 's/^/ /' + fi + done + exit $? + ;; + install) + _install + ;; + hourly|daily|weekly|monthly|reboot) + echo "Running user cron $1" | ts -m "$TSF" >> "$LOG" + if [[ -e "$CRONBASE"."$task" ]]; then + run-parts --report -v "$CRONBASE"."$1" 2>&1 | ts -m "$TSF" >> "$LOG" + fi + if [[ -e "$CRONBASE"/"$task" ]]; then + run-parts --report -v "$CRONBASE"/"$1" 2>&1 | ts -m "$TSF" >> "$LOG" + fi + ;; +esac + diff --git a/shell/useve-runner b/shell/useve-runner new file mode 100755 index 0000000..8a72e6b --- /dev/null +++ b/shell/useve-runner @@ -0,0 +1,280 @@ +#!/bin/bash + +# use as hashbang: +# #!/usr/bin/env -S useve-runner [venv-name] + + +export VENV_HOME=$HOME/.local/share/venvs/ + +function useve() { + + local USEVE_HELP='Wrapper to python3 venv, subcommands: + - setup install global environment + - [env] enters the env if exists + - ls [-v] list envs + - mk [env] [[pkgs../requirements.txt]] create virtualenv and install packages + - rm [env] remove virtualenv + - cd [env] to virtualenv + - w [env] enter (same as [env] + - add [env] [pkg/requirements.txt] add package(s) and add to requirements.txt + - req [env] list the requirements.txt file + - up [env] upgrade packages listed in requirements.txt + - reinstall [env] reinstall env using requirements.txt + - freeze [env] make a list of installed packages in freeze.txt + +Additional command: useve-runner [env] python_script.py +or as hashbang: #!/usr/bin/env -S useve-runner [env] +' + [[ -z "$1" ]] && { + echo -e "$USEVE_HELP" + + if which smenu &>/dev/null; then + local VE_CHOICE=$( + ls "$VENV_HOME" \ + | sort \ + | smenu -t 1 -a c:2,bu i:8,b -n 25 -m "Virtual envs" -W $'\n' + ) + if [[ -z $VE_CHOICE ]]; then + return 0 + fi + useve "$VE_CHOICE" + else + useve ls + fi + return + } + local CMD="$1" + shift 1 + case $CMD in + -h|help) + echo "$USEVE_HELP" + ;; + mk) + if [[ -z "$1" ]]; then + echo "Virtual env name required" + return 1 + fi + if [[ -e "$VENV_HOME"/"$1" ]]; then + echo "Virtual env already exists" + useve ls -v "$1" + return 1 + fi + # for p3.10.x python3 -m venv --copies --upgrade-deps "$VENV_HOME"/"$1" + python3 -m venv --copies "$VENV_HOME"/"$1" + . "$VENV_HOME"/"$1"/bin/activate + pip install -U pip + if [[ -n "$2" ]]; then + useve add "$@" + fi + ;; + rm) + if [[ -z "$1" ]]; then + echo "Virtual env name required" + return 1 + fi + if [[ ! -e "$VENV_HOME"/"$1" ]]; then + echo "No such virtual env '$1'" + return 1 + fi + du -sh "$VENV_HOME"/"$1" + if [[ -e "$VENV_HOME"/"$1"/freeze.txt ]]; then + cp -v "$VENV_HOME"/"$1"/freeze.txt "$VENV_HOME"/.backups/"$1"-freeze.txt + fi + if [[ -e "$VENV_HOME"/"$1"/requirements.txt ]]; then + cp -v "$VENV_HOME"/"$1"/requirements.txt "$VENV_HOME"/.backups/"$1"-requirements.txt + fi + find "$VENV_HOME"/"$1" > "$VENV_HOME"/.backups/"$1"-filelist.txt + rm -r "$VENV_HOME"/"$1" + ;; + ls) + local n + local bn + if [[ "$1" = "-v" ]]; then + printf '# Virtual envs\n\n' + for n in "$VENV_HOME"/*/; do + bn=$( basename "$n" ) + printf "\e[1;32m%15s \e[0m%4s %s %s\n" \ + "$bn" \ + "$( du -sh "$n" | awk '{ print $1}' )" \ + "$( date -I -r "$n" )" \ + "$( "$n"/bin/python -V ) " + done + else + for n in "$VENV_HOME"/*/; do + bn=$( basename "$n" ) + echo "$bn" + done + fi + return 0 + ;; + cd) + if [[ -z "$1" ]]; then + cd "$VENV_HOME" + return + fi + cd "$VENV_HOME"/"$1" + ;; + w) + . "$VENV_HOME"/"$1"/bin/activate + ;; + setup) + mkdir -p "$VENV_HOME" + mkdir -p "$VENV_HOME"/.backups + alias p=python3 + ;; + add) + if [[ -z "$1" ]]; then return; fi + local ALREADY_ACTIVE=0 + if [[ $( readlink -f "$VIRTUAL_ENV" ) = $( readlink -f "$VENV_HOME"/"$1" ) ]]; then + ALREADY_ACTIVE=1 + fi + . "$VENV_HOME"/"$1"/bin/activate || return + local VENV_ENV + VENV_ENV="$1" + shift 1 + local VENV_PKG + for VENV_PKG in "$@"; do + if [[ -f "$VENV_PKG" ]]; then + pip install -r "$VENV_PKG" && { + cat "$VENV_PKG" >> "$VENV_HOME/$VENV_ENV/requirements.txt"; + } || return + + else + pip install "$VENV_PKG" && { + echo "$VENV_PKG" >> "$VENV_HOME/$VENV_ENV/requirements.txt"; + } || return + fi + done + pip freeze > "$VENV_HOME/$VENV_ENV/freeze.txt" + if [[ "$ALREADY_ACTIVE" -eq 0 ]]; then + deactivate + fi + echo Current "$VENV_HOME/$VENV_ENV/requirements.txt" + cat "$VENV_HOME/$VENV_ENV/requirements.txt" + ;; + up) + if [[ -z "$1" ]]; then return; fi + local ALREADY_ACTIVE=0 + if [[ $( readlink -f "$VIRTUAL_ENV" ) = $( readlink -f "$VENV_HOME"/"$1" ) ]]; then + ALREADY_ACTIVE=1 + fi + . "$VENV_HOME"/"$1"/bin/activate || return + local pkg + while read pkg; do + if [[ "$pkg" = "#"* ]]; then continue; fi + if [[ -z $pkg ]]; then continue; fi + pip install --upgrade --upgrade-strategy eager "$pkg" + done < "$VENV_HOME/$1/requirements.txt" + pip freeze > "$VENV_HOME/$1/freeze.txt" + if [[ "$ALREADY_ACTIVE" -eq 0 ]]; then + deactivate + fi + echo Current "$VENV_HOME/$1/requirements.txt" + cat "$VENV_HOME/$1/requirements.txt" + ;; + freeze) + if [[ -z "$1" ]]; then return; fi + . "$VENV_HOME"/"$1"/bin/activate || return + pip freeze > "$VENV_HOME/$1/freeze.txt" + cat "$VENV_HOME/$1/freeze.txt" + deactivate + ;; + req) + if [[ -z "$1" ]]; then return; fi + if [[ -e "$VENV_HOME/$1/requirements.txt" ]]; then + echo "# $VENV_HOME/$1/requirements.txt:" + cat "$VENV_HOME/$1/requirements.txt" + else + echo '# No requirements file.' + return 1 + fi + ;; + reinstall) + if [[ -z "$1" ]]; then return; fi + if [[ ! -e "$VENV_HOME/$1/requirements.txt" ]]; then + echo No requirements to base reinstall on. + return 1 + fi + mkdir -p "$VENV_HOME/.reinstalls/" + cp -v "$VENV_HOME/$1/requirements.txt" "$VENV_HOME/.reinstalls/$1-requirements.txt" + useve rm "$1" + useve mk "$1" + cp -v "$VENV_HOME/.reinstalls/$1-requirements.txt" "$VENV_HOME/$1/requirements.txt" + useve up "$1" + ;; + exists) + if [[ -z "$1" ]]; then return 1; fi + test -d "$VENV_HOME"/"$1" + return $? + ;; + *) + if [[ -d "$VENV_HOME"/"$CMD" ]]; then + . "$VENV_HOME"/"$CMD"/bin/activate + else + echo No such environment, or recognized command + echo Environments: + useve ls + echo -e "\n$USEVE_HELP" + return 1 + fi + ;; + esac +} + +function localve () { + [[ -z "$1" ]] && { + local envs=( $( find . -maxdepth 3 -mindepth 2 -type f -wholename '*/bin/activate' -printf "%P\n" | sed 's,/bin/activate,,' ) ) + if [[ ${#envs[@]} -eq 1 ]]; then + localve ${envs[0]} + return $? + fi + } + + [[ -z "$1" ]] && { + { + LOCAL_ENVS=( $( ) ) + echo -e '# Virtual envs\n' + find . -maxdepth 3 -mindepth 2 -type f -wholename '*/bin/activate' -printf "* %P\n" | sed 's,/bin/activate,,' + } | HB_RULES='"\\*.*" "$G" "#.*" "$W"' highbeam + return 0 + } + test -f "$1"/bin/activate || { + echo "No such environment '$1', create and enter? y/n" | HB_RULES='"^.*" "$R"' highbeam + local VE_ANSWER + read -n 1 VE_ANSWER + [[ "$VE_ANSWER" = "y" ]] && { + python3 -m venv --copies --upgrade-deps "$1" + } + } + source "$1"/bin/activate && { + [[ $- == *i* ]] && { # Interactive + echo -e "\nEntered [[ $1 ]]\n" | HB_RULES='"^.*" "$G"' highbeam + echo "End with: deactivate" + } || { true; } + } +} + + + +if ! (return 0 2>/dev/null); then + # we are being executed + set -e + if [[ -z "$1" ]]; then + echo Virtualenv name missing + echo "Source this script to enable useve() function" + exit 1 + fi + if [[ ! -f "$VENV_HOME/$1/bin/activate" ]]; then + echo No such Virtualenv: "$1" + exit 1 + fi + source "$VENV_HOME/$1/bin/activate" + shift 1 + if [ -t 0 ]; then + exec python "$@" + else + cat - | python "$@" + fi +else + useve setup +fi