Skip to content

Instantly share code, notes, and snippets.

@alexvorobiev
Created November 5, 2021 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexvorobiev/aa5bcda1cb6639f36153b4aa4415b6dd to your computer and use it in GitHub Desktop.
Save alexvorobiev/aa5bcda1cb6639f36153b4aa4415b6dd to your computer and use it in GitHub Desktop.
Update default.nix for a python package by querying Pypi
#! /usr/bin/env nix-shell
#! nix-shell -i bash -p jq curl
set -o nounset
set -o errexit
d=$1
f="$1/default.nix"
pname=$(grep "^[ \t]\+pname = " "${f}" | sed 's/[ \t]\+pname = "\([^\"]\+\)";/\1/')
version=$(grep "^[ \t]\+version = " "${f}" | sed 's/[ \t]\+version = "\([^\"]\+\)";/\1/')
format=$(grep "^[ \t]\+format = " "${f}" | sed 's/[ \t]\+format = "\([^\"]\+\)";/\1/')
if [[ ${format} == "wheel" ]]; then
echo "wheel!"
pypi=$(curl -Ls "https://pypi.org/pypi/$pname/json" | \
jq -r '[.info.version, (.urls[] | select(.python_version == "py2.py3") | .digests.sha256)] | @tsv')
else
pypi=$(curl -Ls "https://pypi.org/pypi/$pname/json" | \
jq -r '[.info.version, (.urls[] | select(.python_version == "source") | .digests.sha256)] | @tsv')
fi
pypi_arr=(${pypi//\t/ })
version_new=${pypi_arr[0]}
sha256=${pypi_arr[1]}
echo "${pname} ${version} ${version_new}"
if [[ ${version} == ${version_new} ]]; then
echo "No updates"
else
echo "New sha256: ${sha256}"
mv "${f}" "${f}.orig"
sed "s/\([ \t]\+\)version = \"[^\"]\+\";/\1version = \"${version_new}\";/" "${f}.orig" | \
sed "s/\([ \t]\+\)sha256 = \"\([^\"]\+\)\";/\1sha256 = \"${sha256}\";/" > ${f}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment