#!/usr/bin/env bash # set -euo pipefail SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" ETC_DIR="${SCRIPT_DIR}/etc" ENV_FILE="${SCRIPT_DIR}/.env" [ -e "${ENV_FILE}" ] && . "${ENV_FILE}" # This is a manual mapping of versions of codes_and_flags.yaml from: # * https://github.com/UniversalDependencies/docs-automation/commits/master/codes_and_flags.yaml # (identified by commit hash) matched to the closest date of UD versions from: # * https://github.com/UniversalDependencies/docs declare -A VER_MAPPING VER_MAPPING["2.7"]="a5124b817c04ee6c8b01b80b2adad518b06aa9b4" VER_MAPPING["2.8"]="26d595cd549e904a4d49b62fccb33602c9d18eb8" VER_MAPPING["2.9"]="6167b04d6dc5d953f4007dcd75b5fbbf8c7645d6" VER_MAPPING["2.10"]="953b1c6e70836b7a64b764e24f578ca787cfd89c" VER_MAPPING["2.11"]="25ee28cd173c2fee5e3a7ad493a4c6e137e13f6b" VER_MAPPING["2.12"]="32a51d28787795eea1c2e11b3572087a3dc228ac" VER_MAPPING["2.13"]="6712e116f6fda486223e183f56ffb8921695d4a6" VER_MAPPING["2.14"]="dabe6c89701b6127b867e27c0256eb4f9dfa9d46" VER_MAPPING["2.15"]="b3b3d95333d603db6aede3601a3cf9929b3470ae" VER_MAPPING["2.16"]="bc0cc9b94112253e3ea1e00b2dbe3a46c5f53fe3" VER_MAPPING["2.17"]="e3847a587baa2a5a7345049b8b2c109ad2162c21" VER_MAPPING["2.18"]="e095c8dc229690bcb7fa73622b06ed62500e9115" VER_MAPPING["latest"]="e095c8dc229690bcb7fa73622b06ed62500e9115" ################### ############# # # USAGE # usage() { OUTPUT=${1:-"verbose"} echo "Usage: $0 [-o] [--ud-ver VER] [-a]" >&2 [ "${OUTPUT}" = "short" ] && exit 0 >&2 cat << EOF Download versions of * codes_and_flags.yaml a yaml file mapping UD (long) language names to metadata. For example: German: flag: DE lcode: de iso3: deu family: IE genus: Germanic Options: -h Print this help message -o|--online Enable online mode: (re-fetch files from GitHub) --ud-ver VER Download one version (default: UD_VER from .env) -a|--all Download all mapped versions EOF } # ###### ############ ################## ONLINE_MODE=false ALL_MODE=false TARGET_VER="${UD_VER:-2.17}" VALID_ARGS=$(getopt -o hoau:r: --long help,online,all,ud-ver:,rev: -- "$@") if [[ $? -ne 0 ]]; then usage "short" exit 1 fi eval set -- "$VALID_ARGS" while [ : ] do case "$1" in -o | --online) ONLINE_MODE=true; shift ;; -a | --all) ALL_MODE=true; shift ;; -u | --ud-ver | -r | --rev) TARGET_VER="$2"; shift 2 ;; -h | --help) usage; shift ; exit 0;; --) shift ; break ;; *) >&2 echo Unsupported option: $1; usage; exit 1;; esac done download_codes_and_flags() { local ver="$1" local git_hash="$2" local codes_and_flags_fn="${ETC_DIR}/codes_and_flags-${ver}.yaml" local codes_and_flags_raw_url="https://raw.githubusercontent.com/UniversalDependencies/docs-automation/${git_hash}/codes_and_flags.yaml" if [[ "${ONLINE_MODE}" != true ]] && [[ -e "${codes_and_flags_fn}" ]]; then return 0 fi if command -v curl >/dev/null 2>&1; then curl -fL --retry 3 --retry-delay 1 "${codes_and_flags_raw_url}" -o "${codes_and_flags_fn}" elif command -v wget >/dev/null 2>&1; then wget "${codes_and_flags_raw_url}" -O "${codes_and_flags_fn}" else python3 - "${codes_and_flags_raw_url}" "${codes_and_flags_fn}" <<'PY' import sys from urllib.request import urlretrieve urlretrieve(sys.argv[1], sys.argv[2]) PY fi } mkdir -p "${ETC_DIR}" declare -a TARGET_VERSIONS=() if [[ "${ALL_MODE}" == true ]]; then mapfile -t TARGET_VERSIONS < <(printf '%s\n' "${!VER_MAPPING[@]}" | awk '/^[0-9]+\.[0-9]+$/' | sort -V) if [[ -n "${VER_MAPPING[latest]:-}" ]]; then TARGET_VERSIONS+=("latest") fi else if [[ -z "${VER_MAPPING[${TARGET_VER}]:-}" ]]; then >&2 echo "Unsupported version: ${TARGET_VER}" >&2 echo "Available versions: $(printf '%s ' "${!VER_MAPPING[@]}" | sed 's/ $//')" exit 1 fi TARGET_VERSIONS+=("${TARGET_VER}") # Keep codes_and_flags-latest.yaml in sync when downloading a single version. if [[ "${TARGET_VER}" != "latest" ]] && [[ -n "${VER_MAPPING[latest]:-}" ]]; then TARGET_VERSIONS+=("latest") fi fi for ver in "${TARGET_VERSIONS[@]}"; do download_codes_and_flags "${ver}" "${VER_MAPPING[${ver}]}" done