1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-11-11 20:05:07 -05:00

libmakepkg: move functions for extracting pkgbuild attributes

Also rename some functions for clarity:
funcgrep -> grep_function
extract_global_var -> extract_global_variable
extract_function_var -> extract_function_variable
pkgbuild_get_attribute -> get_pkgbuild_attribute

Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
Allan McRae 2015-05-12 22:10:24 +10:00
parent d5536d3eb3
commit 83b734a103
3 changed files with 130 additions and 106 deletions

View File

@ -47,7 +47,8 @@ LIBMAKEPKGDIRS = \
LIBMAKEPKG = \ LIBMAKEPKG = \
libmakepkg/util/message.sh \ libmakepkg/util/message.sh \
libmakepkg/util/option.sh libmakepkg/util/option.sh \
libmakepkg/util/pkgbuild.sh
LIBMAKEPKG_IN = \ LIBMAKEPKG_IN = \
libmakepkg/lint_package.sh \ libmakepkg/lint_package.sh \

View File

@ -0,0 +1,111 @@
#!/bin/bash
#
# pkgbuild.sh - functions to extract information from PKGBUILD files
#
# Copyright (c) 2014-2015 Pacman Development Team <pacman-dev@archlinux.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
[ -n "$LIBMAKEPKG_UTIL_PKGBUILD_SH" ] && return
LIBMAKEPKG_UTIL_PKGBUILD_SH=1
have_function() {
declare -f "$1" >/dev/null
}
grep_function() {
{ declare -f "$1" || declare -f package; } 2>/dev/null | grep -E "$2"
}
array_build() {
local dest=$1 src=$2 i keys values
# it's an error to try to copy a value which doesn't exist.
declare -p "$2" &>/dev/null || return 1
# Build an array of the indicies of the source array.
eval "keys=(\"\${!$2[@]}\")"
# Clear the destination array
eval "$dest=()"
# Read values indirectly via their index. This approach gives us support
# for associative arrays, sparse arrays, and empty strings as elements.
for i in "${keys[@]}"; do
values+=("printf -v '$dest[$i]' %s \"\${$src[$i]}\";")
done
eval "${values[*]}"
}
extract_global_variable() {
# $1: variable name
# $2: multivalued
# $3: name of output var
local attr=$1 isarray=$2 outputvar=$3 ref
if (( isarray )); then
array_build ref "$attr"
[[ ${ref[@]} ]] && array_build "$outputvar" "$attr"
else
[[ ${!attr} ]] && printf -v "$outputvar" %s "${!attr}"
fi
}
extract_function_variable() {
# $1: function name
# $2: variable name
# $3: multivalued
# $4: name of output var
local funcname=$1 attr=$2 isarray=$3 outputvar=$4 attr_regex= decl= r=1
if (( isarray )); then
printf -v attr_regex '^[[:space:]]* %s\+?=\(' "$2"
else
printf -v attr_regex '^[[:space:]]* %s\+?=[^(]' "$2"
fi
while read -r; do
# strip leading whitespace and any usage of declare
decl=${REPLY##*([[:space:]])}
eval "${decl/#$attr/$outputvar}"
# entering this loop at all means we found a match, so notify the caller.
r=0
done < <(grep_function "$funcname" "$attr_regex")
return $r
}
get_pkgbuild_attribute() {
# $1: package name
# $2: attribute name
# $3: multivalued
# $4: name of output var
local pkgname=$1 attrname=$2 isarray=$3 outputvar=$4
printf -v "$outputvar" %s ''
if [[ $pkgname ]]; then
extract_global_variable "$attrname" "$isarray" "$outputvar"
extract_function_variable "package_$pkgname" "$attrname" "$isarray" "$outputvar"
else
extract_global_variable "$attrname" "$isarray" "$outputvar"
fi
}

View File

@ -879,7 +879,7 @@ get_pkg_arch() {
fi fi
else else
local arch_override local arch_override
pkgbuild_get_attribute "$1" arch 1 arch_override get_pkgbuild_attribute "$1" arch 1 arch_override
(( ${#arch_override[@]} == 0 )) && arch_override=("${arch[@]}") (( ${#arch_override[@]} == 0 )) && arch_override=("${arch[@]}")
if [[ $arch_override = "any" ]]; then if [[ $arch_override = "any" ]]; then
printf "%s\n" "any" printf "%s\n" "any"
@ -1759,7 +1759,7 @@ pkgbuild_extract_to_srcinfo() {
local pkgname=$1 attrname=$2 isarray=$3 outvalue= local pkgname=$1 attrname=$2 isarray=$3 outvalue=
if pkgbuild_get_attribute "$pkgname" "$attrname" "$isarray" 'outvalue'; then if get_pkgbuild_attribute "$pkgname" "$attrname" "$isarray" 'outvalue'; then
srcinfo_write_attr "$attrname" "${outvalue[@]}" srcinfo_write_attr "$attrname" "${outvalue[@]}"
fi fi
} }
@ -1778,7 +1778,7 @@ srcinfo_write_section_details() {
pkgbuild_extract_to_srcinfo "$1" "$attr" 1 pkgbuild_extract_to_srcinfo "$1" "$attr" 1
done done
pkgbuild_get_attribute "$1" 'arch' 1 'package_arch' get_pkgbuild_attribute "$1" 'arch' 1 'package_arch'
for a in "${package_arch[@]}"; do for a in "${package_arch[@]}"; do
# 'any' is special. there's no support for, e.g. depends_any. # 'any' is special. there's no support for, e.g. depends_any.
[[ $a = any ]] && continue [[ $a = any ]] && continue
@ -2062,7 +2062,7 @@ create_srcpackage() {
[[ ${!i} ]] && files+=("${!i}") [[ ${!i} ]] && files+=("${!i}")
for name in "${pkgname[@]}"; do for name in "${pkgname[@]}"; do
if extract_function_var "package_$name" "$i" 0 file; then if extract_function_variable "package_$name" "$i" 0 file; then
files+=("$file") files+=("$file")
fi fi
done done
@ -2149,94 +2149,6 @@ install_package() {
fi fi
} }
have_function() {
declare -f "$1" >/dev/null
}
array_build() {
local dest=$1 src=$2 i keys values
# it's an error to try to copy a value which doesn't exist.
declare -p "$2" &>/dev/null || return 1
# Build an array of the indicies of the source array.
eval "keys=(\"\${!$2[@]}\")"
# Clear the destination array
eval "$dest=()"
# Read values indirectly via their index. This approach gives us support
# for associative arrays, sparse arrays, and empty strings as elements.
for i in "${keys[@]}"; do
values+=("printf -v '$dest[$i]' %s \"\${$src[$i]}\";")
done
eval "${values[*]}"
}
funcgrep() {
{ declare -f "$1" || declare -f package; } 2>/dev/null | grep -E "$2"
}
extract_global_var() {
# $1: variable name
# $2: multivalued
# $3: name of output var
local attr=$1 isarray=$2 outputvar=$3 ref
if (( isarray )); then
array_build ref "$attr"
[[ ${ref[@]} ]] && array_build "$outputvar" "$attr"
else
[[ ${!attr} ]] && printf -v "$outputvar" %s "${!attr}"
fi
}
extract_function_var() {
# $1: function name
# $2: variable name
# $3: multivalued
# $4: name of output var
local funcname=$1 attr=$2 isarray=$3 outputvar=$4 attr_regex= decl= r=1
if (( isarray )); then
printf -v attr_regex '^[[:space:]]* %s\+?=\(' "$2"
else
printf -v attr_regex '^[[:space:]]* %s\+?=[^(]' "$2"
fi
while read -r; do
# strip leading whitespace and any usage of declare
decl=${REPLY##*([[:space:]])}
eval "${decl/#$attr/$outputvar}"
# entering this loop at all means we found a match, so notify the caller.
r=0
done < <(funcgrep "$funcname" "$attr_regex")
return $r
}
pkgbuild_get_attribute() {
# $1: package name
# $2: attribute name
# $3: multivalued
# $4: name of output var
local pkgname=$1 attrname=$2 isarray=$3 outputvar=$4
printf -v "$outputvar" %s ''
if [[ $pkgname ]]; then
extract_global_var "$attrname" "$isarray" "$outputvar"
extract_function_var "package_$pkgname" "$attrname" "$isarray" "$outputvar"
else
extract_global_var "$attrname" "$isarray" "$outputvar"
fi
}
lint_pkgbase() { lint_pkgbase() {
if [[ ${pkgbase:0:1} = "-" ]]; then if [[ ${pkgbase:0:1} = "-" ]]; then
error "$(gettext "%s is not allowed to start with a hyphen.")" "pkgname" error "$(gettext "%s is not allowed to start with a hyphen.")" "pkgname"
@ -2322,7 +2234,7 @@ lint_arch() {
fi fi
for name in "${pkgname[@]}"; do for name in "${pkgname[@]}"; do
pkgbuild_get_attribute "$name" 'arch' 1 list get_pkgbuild_attribute "$name" 'arch' 1 list
if [[ $list && $list != 'any' ]] && ! in_array $CARCH "${list[@]}"; then if [[ $list && $list != 'any' ]] && ! in_array $CARCH "${list[@]}"; then
if (( ! IGNOREARCH )); then if (( ! IGNOREARCH )); then
error "$(gettext "%s is not available for the '%s' architecture.")" "$name" "$CARCH" error "$(gettext "%s is not available for the '%s' architecture.")" "$name" "$CARCH"
@ -2342,12 +2254,12 @@ lint_provides() {
done done
for name in "${pkgname[@]}"; do for name in "${pkgname[@]}"; do
if extract_function_var "package_$name" provides 1 list; then if extract_function_variable "package_$name" provides 1 list; then
provides_list+=("${list[@]}") provides_list+=("${list[@]}")
fi fi
for a in "${arch[@]}"; do for a in "${arch[@]}"; do
if extract_function_var "package_$name" "provides_$a" 1 list; then if extract_function_variable "package_$name" "provides_$a" 1 list; then
provides_list+=("${list[@]}") provides_list+=("${list[@]}")
fi fi
done done
@ -2368,7 +2280,7 @@ lint_backup() {
backup_list=("${backup[@]}") backup_list=("${backup[@]}")
for name in "${pkgname[@]}"; do for name in "${pkgname[@]}"; do
if extract_function_var "package_$name" backup 1 list; then if extract_function_variable "package_$name" backup 1 list; then
backup_list+=("${list[@]}") backup_list+=("${list[@]}")
fi fi
done done
@ -2393,12 +2305,12 @@ lint_optdepends() {
done done
for name in "${pkgname[@]}"; do for name in "${pkgname[@]}"; do
if extract_function_var "package_$name" optdepends 1 list; then if extract_function_variable "package_$name" optdepends 1 list; then
optdepends_list+=("${list[@]}") optdepends_list+=("${list[@]}")
fi fi
for a in "${arch[@]}"; do for a in "${arch[@]}"; do
if extract_function_var "package_$name" "optdepends_$a" 1 list; then if extract_function_variable "package_$name" "optdepends_$a" 1 list; then
optdepends_list+=("${list[@]}") optdepends_list+=("${list[@]}")
fi fi
done done
@ -2435,7 +2347,7 @@ lint_install() {
install_list=("${install[@]}") install_list=("${install[@]}")
for name in "${pkgname[@]}"; do for name in "${pkgname[@]}"; do
extract_function_var "package_$name" install 0 file extract_function_variable "package_$name" install 0 file
install_list+=("$file") install_list+=("$file")
done done
@ -2447,7 +2359,7 @@ lint_changelog() {
changelog_list=("${changelog[@]}") changelog_list=("${changelog[@]}")
for name in "${pkgname[@]}"; do for name in "${pkgname[@]}"; do
if extract_function_var "package_$name" changelog 0 file; then if extract_function_variable "package_$name" changelog 0 file; then
changelog_list+=("$file") changelog_list+=("$file")
fi fi
done done
@ -2460,7 +2372,7 @@ lint_options() {
options_list=("${options[@]}") options_list=("${options[@]}")
for name in "${pkgname[@]}"; do for name in "${pkgname[@]}"; do
if extract_function_var "package_$name" options 1 list; then if extract_function_variable "package_$name" options 1 list; then
options_list+=("${list[@]}") options_list+=("${list[@]}")
fi fi
done done
@ -2606,10 +2518,10 @@ check_vcs_software() {
# we currently only use global depends/makedepends arrays for --syncdeps # we currently only use global depends/makedepends arrays for --syncdeps
for attr in depends makedepends; do for attr in depends makedepends; do
pkgbuild_get_attribute "$pkg" "$attr" 1 'deps' get_pkgbuild_attribute "$pkg" "$attr" 1 'deps'
all_deps+=("${deps[@]}") all_deps+=("${deps[@]}")
pkgbuild_get_attribute "$pkg" "${attr}_$CARCH" 1 'deps' get_pkgbuild_attribute "$pkg" "${attr}_$CARCH" 1 'deps'
all_deps+=("${deps[@]}") all_deps+=("${deps[@]}")
done done
@ -2838,8 +2750,8 @@ print_all_package_names() {
local version=$(get_full_version) local version=$(get_full_version)
local architecture pkg opts a local architecture pkg opts a
for pkg in ${pkgname[@]}; do for pkg in ${pkgname[@]}; do
pkgbuild_get_attribute "$pkg" 'arch' 1 architecture get_pkgbuild_attribute "$pkg" 'arch' 1 architecture
pkgbuild_get_attribute "$pkg" 'options' 1 opts get_pkgbuild_attribute "$pkg" 'options' 1 opts
for a in ${architecture[@]}; do for a in ${architecture[@]}; do
printf "%s-%s-%s\n" "$pkg" "$version" "$a" printf "%s-%s-%s\n" "$pkg" "$version" "$a"
if in_opt_array "debug" ${opts[@]} && in_opt_array "strip" ${opts[@]}; then if in_opt_array "debug" ${opts[@]} && in_opt_array "strip" ${opts[@]}; then