# Interface for retrieving source and binary package metadata # # Copyright (C) 2012, 2019 Patrick McDermott # # This file is part of opkbuild. # # opkbuild 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 3 of the License, or # (at your option) any later version. # # opkbuild 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 opkbuild. If not, see . _OB_PACKAGE_DIR= _OB_PACKAGE_FORMAT= _OB_BINARY_PACKAGES= _OB_SOURCE_PARAMETERS= _OB_BINARY_PARAMETERS= ob_init_package() { local dir="${1}" shift 1 || _ob_abort _OB_PACKAGE_DIR="$(cd "${dir}" && pwd)" if [ -r "${_OB_PACKAGE_DIR}/format" ]; then case "$(cat "${_OB_PACKAGE_DIR}/format")" in 2.0) _OB_PACKAGE_FORMAT='2' ;; esac fi if [ -z "${_OB_PACKAGE_FORMAT}" ]; then _ob_error_msg 'unable_to_detect_package_format' return 1 fi _OB_BINARY_PACKAGES= return 0 } _ob_package_do() { local func="${1}" shift 1 || _ob_abort "_ob_${func}_${_OB_PACKAGE_FORMAT}" "${@}" || return ${?} return 0 } ob_parse_package_metadata() { local opt= local cache_file= local pkg= local pkg_clean= local param_name= local param_value= OPTIND=1 while getopts 'c:' opt; do case "${opt}" in c) cache_file="${OPTARG}" ;; ?) _ob_abort ;; esac done shift $(($OPTIND - 1)) # Load a previously saved cache file, if any. if [ -n "${cache_file}" -a -r "${cache_file}" ]; then if [ "${cache_file#*/}" != "${cache_file}" ]; then . "${cache_file}" else . "./${cache_file}" fi return 0 fi _ob_package_do 'parse_package_metadata' if [ -n "${cache_file}" ]; then >"${cache_file}" for param_name in ${_OB_SOURCE_PARAMETERS}; do param_value="$(ob_get_source_parameter \ "${param_name}" | sed "s/'/'\\\\''/g")" printf "%s='%s'\n" \ "_OB_SRCFIELD_${param_name}" "${param_value}" \ >>"${cache_file}" done for pkg in $(ob_get_binary_packages); do pkg_clean="$({ tr 'a-z' 'A-Z' | tr -dC 'A-Z0-9';} <<-EOF "${pkg}" EOF )" for param_name in ${_OB_BINARY_PARAMETERS}; do param_value="$(ob_get_binary_parameter "${pkg}" \ "${param_name}" | sed "s/'/'\\\\''/g")" printf "%s='%s'\n" \ "_OB_BINFIELD_${pkg_clean}_${param_name}" \ "${param_value}" \ >>"${cache_file}" done done fi return 0 } ob_get_binary_packages() { local opt= local host_arch= local host_plat= local pkg= local pkgs_clean= local pkg_clean= local pkgs= OPTIND=1 while getopts 'a:P:' opt; do case "${opt}" in a) host_arch="${OPTARG}" ;; P) host_plat="${OPTARG}" ;; ?) _ob_abort ;; esac done shift $(($OPTIND - 1)) # NB: If a source package lists no binary packages, this will be true each # call. if [ -z "${_OB_BINARY_PACKAGES}" ]; then _ob_package_do 'get_binary_packages' pkgs_clean=' ' for pkg in ${_OB_BINARY_PACKAGES}; do # Validate the name. if ! ob_validate_binary_name "${pkg}"; then _ob_warn_msg 'bad_binary_name' "${pkg}" continue fi # Make sure the "clean" name is unique. pkg_clean="$({ tr 'a-z' 'A-Z' | tr -dC 'A-Z0-9';} <<-EOF "${pkg}" EOF )" case "${pkgs_clean}" in *" ${pkg_clean} "*) _ob_warn_msg 'duplicate_clean_binary_name' \ "${pkg_clean}" continue esac pkgs_clean="${pkgs_clean}${pkg_clean} " _OB_BINARY_PACKAGES="${_OB_BINARY_PACKAGES} ${pkgs}" done fi pkgs='' for pkg in ${_OB_BINARY_PACKAGES}; do if [ -n "${host_arch}" ] && ! ob_arch_is_concerned \ "${host_arch}" "$(ob_get_binary_parameter \ "${pkg}" 'Architecture')"; then continue fi if [ -n "${host_plat}" ] && ! ob_plat_is_concerned \ "${host_plat}" "$(ob_get_binary_parameter \ "${pkg}" 'Platform')"; then continue fi pkgs="${pkgs} ${pkg}" done printf '%s ' ${pkgs} return 0 } ob_get_source_parameter() { local name="${1}" shift 1 || _ob_abort # Convert field name to uppercase and validate. case "${name}" in *[!A-Za-z0-9-]* | '') return 1 esac name="$(tr 'a-z-' 'A-Z_' <<-EOF ${name} EOF )" eval "printf '%s' \"\${_OB_SRCFIELD_${name}}\"" return 0 } ob_get_binary_parameter() { local package="${1}" local name="${2}" shift 2 || _ob_abort if ! ob_validate_binary_name "${package}"; then return 1 fi # Convert package name to its uppercase "clean" form. package="$({ tr 'a-z' 'A-Z' | tr -dC 'A-Z0-9'; } <<-EOF "${pkg}" EOF )" # Convert field name to uppercase and validate. case "${name}" in *[!A-Za-z0-9-]* | '') return 1 esac name="$(tr 'a-z-' 'A-Z_' <<-EOF ${name} EOF )" eval "printf '%s' \"\${_OB_BINFIELD_${package}_${name}}\"" return 0 } ob_get_doc_package() { _ob_package_do 'get_doc_package' || return ${?} return 0 } ob_get_doc_files() { local arch="${1}" local plat="${2}" shift 2 || _ob_abort _ob_package_do 'get_doc_files' "${arch}" "${plat}" || return ${?} return 0 } ob_summarize_package_arch() { # XXX: Stub. return 0 } ob_summarize_package_plat() { # XXX: Stub. return 0 } ob_set_package_substvars() { local pkg="${1}" shift 1 || _ob_abort _ob_package_do 'set_package_substvars' "${pkg}" || return ${?} return 0 } _ob_set_binary_packages() { local packages="${1}" shift 1 || _ob_abort _OB_BINARY_PACKAGES="${packages}" return 0 } _ob_set_source_parameter() { local name="${1}" local value="${2}" shift 2 || _ob_abort # Convert field name to uppercase and validate. case "${name}" in *[!A-Za-z0-9-]* | '') return 1 esac name="$(tr 'a-z-' 'A-Z_' <<-EOF ${name} EOF )" _OB_SOURCE_PARAMETERS="${_OB_SOURCE_PARAMETERS} ${name}" eval "_OB_SRCFIELD_${name}=\"\${value}\"" return 0 } _ob_set_binary_parameter() { local package="${1}" local name="${2}" local value="${3}" shift 3 || _ob_abort if ! ob_validate_binary_name "${package}"; then return 1 fi # Convert package name to its uppercase "clean" form. package="$({ tr 'a-z' 'A-Z' | tr -dC 'A-Z0-9'; } <<-EOF "${pkg}" EOF )" # Convert field name to uppercase and validate. case "${name}" in *[!A-Za-z0-9-]* | '') return 1 esac name="$(tr 'a-z-' 'A-Z_' <<-EOF ${name} EOF )" case " ${_OB_BINARY_PARAMETERS} " in *" ${name} "*) ;; *) _OB_BINARY_PARAMETERS="${_OB_BINARY_PARAMETERS} ${name}" ;; esac eval "_OB_BINFIELD_${package}_${name}=\"\${value}\"" return 0 }