update-luarocks: Several improvements

Changes:
- Fetches rocks and builds Nix expressions for them in parallel
- Passes 'maintainers' list to luarocks-nix
- Constructs the luarocks argument list more cleanly, by using an
  indexed array
- Made indentation consistent
This commit is contained in:
Alexei Robyn 2019-06-13 20:00:00 +10:00
parent b7e6161b4d
commit 46c6b27633

View file

@ -1,5 +1,5 @@
#!/usr/bin/env nix-shell
#!nix-shell -p nix-prefetch-scripts luarocks-nix -i bash
#!nix-shell -p parallel nix-prefetch-scripts luarocks-nix -i bash
# You'll likely want to use
# ``
@ -8,17 +8,15 @@
# to update all libraries in that folder.
# to debug, redirect stderr to stdout with 2>&1
# stop the script upon C-C
set -eu -o pipefail
CSV_FILE="maintainers/scripts/luarocks-packages.csv"
TMP_FILE="$(mktemp)"
exit_trap()
{
local lc="$BASH_COMMAND" rc=$?
test $rc -eq 0 || echo -e "*** error $rc: $lc.\nGenerated temporary file in $TMP_FILE" >&2
exit_trap() {
local lc="$BASH_COMMAND" rc=$?
test $rc -eq 0 || echo -e "*** error $rc: $lc.\nGenerated temporary file in $TMP_FILE" >&2
}
print_help() {
@ -37,19 +35,19 @@ fi
trap exit_trap EXIT
while getopts ":hc:" opt; do
case $opt in
case $opt in
h)
print_help
;;
print_help
;;
c)
echo "Loading package list from $OPTARG !" >&2
CSV_FILE="$OPTARG"
;;
echo "Loading package list from $OPTARG !" >&2
CSV_FILE="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
shift $((OPTIND-1))
echo "Invalid option: -$OPTARG" >&2
;;
esac
shift $((OPTIND - 1))
done
GENERATED_NIXFILE="$1"
@ -72,43 +70,61 @@ FOOTER="
/* GENERATED */
"
function convert_pkg () {
function convert_pkg() {
nix_pkg_name="$1"
lua_pkg_name="$2"
server="${3:+--only-server=$3}"
pkg_version="${4:-}"
lua_version="${5:+--lua-dir=$(nix path-info nixpkgs.$5)/bin}"
server="$3"
pkg_version="$4"
lua_version="$5"
maintainers="$6"
echo "looking at $lua_pkg_name (version $pkg_version) from server [$server]" >&2
cmd="luarocks nix $server $lua_version $lua_pkg_name $pkg_version"
echo "Running $cmd" >&2
drv="$nix_pkg_name = $($cmd)"
if [ $? -ne 0 ]; then
echo "Failed to convert $pkg" >&2
if [ "${nix_pkg_name:0:1}" == "#" ]; then
echo "Skipping comment ${*}" >&2
return
fi
if [ -z "$lua_pkg_name" ]; then
echo "Using nix_name as lua_pkg_name for '$nix_pkg_name'" >&2
lua_pkg_name="$nix_pkg_name"
fi
echo "Building expression for $lua_pkg_name (version $pkg_version) from server [$server]" >&2
luarocks_args=(nix)
if [[ -n $server ]]; then
luarocks_args+=("--only-server=$server")
fi
if [[ -n $maintainers ]]; then
luarocks_args+=("--maintainers=$maintainers")
fi
if [[ -n $lua_version ]]; then
luarocks_args+=("--lua-dir=$(nix path-info "nixpkgs.$lua_version")/bin")
fi
luarocks_args+=("$lua_pkg_name")
if [[ -n $pkg_version ]]; then
luarocks_args+=("$pkg_version")
fi
echo "Running 'luarocks ${luarocks_args[*]}'" >&2
if drv="$nix_pkg_name = $(luarocks "${luarocks_args[@]}")"; then
# echo "$drv" | tee -a "$TMP_FILE"
echo "$drv"
else
echo "$drv" | tee -a "$TMP_FILE"
echo "Failed to convert $nix_pkg_name" >&2
return 1
fi
}
# params needed when called via callPackage
echo "$HEADER" | tee "$TMP_FILE"
# list of packages with format
while IFS=, read -r nix_pkg_name lua_pkg_name server pkg_version luaversion
do
if [ "${nix_pkg_name:0:1}" == "#" ]; then
echo "Skipping comment ${nix_pkg_name}" >&2
continue
fi
if [ -z "$lua_pkg_name" ]; then
echo "Using nix_name as lua_pkg_name" >&2
lua_pkg_name="$nix_pkg_name"
fi
convert_pkg "$nix_pkg_name" "$lua_pkg_name" "$server" "$pkg_version" "$luaversion"
done < "$CSV_FILE"
export -f convert_pkg
export SHELL=bash
# Read each line in the csv file and run convert_pkg for each, in parallel
# 10 is a pretty arbitrary number of simultaneous jobs, but it is generally
# impolite to hit a webserver with *too* many simultaneous connections :)
parallel --group --keep-order --halt now,fail=1 --jobs 10 --colsep ',' convert_pkg {} <"$CSV_FILE" | tee -a "$TMP_FILE"
# close the set
echo "$FOOTER" | tee -a "$TMP_FILE"
cp "$TMP_FILE" "$GENERATED_NIXFILE"
# vim: set ts=4 sw=4 ft=sh: