generated from bazel-contrib/rules-template
-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathmirror_ruff.sh
executable file
·65 lines (56 loc) · 1.94 KB
/
mirror_ruff.sh
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
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
RELEASES=$(mktemp)
RAW=$(mktemp)
REPOSITORY=${1:-"astral-sh/ruff"}
JQ_FILTER=\
'map(
{
"key": .tag_name,
"value": .assets
| map(select((.name | contains("ruff-")) and (.name | contains("sha256") | not) ))
| map({
"key": .name | capture("ruff-(?<platform>.*)\\.(tar\\.gz|zip)") | .platform,
"value": (.browser_download_url + ".sha256"),
})
| from_entries
}
) | from_entries'
SHA256_FILTER=\
'
map(
select(.name == $tag)
| .assets
| map(.browser_download_url)[]
| select(endswith(".sha256"))
)[]
'
curl > $RELEASES \
--silent \
--header "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${REPOSITORY}/releases?per_page=1
jq "$JQ_FILTER" <$RELEASES >$RAW
# Combine the new versions with the existing ones.
# New versions should appear first, but existing content should overwrite new
CURRENT=$(mktemp)
python3 -c "import json; exec(open('$SCRIPT_DIR/ruff_versions.bzl').read()); print(json.dumps(RUFF_VERSIONS))" > $CURRENT
OUT=$(mktemp)
jq --slurp '.[0] * .[1]' $RAW $CURRENT > $OUT
FIXED=$(mktemp)
# Replace placeholder sha256 URLs with their content
for tag in $(jq -r 'keys | .[]' < $OUT); do
# Download checksums for this tag
# Note: this is wasteful; we will curl for sha256 files even if the CURRENT content already had resolved them
for sha256url in $(jq --arg tag $tag -r "$SHA256_FILTER" < $RELEASES); do
sha256=$(curl --silent --location $sha256url | awk '{print $1}')
jq ".[\"$tag\"] |= with_entries(.value = (if .value == \"$sha256url\" then \"$sha256\" else .value end))" < $OUT > $FIXED
mv $FIXED $OUT
done
done
# Overwrite the file with updated content
(
echo '"This file is automatically updated by mirror_ruff.sh"'
echo -n "RUFF_VERSIONS = "
cat $OUT
)>$SCRIPT_DIR/ruff_versions.bzl