repo-parse: remove unused import
[aurutils.git] / lib / aur-srcver
blobc895a0d4fc82cfcc132204c83731bf1a8de45c1a
1 #!/bin/bash
2 # aur-srcver - update and print package revisions
3 [[ -v AUR_DEBUG ]] && set -o xtrace
4 shopt -s nullglob
5 argv0=srcver
6 PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
8 # default arguments
9 # XXX: --skipinteg is an optional argument
10 makepkg_args=('--nobuild' '--nodeps' '--skipinteg')
11 num_procs=$(( "$(nproc)" + 2 ))
13 args_csv() {
14 # shellcheck disable=SC2155
15 local str=$(printf '%s,' "$@")
16 printf '%s' "${str%,}"
19 # shellcheck disable=SC2154
20 get_full_version() {
21 if (( epoch > 0 )); then
22 printf "%s\n" "$epoch:$pkgver-$pkgrel"
23 else
24 printf "%s\n" "$pkgver-$pkgrel"
28 trap_exit() {
29 if [[ ! -v AUR_DEBUG ]]; then
30 rm -rf -- "$tmp"
31 else
32 printf >&2 'AUR_DEBUG: %s: temporary files at %s\n' "$argv0" "$tmp"
36 usage() {
37 printf >&2 'usage: %s [--no-prepare] <pkgbase> [<pkgbase> ...]\n' "$argv0"
38 exit 1
41 # mollyguard for makepkg
42 if (( UID == 0 )) && [[ ! -v AUR_ASROOT ]]; then
43 printf >&2 'warning: aur-%s is not meant to be run as root.\n' "$argv0"
44 printf >&2 'warning: To proceed anyway, set the %s variable.\n' 'AUR_ASROOT'
45 exit 1
48 # option parsing
49 opt_short=a:j:z
50 opt_long=('arg-file:' 'no-prepare' 'jobs:' 'buildscript:' 'margs:' 'makepkg-args:' 'null')
51 opt_hidden=('dump-options' 'noprepare')
53 if opts=$(getopt -o "$opt_short" -l "$(args_csv "${opt_long[@]}" "${opt_hidden[@]}")" -n "$argv0" -- "$@"); then
54 eval set -- "$opts"
55 else
56 usage
59 unset arg_file buildscript mapfile_args
60 while true; do
61 case "$1" in
62 -a|--arg-file)
63 shift; arg_file=$1 ;;
64 --noprepare|--no-prepare)
65 makepkg_args+=(--noprepare) ;;
66 --makepkg-args|--margs)
67 shift; IFS=, read -a arg -r <<< "$1"
68 makepkg_args+=("${arg[@]}") ;;
69 -j|--jobs)
70 shift; num_procs=$1 ;;
71 --buildscript)
72 shift; buildscript=$1
73 makepkg_args+=(-p "$1") ;;
74 -z|--null)
75 mapfile_args+=(-d $'\0') ;;
76 --dump-options)
77 printf -- '--%s\n' "${opt_long[@]}" ${AUR_DEBUG+"${opt_hidden[@]}"}
78 printf -- '%s' "${opt_short}" | sed 's/.:\?/-&\n/g'
79 exit ;;
80 --)
81 shift; break ;;
82 esac
83 shift
84 done
86 # Single hyphen to denote input taken from stdin
87 stdin=0
88 if (( $# == 1 )) && [[ $1 == "-" || $1 == "/dev/stdin" ]]; then
89 stdin=1
92 # shellcheck disable=SC2174
93 mkdir -pm 0700 -- "${TMPDIR:-/tmp}/aurutils-$UID"
94 tmp=$(mktemp -d --tmpdir "aurutils-$UID/$argv0.XXXXXXXX") || exit
95 trap 'trap_exit' EXIT
97 # A pipeline `foo | bar &` causes `bar` to detach from the script. In
98 # this case, aur-srcver returns immediately with `makepkg` processes
99 # still running in the background. Read all input into an array to
100 # avoid this. (cf. aur-view, #958)
101 if (( stdin )); then
102 mapfile "${mapfile_args[@]}" -t packages
104 elif [[ -v argfile ]]; then
105 mapfile "${mapfile_args[@]}" -t packages <"$arg_file"
106 else
107 packages=("${@:-$PWD}")
108 set --
111 # Remove duplicates in pkgbuild dirs
112 declare -A seen
114 i=0 # package counter
115 for n in "${packages[@]}"; do
116 n=$(realpath "$n"); [[ ${seen[$n]} ]] && continue
117 seen[$n]=1
119 if (( i++ >= num_procs )); then
120 wait -n
123 { mkdir -p -- "$tmp/$i"
124 echo "$n" > "$tmp/$i"/path
125 mpkg_err=0
127 #shellcheck disable=SC2086
128 env -C "$n" nice -n 20 ${AUR_MAKEPKG:-makepkg} \
129 "${makepkg_args[@]}" >"$tmp/$i"/log 2>&1 || mpkg_err=$?
131 if (( mpkg_err )); then
132 echo "$mpkg_err" >"$tmp/$i"/failed
135 done
136 wait
138 declare -A failed
139 for d in "$tmp"/*/; do # iterate over directories
140 i=$(basename "$d") p=$(<"$d"/path)
142 if [[ -e $d/failed ]]; then
143 failed[$i]=$p
144 else
145 # Precautions when sourcing the PKGBUILD have no effect here,
146 # because makepkg already sourced the PKGBUILD above.
147 # shellcheck disable=SC1090
148 ( source "$p/${buildscript-PKGBUILD}"
150 fullver=$(get_full_version)
151 printf '%s\t%s\n' "${pkgbase:-$pkgname}" "$fullver"
154 done
156 for i in "${!failed[@]}"; do
157 printf >&2 '%s: makepkg %s failed for path %s with exit %s\n' \
158 "$argv0" "${makepkg_args[*]}" "${failed[$i]}" "$(<"$tmp/$i"/failed)"
160 cat "$tmp/$i"/log >&2
161 printf >&2 '8<----\n'
162 done
164 if (( ${#failed[@]} )); then
165 exit 1 # E_FAIL
168 # vim: set et sw=4 sts=4 ft=sh: