2023-10-03 05:41:24 +04:00
|
|
|
##@cooked comments # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
2023-10-20 10:30:12 +04:00
|
|
|
## Charger nulib et rendre disponible les modules bash, awk, php et python
|
2023-10-03 05:41:24 +04:00
|
|
|
##@cooked nocomments
|
|
|
|
# Ce fichier doit être sourcé en premier. Si ce fichier n'est pas sourcé, alors
|
2023-10-20 10:30:12 +04:00
|
|
|
# le répertoire nulib doit être disponible dans le répertoire du script qui
|
2023-10-03 05:41:24 +04:00
|
|
|
# inclue ce fichier.
|
|
|
|
# Une fois ce fichier sourcé, les autres modules peuvent être importés avec
|
|
|
|
# require:() e.g.
|
2023-10-20 10:30:12 +04:00
|
|
|
# source /etc/nulib.sh || exit 1
|
2023-10-03 05:41:24 +04:00
|
|
|
# require: other_modules
|
2023-10-20 10:30:12 +04:00
|
|
|
# ou pour une copie locale de nulib:
|
|
|
|
# source "$(dirname "$0")/nulib/load.sh" || exit 1
|
2023-10-03 05:41:24 +04:00
|
|
|
# require: other_modules
|
|
|
|
|
|
|
|
# vérifier version minimum de bash
|
|
|
|
if [ "x$BASH" = "x" ]; then
|
2023-10-20 10:30:12 +04:00
|
|
|
echo "ERROR: nulib: this script requires bash"
|
2023-10-03 05:41:24 +04:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
function eerror() { echo "ERROR: $*" 1>&2; }
|
|
|
|
function die() { [ $# -gt 0 ] && eerror "$*"; exit 1; }
|
|
|
|
function edie() { [ $# -gt 0 ] && eerror "$*"; return 1; }
|
|
|
|
function delpath() { local _qdir="${1//\//\\/}"; eval "export ${2:-PATH}; ${2:-PATH}"'="${'"${2:-PATH}"'#$1:}"; '"${2:-PATH}"'="${'"${2:-PATH}"'%:$1}"; '"${2:-PATH}"'="${'"${2:-PATH}"'//:$_qdir:/:}"; [ "$'"${2:-PATH}"'" == "$1" ] && '"${2:-PATH}"'='; }
|
|
|
|
function addpath() { local _qdir="${1//\//\\/}"; eval "export ${2:-PATH}; "'[ "${'"${2:-PATH}"'#$1:}" == "$'"${2:-PATH}"'" -a "${'"${2:-PATH}"'%:$1}" == "$'"${2:-PATH}"'" -a "${'"${2:-PATH}"'//:$_qdir:/:}" == "$'"${2:-PATH}"'" -a "$'"${2:-PATH}"'" != "$1" ] && '"${2:-PATH}"'="${'"${2:-PATH}"':+$'"${2:-PATH}"':}$1"'; }
|
|
|
|
function inspathm() { local _qdir="${1//\//\\/}"; eval "export ${2:-PATH}; "'[ "${'"${2:-PATH}"'#$1:}" == "$'"${2:-PATH}"'" -a "${'"${2:-PATH}"'%:$1}" == "$'"${2:-PATH}"'" -a "${'"${2:-PATH}"'//:$_qdir:/:}" == "$'"${2:-PATH}"'" -a "$'"${2:-PATH}"'" != "$1" ] && '"${2:-PATH}"'="$1${'"${2:-PATH}"':+:$'"${2:-PATH}"'}"'; }
|
|
|
|
function inspath() { delpath "$@"; inspathm "$@"; }
|
|
|
|
|
|
|
|
if [ ${BASH_VERSINFO[0]} -ge 5 -o \( ${BASH_VERSINFO[0]} -eq 4 -a ${BASH_VERSINFO[1]} -ge 1 \) ]; then :
|
2023-10-20 10:30:12 +04:00
|
|
|
elif [ -n "$NULIB_IGNORE_BASH_VERSION" ]; then :
|
|
|
|
else die "nulib: bash 4.1+ is required"
|
2023-10-03 05:41:24 +04:00
|
|
|
fi
|
|
|
|
|
2023-10-20 10:30:12 +04:00
|
|
|
# Calculer emplacement de nulib
|
|
|
|
NULIBDIR="@@dest@@"
|
|
|
|
if [ "$NULIBDIR" = "@@""dest""@@" ]; then
|
2023-10-03 05:41:24 +04:00
|
|
|
# La valeur "@@"dest"@@" n'est remplacée que dans la copie de ce script
|
|
|
|
# faite dans /etc. Sinon, il faut toujours faire le calcul. Cela permet de
|
|
|
|
# déplacer la librairie n'importe où sur le disque, ce qui est
|
|
|
|
# particulièrement intéressant quand on fait du déploiement.
|
2023-10-20 10:30:12 +04:00
|
|
|
NULIBDIR="${BASH_SOURCE[0]}"
|
|
|
|
if [ -f "$NULIBDIR" -a "$(basename -- "$NULIBDIR")" == load.sh ]; then
|
|
|
|
# Fichier sourcé depuis nulib/
|
|
|
|
NULIB_SOURCED=1
|
|
|
|
NULIBDIR="$(dirname -- "$NULIBDIR")"
|
|
|
|
elif [ -f "$NULIBDIR" -a "$(basename -- "$NULIBDIR")" == nulib.sh ]; then
|
|
|
|
# Fichier sourcé depuis nulib/bash/src
|
|
|
|
NULIB_SOURCED=1
|
|
|
|
NULIBDIR="$(dirname -- "$NULIBDIR")/../.."
|
2023-10-03 05:41:24 +04:00
|
|
|
else
|
|
|
|
# Fichier non sourcé. Tout exprimer par rapport au script courant
|
2023-10-20 10:30:12 +04:00
|
|
|
NULIB_SOURCED=
|
|
|
|
NULIBDIR="$(dirname -- "$0")"
|
|
|
|
if [ -d "$NULIBDIR/nulib" ]; then
|
|
|
|
NULIBDIR="$NULIBDIR/nulib"
|
|
|
|
elif [ -d "$NULIBDIR/lib/nulib" ]; then
|
|
|
|
NULIBDIR="$NULIBDIR/lib/nulib"
|
2023-10-03 05:41:24 +04:00
|
|
|
fi
|
|
|
|
fi
|
2023-10-20 10:30:12 +04:00
|
|
|
elif [ "${BASH_SOURCE[0]}" = /etc/nulib.sh ]; then
|
|
|
|
# Fichier chargé depuis /etc/nulib.sh
|
|
|
|
NULIB_SOURCED=1
|
2023-10-03 05:41:24 +04:00
|
|
|
fi
|
2023-10-20 10:30:12 +04:00
|
|
|
NULIBDIR="$(cd "$NULIBDIR" 2>/dev/null; pwd)"
|
|
|
|
NULIBDIRS=("$NULIBDIR/bash/src")
|
2023-10-03 05:41:24 +04:00
|
|
|
|
2023-10-20 10:30:12 +04:00
|
|
|
# marqueur pour vérifier que nulib a réellement été chargé. il faut avoir $NULIBINIT == $NULIBDIR
|
2023-10-03 05:41:24 +04:00
|
|
|
# utilisé par le module base qui doit pouvoir être inclus indépendamment
|
2023-10-20 10:30:12 +04:00
|
|
|
NULIBINIT="$NULIBDIR"
|
2023-10-03 05:41:24 +04:00
|
|
|
|
|
|
|
## Modules bash
|
2023-10-20 10:30:12 +04:00
|
|
|
NULIB_LOADED_MODULES=(nulib)
|
|
|
|
NULIB_DEFAULT_MODULES=(base pretty sysinfos)
|
2023-10-03 05:41:24 +04:00
|
|
|
|
|
|
|
# Si cette variable est non vide, require: recharge toujours le module, même
|
|
|
|
# s'il a déjà été chargé. Cette valeur n'est pas transitive: il faut toujours
|
|
|
|
# recharger explicitement tous les modules désirés
|
2023-10-20 10:30:12 +04:00
|
|
|
NULIB_FORCE_RELOAD=
|
2023-10-03 05:41:24 +04:00
|
|
|
|
2023-10-20 10:30:12 +04:00
|
|
|
function nulib__define_functions() {
|
|
|
|
function nulib_check_loaded() {
|
2023-10-03 05:41:24 +04:00
|
|
|
local module
|
2023-10-20 10:30:12 +04:00
|
|
|
for module in "${NULIB_LOADED_MODULES[@]}"; do
|
2023-10-03 05:41:24 +04:00
|
|
|
[ "$module" == "$1" ] && return 0
|
|
|
|
done
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
function module:() {
|
2023-10-20 10:30:12 +04:00
|
|
|
NULIB_MODULE="$1"
|
|
|
|
if ! nulib_check_loaded "$1"; then
|
|
|
|
NULIB_LOADED_MODULES+=("$1")
|
2023-10-03 05:41:24 +04:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
function function:() {
|
|
|
|
:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-10 23:35:15 +04:00
|
|
|
function nulib__load:() {
|
|
|
|
local nl__module nl__nulibdir nl__found
|
|
|
|
[ $# -gt 0 ] || set DEFAULTS
|
|
|
|
|
|
|
|
for nl__module in "$@"; do
|
|
|
|
nl__found=
|
|
|
|
for nl__nulibdir in "${NULIBDIRS[@]}"; do
|
|
|
|
if [ -f "$nl__nulibdir/$nl__module.sh" ]; then
|
|
|
|
source "$nl__nulibdir/$nl__module.sh" || die
|
|
|
|
nl__found=1
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
[ -n "$nl__found" ] || die "nulib: unable to find module $nl__module in (${NULIBDIRS[*]})"
|
|
|
|
done
|
|
|
|
}
|
2023-10-20 10:30:12 +04:00
|
|
|
function nulib__require:() {
|
|
|
|
local nr__module nr__nulibdir nr__found
|
2023-10-03 05:41:24 +04:00
|
|
|
[ $# -gt 0 ] || set DEFAULTS
|
|
|
|
|
|
|
|
# sauvegarder valeurs globales
|
2023-10-20 10:30:12 +04:00
|
|
|
local nr__orig_module="$NULIB_MODULE"
|
|
|
|
NULIB_MODULE=
|
2023-10-03 05:41:24 +04:00
|
|
|
|
|
|
|
# garder une copie de la valeur originale et casser la transitivité
|
2023-10-20 10:30:12 +04:00
|
|
|
local nr__force_reload="$NULIB_FORCE_RELOAD"
|
|
|
|
local NULIB_FORCE_RELOAD
|
2023-10-03 05:41:24 +04:00
|
|
|
|
|
|
|
for nr__module in "$@"; do
|
|
|
|
nr__found=
|
2023-10-20 10:30:12 +04:00
|
|
|
for nr__nulibdir in "${NULIBDIRS[@]}"; do
|
|
|
|
if [ -f "$nr__nulibdir/$nr__module.sh" ]; then
|
2023-10-03 05:41:24 +04:00
|
|
|
nr__found=1
|
2023-10-20 10:30:12 +04:00
|
|
|
if [ -n "$nr__force_reload" ] || ! nulib_check_loaded "$nr__module"; then
|
|
|
|
NULIB_LOADED_MODULES+=("$nr__module")
|
|
|
|
source "$nr__nulibdir/$nr__module.sh" || die
|
2023-10-03 05:41:24 +04:00
|
|
|
fi
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [ -z "$nr__found" -a "$nr__module" == DEFAULTS ]; then
|
2023-10-20 10:30:12 +04:00
|
|
|
for nr__module in "${NULIB_DEFAULT_MODULES[@]}"; do
|
|
|
|
if [ -f "$nr__nulibdir/$nr__module.sh" ]; then
|
2023-10-03 05:41:24 +04:00
|
|
|
nr__found=1
|
2023-10-20 10:30:12 +04:00
|
|
|
if [ -n "$nr__force_reload" ] || ! nulib_check_loaded "$nr__module"; then
|
|
|
|
NULIB_LOADED_MODULES+=("$nr__module")
|
|
|
|
source "$nr__nulibdir/$nr__module.sh" || die
|
2023-10-03 05:41:24 +04:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
2023-10-20 10:30:12 +04:00
|
|
|
[ -n "$nr__found" ] || die "nulib: unable to find module $nr__module in (${NULIBDIRS[*]})"
|
2023-10-03 05:41:24 +04:00
|
|
|
done
|
|
|
|
|
|
|
|
# restaurer valeurs globales
|
2023-10-20 10:30:12 +04:00
|
|
|
NULIB_MODULE="$nr__orig_module"
|
2023-10-03 05:41:24 +04:00
|
|
|
}
|
|
|
|
|
2024-02-10 23:35:15 +04:00
|
|
|
# désactiver set -x
|
|
|
|
NULIB__DISABLE_SET_X='local NULIB__SET_X; [ -z "$NULIB_NO_DISABLE_SET_X" ] && [[ $- == *x* ]] && { set +x; NULIB__SET_X=1; }'
|
|
|
|
NULIB__ENABLE_SET_X='[ -n "$NULIB__SET_X" ] && set -x'
|
|
|
|
# désactiver set -x de manière réentrante
|
|
|
|
NULIB__RDISABLE_SET_X='[ -z "$NULIB_NO_DISABLE_SET_X" ] && [[ $- == *x* ]] && { set +x; local NULIB_REQUIRE_SET_X=1; }; if [ -n "$NULIB_REQUIRE_SET_X" ]; then [ -n "$NULIB_REQUIRE_SET_X_RL1" ] || local NULIB_REQUIRE_SET_X_RL1; local NULIB_REQUIRE_SET_X_RL2=$RANDOM; [ -n "$NULIB_REQUIRE_SET_X_RL1" ] || NULIB_REQUIRE_SET_X_RL1=$NULIB_REQUIRE_SET_X_RL2; fi'
|
|
|
|
NULIB__RENABLE_SET_X='[ -n "$NULIB_REQUIRE_SET_X" -a "$NULIB_REQUIRE_SET_X_RL1" == "$NULIB_REQUIRE_SET_X_RL2" ] && set -x'
|
|
|
|
|
2023-10-03 05:41:24 +04:00
|
|
|
function require:() {
|
2024-02-10 23:35:15 +04:00
|
|
|
eval "$NULIB__RDISABLE_SET_X"
|
2023-10-20 10:30:12 +04:00
|
|
|
nulib__define_functions
|
|
|
|
nulib__require: "$@"
|
2024-02-10 23:35:15 +04:00
|
|
|
eval "$NULIB__RENABLE_SET_X"
|
2023-10-03 05:41:24 +04:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
## Autres modules
|
2023-10-20 10:30:12 +04:00
|
|
|
[ -d "$NULIBDIR/awk/src" ] && inspath "$NULIBDIR/awk/src" AWKPATH; export AWKPATH
|
|
|
|
[ -d "$NULIBDIR/python3/src" ] && inspath "$NULIBDIR/python3/src" PYTHONPATH; export PYTHONPATH
|
2023-10-03 05:41:24 +04:00
|
|
|
|
|
|
|
## Auto import DEFAULTS
|
2023-10-20 10:30:12 +04:00
|
|
|
nulib__define_functions
|
|
|
|
if [ -n "$NULIB_SOURCED" -a -z "$NULIB_NO_IMPORT_DEFAULTS" ]; then
|
2023-10-03 05:41:24 +04:00
|
|
|
require: DEFAULTS
|
|
|
|
fi
|