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