Spaces:
Running
Running
File size: 4,735 Bytes
d1ffd83 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | #!/usr/bin/env bash
# =============================================================================
# fvc_env.sh — shared environment and path resolution for scripts/*.sh
# =============================================================================
# Source this at the top of any driver script, immediately after `set -euo
# pipefail`:
#
# source "$(dirname "${BASH_SOURCE[0]}")/lib/fvc_env.sh"
#
# It provides:
#
# FVC_REPO_ROOT absolute path to the repository checkout
# PYTHON_BIN the interpreter to run project code with
# FVC_ROOT_<NAME> every root from configs/paths.yaml (DATA, QGIS, UAS, ...)
# FVC_SITE_<NAME> every site from configs/paths.yaml (SITE1, SITE2, ...)
# fvc_layout resolve a layout template, e.g.
# fvc_layout predictors site=site1 grid=3072
# fvc_require_dir assert a directory exists, with an actionable message
#
# Roots come from configs/paths.yaml, so the shell scripts and the Python code
# read the same declarations. Override any of them per run:
#
# FVC_DATA_ROOT=/mnt/disk/calperumResearch bash scripts/<script>.sh
# =============================================================================
# Interpreter resolution must happen before Python can be used, so the
# candidates live here rather than in configs/paths.yaml.
# Order: explicit override, active conda/mamba env, known install paths, PATH.
_fvc_resolve_python() {
local candidate
for candidate in \
"${ENV_PYTHON:-}" \
"${PYTHON_BIN:-}" \
"${CONDA_PREFIX:+${CONDA_PREFIX}/bin/python}" \
"${HOME}/.local/share/mamba/envs/fvc_function/bin/python" \
"/opt/conda/bin/python" \
"/opt/conda/envs/fvc_function/bin/python"
do
if [[ -n "${candidate}" && -x "${candidate}" ]]; then
printf '%s\n' "${candidate}"
return 0
fi
done
if command -v python3 >/dev/null 2>&1; then
command -v python3
return 0
fi
if command -v python >/dev/null 2>&1; then
command -v python
return 0
fi
echo "fvc_env: no Python interpreter found. Activate the environment" >&2
echo " (mamba activate fvc_function) or set ENV_PYTHON=/path/to/python" >&2
return 1
}
FVC_REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
export FVC_REPO_ROOT
PYTHON_BIN="$(_fvc_resolve_python)" || exit 1
export PYTHON_BIN
# Kept for scripts that still reference ENV_PYTHON.
ENV_PYTHON="${ENV_PYTHON:-${PYTHON_BIN}}"
export ENV_PYTHON
_FVC_PATHS_PY="${FVC_REPO_ROOT}/utils/fvc_paths.py"
if [[ ! -f "${_FVC_PATHS_PY}" ]]; then
echo "fvc_env: missing ${_FVC_PATHS_PY}" >&2
exit 1
fi
# Resolve every root and site in a single interpreter start, rather than
# spawning Python once per lookup.
if ! _fvc_exports="$("${PYTHON_BIN}" "${_FVC_PATHS_PY}" --shell)"; then
echo "fvc_env: could not read configs/paths.yaml" >&2
echo " run: ${PYTHON_BIN} ${_FVC_PATHS_PY} --check" >&2
exit 1
fi
eval "${_fvc_exports}"
unset _fvc_exports
# Export the resolved names so child processes inherit them.
for _fvc_var in $(compgen -v FVC_ROOT_) $(compgen -v FVC_SITE_); do
export "${_fvc_var?}"
done
unset _fvc_var
# ---------------------------------------------------------------------------
# fvc_layout <template> [key=value ...]
#
# GRID_DIR="$(fvc_layout gridding site=site1 grid=3072)"
# OUT_DIR="$(fvc_layout outputs site=site1 density=medium)"
#
# Templates are declared under `layout:` in configs/paths.yaml.
# ---------------------------------------------------------------------------
fvc_layout() {
local template="$1"; shift
local args=(--layout "${template}")
local kv
for kv in "$@"; do
args+=(--set "${kv}")
done
"${PYTHON_BIN}" "${_FVC_PATHS_PY}" "${args[@]}"
}
# ---------------------------------------------------------------------------
# fvc_require_dir <path> [description]
#
# Fail early with a message that points at the config, instead of letting a
# missing external drive surface as a confusing error deep in a Python run.
# ---------------------------------------------------------------------------
fvc_require_dir() {
local path="$1"
local what="${2:-directory}"
if [[ ! -d "${path}" ]]; then
echo "fvc_env: missing ${what}: ${path}" >&2
echo " check the drive is mounted, then run:" >&2
echo " ${PYTHON_BIN} ${_FVC_PATHS_PY} --check" >&2
return 1
fi
}
# ---------------------------------------------------------------------------
# fvc_require_file <path> [description]
# ---------------------------------------------------------------------------
fvc_require_file() {
local path="$1"
local what="${2:-file}"
if [[ ! -f "${path}" ]]; then
echo "fvc_env: missing ${what}: ${path}" >&2
return 1
fi
}
|