From 133cb6b52f0cdd13c7d34c0a1e904ff12859791c Mon Sep 17 00:00:00 2001 From: Krzesimir Nowak Date: Mon, 11 Jul 2022 20:04:54 +0200 Subject: [PATCH] ci-automation: Factor out listing files into a separate function This will come in handy when listing files for creating digests files. --- ci-automation/ci_automation_common.sh | 67 +++++++++++++++++++++------ 1 file changed, 52 insertions(+), 15 deletions(-) diff --git a/ci-automation/ci_automation_common.sh b/ci-automation/ci_automation_common.sh index adc2b85d77..a4f1d1e60e 100644 --- a/ci-automation/ci_automation_common.sh +++ b/ci-automation/ci_automation_common.sh @@ -332,26 +332,13 @@ function sign_artifacts() { # rest of the parameters are directories/files to sign local to_sign=() local file - local files if [[ -z "${signer}" ]]; then return fi - for file; do - files=() - if [[ -d "${file}" ]]; then - readarray -d '' files < <(find "${file}" ! -type d -print0) - elif [[ -e "${file}" ]]; then - files+=( "${file}" ) - fi - for file in "${files[@]}"; do - if [[ "${file}" =~ \.(asc|gpg|sig)$ ]]; then - continue - fi - to_sign+=( "${file}" ) - done - done + list_files to_sign 'asc,gpg,sig' "${@}" + for file in "${to_sign[@]}"; do gpg --batch --local-user "${signer}" \ --output "${file}.sig" \ @@ -359,3 +346,53 @@ function sign_artifacts() { done } # -- + +# Puts a filtered list of files from the passed files and directories +# in the passed variable. The filtering is done by ignoring files that +# end with the passed extensions. The extensions list should not +# contain the leading dot. +# +# Typical use: +# local all_files=() +# local ignored_extensions='sh,py,pl' # ignore the shell, python and perl scripts +# list_files all_files "${ignored_extensions}" "${directories_and_files[@]}" +# +# Parameters: +# +# 1 - name of an array variable where the filtered files will be stored +# 2 - comma-separated list of extensions that will be used for filtering files +# @ - files and directories to scan for files +function list_files() { + local files_variable_name="${1}"; shift + local ignored_extensions="${1}"; shift + # rest of the parameters are files or directories to list + local -n files="${files_variable_name}" + local file + local tmp_files + local pattern='' + + if [[ -n "${ignored_extensions}" ]]; then + pattern='\.('"${ignored_extensions//,/|}"')$' + fi + + files=() + for file; do + tmp_files=() + if [[ -d "${file}" ]]; then + readarray -d '' tmp_files < <(find "${file}" ! -type d -print0) + elif [[ -e "${file}" ]]; then + tmp_files+=( "${file}" ) + fi + if [[ -z "${pattern}" ]]; then + files+=( "${tmp_files[@]}" ) + continue + fi + for file in "${tmp_files[@]}"; do + if [[ "${file}" =~ ${pattern} ]]; then + continue + fi + files+=( "${file}" ) + done + done +} +# --