diff options
author | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2020-07-05 23:18:12 (EDT) |
---|---|---|
committer | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2020-07-05 23:25:52 (EDT) |
commit | 984ec34f0ab73a8558ad417db3b1f9651029a46f (patch) | |
tree | 6c88a9d25ad2f6e221d36a43200e50db384d7910 | |
parent | a53880a6e630d05c72a2a9b32f748d92cd13d973 (diff) |
ob_get_binary_packages(): Factor out side effects
As described in the documentation, it was only pure after being called
once outside a subshell, but it was never actually called outside a
subshell. So it rescanned the file system for binary packages every
time.
-rw-r--r-- | NEWS | 6 | ||||
-rw-r--r-- | lib/package.sh | 75 |
2 files changed, 41 insertions, 40 deletions
@@ -41,6 +41,8 @@ Utilities: * Source package names are now prefixed with "src:" instead of "src-". pro-archman has supported this in its package name hashing since version 2.0.0. + * With ob_get_binary_packages() becoming always pure (see below), all + utilities now scan the file system for binary packages only once. libopkbuild: @@ -52,6 +54,10 @@ libopkbuild: correctly. * New functions ob_validate_section() and ob_qualify_package_name() have been added. + * ob_get_binary_packages() is now always pure (without side effects + that would preclude use in a subshell environment). Side effects + have been factored out into a new internal function called by + ob_init_package(). opkbuild version 4.1.4 ---------------------- diff --git a/lib/package.sh b/lib/package.sh index da6d688..df129c7 100644 --- a/lib/package.sh +++ b/lib/package.sh @@ -21,7 +21,6 @@ _ob_package_dir= _ob_package_format_full= _ob_package_format= _ob_binary_packages= -_ob_got_binary_packages=false _ob_source_parameters= _ob_binary_parameters= @@ -35,6 +34,38 @@ _ob_package_do() return 0 } +_ob_find_binary_packages() +{ + local pkgs_clean= + local pkg= + local pkg_clean= + + pkgs_clean=' ' + _ob_binary_packages='' + + for pkg in $(_ob_package_do 'get_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="$(printf '%s' "${pkg}" | tr 'a-z' 'A-Z' | \ + tr -C 'A-Z0-9' '_')" + 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} ${pkg}" + done + + return 0 +} + ## @brief Initialize libopkbuild for a source package ## @details \fBob_init_package\fP() detects the version of and parses all ## metadata of a source package. This function must be called before @@ -65,7 +96,7 @@ ob_init_package() return 1 fi - _ob_binary_packages= + _ob_find_binary_packages _ob_package_do 'parse_package_metadata' || return ${?} @@ -82,19 +113,14 @@ ob_init_package() ## @stdout Prints the resulting list of binary packages. ## @stderr Prints warning messages on invalid binary package names and duplicate ## "clean" binary package names. -## @pure maybe This function caches a list of all binary packages to an internal -## global variable to save time and avoid repeating warning messages -## on subsequent invocations. It should therefore first be called -## outside a subshell, though this is not required. +## @pure yes This function has no side effects. ob_get_binary_packages() { local opt= local host_arch= local host_plat= - local pkg= - local pkgs_clean= - local pkg_clean= local pkgs= + local pkg= OPTIND=1 while getopts 'a:p:' opt; do @@ -112,37 +138,6 @@ ob_get_binary_packages() done shift $((${OPTIND} - 1)) - if ! ${_ob_got_binary_packages}; then - - pkgs_clean=' ' - _ob_binary_packages='' - - for pkg in $(_ob_package_do 'get_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="$(printf '%s' "${pkg}" | tr 'a-z' 'A-Z' | \ - tr -C 'A-Z0-9' '_')" - 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} ${pkg}" - - done - - _ob_got_binary_packages=true - - fi - pkgs='' for pkg in ${_ob_binary_packages}; do |