commit:     aecc59a5f2c05db09ee7e04a32875998adddf446
Author:     Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Thu Sep 20 18:18:44 2018 +0000
Commit:     Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Thu Sep 20 18:48:37 2018 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=aecc59a5

Move parallel xargs wrapping to isolated-functions.sh

Move the parallel xargs support used in pngfix QA check
to isolated-functions.sh, to be reused in ecompress.

Signed-off-by: Michał Górny <mgorny <AT> gentoo.org>
Reviewed-by: Zac Medico <zmedico <AT> gentoo.org>

 bin/helper-functions.sh         |  9 +--------
 bin/install-qa-check.d/60pngfix | 13 ++-----------
 bin/isolated-functions.sh       | 22 ++++++++++++++++++++++
 3 files changed, 25 insertions(+), 19 deletions(-)

diff --git a/bin/helper-functions.sh b/bin/helper-functions.sh
index 9b6e201aa..2d359762a 100644
--- a/bin/helper-functions.sh
+++ b/bin/helper-functions.sh
@@ -10,13 +10,6 @@ source "${PORTAGE_BIN_PATH}"/isolated-functions.sh || exit 1
 #
 # API functions for doing parallel processing
 #
-makeopts_jobs() {
-       # Copied from eutils.eclass:makeopts_jobs()
-       local jobs=$(echo " ${MAKEOPTS} " | \
-               sed -r -n 
's:.*[[:space:]](-j|--jobs[=[:space:]])[[:space:]]*([0-9]+).*:\2:p')
-       echo ${jobs:-1}
-}
-
 __multijob_init() {
        # Setup a pipe for children to write their pids to when they finish.
        # We have to allocate two fd's because POSIX has undefined behavior
@@ -34,7 +27,7 @@ __multijob_init() {
        rm -f "${pipe}"
 
        # See how many children we can fork based on the user's settings.
-       mj_max_jobs=$(makeopts_jobs "$@")
+       mj_max_jobs=$(___makeopts_jobs "$@")
        mj_num_jobs=0
 }
 

diff --git a/bin/install-qa-check.d/60pngfix b/bin/install-qa-check.d/60pngfix
index b69c55614..a2e23d1d3 100644
--- a/bin/install-qa-check.d/60pngfix
+++ b/bin/install-qa-check.d/60pngfix
@@ -1,16 +1,7 @@
 # Check for issues with PNG files
 
-source "${PORTAGE_BIN_PATH}/helper-functions.sh" || exit 1
-
 pngfix_check() {
-       local chunksize=1 jobs pngfix=$(type -P pngfix) xargs=(${XARGS})
-
-       if "${xargs[@]}" --help | grep -q -- --max-procs=; then
-               jobs=$(makeopts_jobs)
-               if [[ ${jobs} -gt 1 ]]; then
-                       xargs+=("--max-procs=${jobs}" -L "${chunksize}")
-               fi
-       fi
+       local pngfix=$(type -P pngfix)
 
        if [[ -n ${pngfix} ]] ; then
                local pngout=()
@@ -35,7 +26,7 @@ pngfix_check() {
                                fi
                                eqawarn "   ${pngout[@]:7}: ${error}"
                        fi
-               done < <(find "${ED}" -type f -name '*.png' -print0 | 
"${xargs[@]}" -0 "${pngfix}")
+               done < <(find "${ED}" -type f -name '*.png' -print0 | 
___parallel_xargs -0 "${pngfix}")
        fi
 }
 

diff --git a/bin/isolated-functions.sh b/bin/isolated-functions.sh
index cac42a4c5..39b0ad344 100644
--- a/bin/isolated-functions.sh
+++ b/bin/isolated-functions.sh
@@ -460,6 +460,28 @@ if [[ -z ${XARGS} ]] ; then
        esac
 fi
 
+___makeopts_jobs() {
+       # Copied from eutils.eclass:makeopts_jobs()
+       local jobs=$(echo " ${MAKEOPTS} " | \
+               sed -r -n 
's:.*[[:space:]](-j|--jobs[=[:space:]])[[:space:]]*([0-9]+).*:\2:p')
+       echo ${jobs:-1}
+}
+
+# Run ${XARGS} in parallel for detected number of CPUs, if supported.
+# Passes all arguments to xargs, and returns its exit code
+___parallel_xargs() {
+       local chunksize=1 jobs xargs=( ${XARGS} )
+
+       if "${xargs[@]}" --help | grep -q -- --max-procs=; then
+               jobs=$(___makeopts_jobs)
+               if [[ ${jobs} -gt 1 ]]; then
+                       xargs+=("--max-procs=${jobs}" -L "${chunksize}")
+               fi
+       fi
+
+       "${xargs[@]}" "${@}"
+}
+
 hasq() {
        has $EBUILD_PHASE prerm postrm || eqawarn \
                "QA Notice: The 'hasq' function is deprecated (replaced by 
'has')"

Reply via email to