|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Packages optional patches from the newest Store-listed release per platform. |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +if [[ $# -ne 3 ]]; then |
| 6 | + printf 'usage: %s STORE_INDEX ZIP_DIRECTORY TARGET_VERSION\n' "$0" >&2 |
| 7 | + exit 2 |
| 8 | +fi |
| 9 | +index=$1 |
| 10 | +dist=$2 |
| 11 | +version=$3 |
| 12 | +plugin_id=io.github.lithe-idea.python-support |
| 13 | +max_zip=67108864 |
| 14 | +version_pattern='^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$' |
| 15 | +[[ "$version" =~ $version_pattern ]] |
| 16 | + |
| 17 | +scratch=$(mktemp -d) |
| 18 | +trap 'python3 -c "import shutil, sys; shutil.rmtree(sys.argv[1])" "$scratch"' EXIT |
| 19 | + |
| 20 | +shopt -s nullglob |
| 21 | +for target in "$dist"/python-support-"$version"-*.zip; do |
| 22 | + platform=${target##*/python-support-"$version"-} |
| 23 | + platform=${platform%.zip} |
| 24 | + [[ "$platform" =~ ^(darwin|linux|windows)-(arm64|x64)$ ]] || continue |
| 25 | + size=$(wc -c < "$target") |
| 26 | + (( size > 0 && size <= max_zip )) || continue |
| 27 | + |
| 28 | + # The index is already ordered newest first. Compare the numeric parts to |
| 29 | + # ignore a concurrent newer release and release reruns. |
| 30 | + base=$(jq -r --arg id "$plugin_id" --arg platform "$platform" --arg target "$version" ' |
| 31 | + def semver: split(".") | map(tonumber); |
| 32 | + first( |
| 33 | + .plugins[] | select(.id == $id) | .versions[] |
| 34 | + | select((.version | semver) < ($target | semver)) |
| 35 | + | .version as $version |
| 36 | + | .downloads[] | select(.platforms | index($platform)) |
| 37 | + | [$version, .downloadURL, .sha256] | @tsv |
| 38 | + ) // empty |
| 39 | + ' "$index") |
| 40 | + [[ -n "$base" ]] || continue |
| 41 | + IFS=$'\t' read -r previous url digest <<< "$base" |
| 42 | + if [[ ! "$previous" =~ $version_pattern || |
| 43 | + ! "$url" =~ ^https://github\.com/Lithe-IDEA/lithe-plugin-python/releases/download/[^/]+/[^/]+\.zip$ || |
| 44 | + ! "$digest" =~ ^[a-f0-9]{64}$ ]]; then |
| 45 | + printf 'Skipping delta for %s: invalid Store archive identity.\n' "$platform" >&2 |
| 46 | + continue |
| 47 | + fi |
| 48 | + if ! curl --fail --silent --show-error --location --proto '=https' --proto-redir '=https' \ |
| 49 | + --max-time 25 --max-filesize "$max_zip" -o "$scratch/base.zip" "$url" || |
| 50 | + ! actual=$(sha256sum "$scratch/base.zip") || [[ "${actual%% *}" != "$digest" ]]; then |
| 51 | + printf 'Skipping delta for %s: Store base could not be verified.\n' "$platform" >&2 |
| 52 | + continue |
| 53 | + fi |
| 54 | + |
| 55 | + patch="$scratch/patch.zst" |
| 56 | + if ! zstd -f -q -M128MB --patch-from="$scratch/base.zip" -o "$patch" -- "$target"; then |
| 57 | + printf 'Skipping delta for %s: compression failed.\n' "$platform" >&2 |
| 58 | + continue |
| 59 | + fi |
| 60 | + (( $(wc -c < "$patch") <= size * 3 / 4 )) || continue |
| 61 | + if ! zstd -q -dc -M128MB --patch-from="$scratch/base.zip" -- "$patch" | cmp -s "$target" -; then |
| 62 | + printf 'Skipping delta for %s: reconstruction did not match target.\n' "$platform" >&2 |
| 63 | + continue |
| 64 | + fi |
| 65 | + mv "$patch" "${target%.zip}.from-$previous.zip.zst" |
| 66 | +done |
0 commit comments