168168fi
169169check_command pip-licenses ;
170170check_command shlock ;
171-
172171# Set default exitcode to failure until sure we won't need to abort
173172EXIT_CODE=1
174173
@@ -224,13 +223,33 @@ function handle_signals() {
224223handle_signals
225224
226225# lazy defined variables should be defined now that this is the only script instance.
226+ # identify git root
227+ if GIT_ROOT_DIR=$( git rev-parse --show-superproject-working-tree 2> /dev/null) ; then
228+ if [ -z " ${GIT_ROOT_DIR} " ]; then
229+ GIT_ROOT_DIR=$( git rev-parse --show-toplevel 2> /dev/null)
230+ fi
231+ else
232+ python3 -B .github/tools/cioutput.py -l error --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " FAIL" " Missing valid repository or source structure." >&2
233+ EXIT_CODE=40
234+ fi
235+
236+ CIOUTPUT_PY=" .github/tools/cioutput.py"
237+ # check for tool by full path
238+ if [[ ( -r ${GIT_ROOT_DIR} /.github/tools/cioutput.py ) ]] ; then
239+ # set absolute path to .github/tools/cioutput.py
240+ CIOUTPUT_PY=${GIT_ROOT_DIR} /.github/tools/cioutput.py
241+ else
242+ python3 -B .github/tools/cioutput.py -l info --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " SKIP" " Required CI Output tool can not be found." >&2 ;
243+ EXIT_CODE=126
244+ fi
245+
227246
228247# set the script-file to check_pip
229248SCRIPT_FILE=" tests/check_pip"
230249# Set pip-audit options
231250AUDIT_OPTIONS=" --progress-spinner off --desc on --requirement"
232251# List of Allowed Licenses delimited by semicolon ;
233- ALLOW_LICENSES=" Public Domain;Apache Software License;MIT License;BSD License;Python Software Foundation License;The Unlicense (Unlicense);Mozilla Public License 2.0 (MPL 2.0);"
252+ ALLOW_LICENSES=" Public Domain;Apache Software License;Apache-2.0; MIT License;BSD License;Python Software Foundation License;The Unlicense (Unlicense);Mozilla Public License 2.0 (MPL 2.0);"
234253# Set pip-licenses options
235254LICENSE_OPTIONS=" --from=mixed"
236255# Set pip options
@@ -244,10 +263,12 @@ else
244263 PIP_ENV_FLAGS=" "
245264 LICENSE_OPTIONS=" ${LICENSE_OPTIONS} --ignore-packages chardet"
246265fi ;
266+ # urllib is licensed under MIT, but reports "UNKNOWN"
267+ LICENSE_OPTIONS=" ${LICENSE_OPTIONS} --ignore-packages urllib3"
247268# Enable auto-fix if '--fix' argument is provided
248269if [[ " $1 " == " --fix" ]]; then
249270 AUDIT_OPTIONS=" --fix --strict ${AUDIT_OPTIONS} "
250- python3 -B .github/tools/cioutput.py --log-level debug -l debug " Auto-fix enabled."
271+ python3 -B " ${CIOUTPUT_PY} " --log-level debug -l debug " Auto-fix enabled."
251272fi
252273
253274# lazy defined functions should be defined now that this is the only script instance.
@@ -258,7 +279,7 @@ function setup_venv() {
258279 local temp_dir
259280 # relax umask to allow mktemp and venv
260281 umask 007
261- python3 -B .github/tools/cioutput.py --log-level debug -l debug " need venv ..."
282+ python3 -B " ${CIOUTPUT_PY} " --log-level debug -l debug " need venv ..."
262283 # Create a temporary directory for the virtual environment
263284 temp_dir=$( mktemp -d)
264285 TEMP_DIRS=" ${TEMP_DIRS} ${temp_dir} " ;
@@ -300,42 +321,42 @@ function install_package() {
300321 local pkg=" $1 "
301322 # shellcheck disable=SC2086
302323 if ! python3 -m pip install $PIP_COMMON_FLAGS $PIP_ENV_FLAGS " ${pkg} " ; then
303- python3 -B .github/tools/cioutput.py -l warning --file " ${req_file} " --title " PIP" " Failed to install ${pkg} " >&2 ;
324+ python3 -B " ${CIOUTPUT_PY} " -l warning --file " ${req_file} " --title " PIP" " Failed to install ${pkg} " >&2 ;
304325 return 6
305326 fi
306327 return 0
307328}
308329
309330# === Utilities ===
310331function report_summary() {
311- python3 -B .github/tools/cioutput.py --group " Results" ;
332+ python3 -B " ${CIOUTPUT_PY} " --group " Results" ;
312333 # Improved reporting based on EXIT_CODE
313334 case " ${EXIT_CODE} " in
314- 0) python3 -B .github/tools/cioutput.py -l info --file " ${SCRIPT_FILE} " --title " OK" " OK: Found no detected requirements errors." ;;
315- 1) python3 -B .github/tools/cioutput.py -l error --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " CHECK-PIP" " FAIL: General failure during script execution." >&2 ;;
316- 3) python3 -B .github/tools/cioutput.py -l error --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " CONFIGURATION" " FAIL: Gathering repostory's requirements failed." >&2 ;; # git ls-tree command failed
317- 4) python3 -B .github/tools/cioutput.py -l critical --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " SECURITY" " FAIL: pip-audit detected security vulnerabilities." >&2 ;;
318- 5) python3 -B .github/tools/cioutput.py -l error --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " LICENSE" " FAIL: pip-licenses detected license issues." >&2 ;;
319- 6) python3 -B .github/tools/cioutput.py -l error --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " INSTALL" " FAIL: pip install failed." >&2 ;;
320- 126) python3 -B .github/tools/cioutput.py -l warning --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " SKIPPED" " SKIP: Unable to continue script execution." >&2 ;;
321- * ) python3 -B .github/tools/cioutput.py -l error --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " FAILED" " FAIL: Detected requirements errors." >&2 ;;
335+ 0) python3 -B " ${CIOUTPUT_PY} " -l info --file " ${SCRIPT_FILE} " --title " OK" " OK: Found no detected requirements errors." ;;
336+ 1) python3 -B " ${CIOUTPUT_PY} " -l error --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " CHECK-PIP" " FAIL: General failure during script execution." >&2 ;;
337+ 3) python3 -B " ${CIOUTPUT_PY} " -l error --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " CONFIGURATION" " FAIL: Gathering repostory's requirements failed." >&2 ;; # git ls-tree command failed
338+ 4) python3 -B " ${CIOUTPUT_PY} " -l critical --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " SECURITY" " FAIL: pip-audit detected security vulnerabilities." >&2 ;;
339+ 5) python3 -B " ${CIOUTPUT_PY} " -l error --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " LICENSE" " FAIL: pip-licenses detected license issues." >&2 ;;
340+ 6) python3 -B " ${CIOUTPUT_PY} " -l error --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " INSTALL" " FAIL: pip install failed." >&2 ;;
341+ 126) python3 -B " ${CIOUTPUT_PY} " -l warning --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " SKIPPED" " SKIP: Unable to continue script execution." >&2 ;;
342+ * ) python3 -B " ${CIOUTPUT_PY} " -l error --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " FAILED" " FAIL: Detected requirements errors." >&2 ;;
322343 esac
323- python3 -B .github/tools/cioutput.py --group ;
344+ python3 -B " ${CIOUTPUT_PY} " --group ;
324345}
325346
326347function navigate_dirs_by_git() {
327- if _TEST_ROOT_DIR =$( git rev-parse --show-superproject-working-tree 2> /dev/null) ; then
328- if [ -z " ${_TEST_ROOT_DIR } " ]; then
329- _TEST_ROOT_DIR =$( git rev-parse --show-toplevel 2> /dev/null)
330- if [ -z " ${_TEST_ROOT_DIR } " ]; then
331- python3 -B .github/tools/cioutput.py -l error --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " ${FUNCNAME:- $0 } " " FAIL: Could not determine repository root" >&2
348+ if GIT_ROOT_DIR =$( git rev-parse --show-superproject-working-tree 2> /dev/null) ; then
349+ if [ -z " ${GIT_ROOT_DIR } " ]; then
350+ GIT_ROOT_DIR =$( git rev-parse --show-toplevel 2> /dev/null)
351+ if [ -z " ${GIT_ROOT_DIR } " ]; then
352+ python3 -B " ${CIOUTPUT_PY} " -l error --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " ${FUNCNAME:- $0 } " " FAIL: Could not determine repository root" >&2
332353 EXIT_CODE=40
333354 return ${EXIT_CODE}
334355 fi
335356 fi
336- python3 -B .github/tools/cioutput.py --log-level debug -l debug " Found ${_TEST_ROOT_DIR } ..." ;
357+ python3 -B " ${CIOUTPUT_PY} " --log-level debug -l debug " Found ${GIT_ROOT_DIR } ..." ;
337358 else
338- python3 -B .github/tools/cioutput.py -l error --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " ${FUNCNAME:- $0 } " " FAIL: missing valid repository or source structure" >&2
359+ python3 -B " ${CIOUTPUT_PY} " -l error --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " ${FUNCNAME:- $0 } " " FAIL: missing valid repository or source structure" >&2
339360 EXIT_CODE=40
340361 return ${EXIT_CODE}
341362 fi
@@ -351,23 +372,23 @@ function check_package_licenses() {
351372 # move into a venv
352373 setup_venv ; SUB_CODE=$? ;
353374 wait ;
354- python3 -B .github/tools/cioutput.py --log-level debug -l debug " venv setup ... (${SUB_CODE} )" ;
375+ python3 -B " ${CIOUTPUT_PY} " --log-level debug -l debug " venv setup ... (${SUB_CODE} )" ;
355376 # Install pip-licenses
356377 install_package " pip-licenses>=5.0" || SUB_CODE=6 ;
357378 wait ;
358379 local pkg
359380 # Install the given Python modules using pip
360381 # shellcheck disable=SC2086
361382 for pkg in ${packages} ; do
362- python3 -B .github/tools/cioutput.py --log-level debug -l debug " Checking license from package '${pkg} ' ..." ;
363- REQ_SPEC=$( grep -F -- " ${pkg} " <( cat < " ${_TEST_ROOT_DIR } " /$req_file | sed -E -e ' /^[[:space:]]*$/d' | sed -E -e ' /^[#]+.*$/d' ) | grep -m1 -F -- " ${pkg} " )
383+ python3 -B " ${CIOUTPUT_PY} " --log-level debug -l debug " Checking license from package '${pkg} ' ..." ;
384+ REQ_SPEC=$( grep -F -- " ${pkg} " <( cat < " ${GIT_ROOT_DIR } " /$req_file | sed -E -e ' /^[[:space:]]*$/d' | sed -E -e ' /^[#]+.*$/d' ) | grep -m1 -F -- " ${pkg} " )
364385 ERR_MSG=" pip install '${pkg} ' failed for $req_file ." ;
365386 if [[ (" ${SUB_CODE} " -eq 0) ]] && install_package " ${REQ_SPEC} ;" 2> /dev/null ;
366387 then
367- python3 -B .github/tools/cioutput.py --log-level debug -l debug " Fetched license from package '${pkg} ' ..." ;
388+ python3 -B " ${CIOUTPUT_PY} " --log-level debug -l debug " Fetched license from package '${pkg} ' ..." ;
368389 else
369390 [[ (" ${SUB_CODE} " -eq 0) ]] && SUB_CODE=6 && \
370- python3 -B .github/tools/cioutput.py -l warning --file " ${req_file} " --line 1 --col 1 --title " PIP" " ${ERR_MSG} " >&2
391+ python3 -B " ${CIOUTPUT_PY} " -l warning --file " ${req_file} " --line 1 --col 1 --title " PIP" " ${ERR_MSG} " >&2
371392 fi
372393 unset ERR_MSG 2> /dev/null || : ;
373394 done
@@ -380,51 +401,52 @@ function check_package_licenses() {
380401 return " ${SUB_CODE} "
381402}
382403
383- # THIS IS THE ACTUAL TEST DIR USED (update _TEST_ROOT_DIR as needed)
384- _TEST_ROOT_DIR =$( git rev-parse --show-toplevel 2> /dev/null) ;
404+ # THIS IS THE ACTUAL TEST DIR USED (update GIT_ROOT_DIR as needed)
405+ GIT_ROOT_DIR =$( git rev-parse --show-toplevel 2> /dev/null) ;
385406navigate_dirs_by_git
386407
387408if [[ (" ${EXIT_CODE} " -eq 0) ]] ; then
388409
389- python3 -B .github/tools/cioutput.py --log-level debug -l debug " Reading from repository ${_TEST_ROOT_DIR } ..." ;
410+ python3 -B " ${CIOUTPUT_PY} " --log-level debug -l debug " Reading from repository ${GIT_ROOT_DIR } ..." ;
390411 # Get a list of files to check using git ls-tree with filtering (and careful shell globing)
391- FILES_TO_CHECK=$( git ls-tree -r --full-tree --name-only HEAD -- " ${_TEST_ROOT_DIR } " /test/requirements.txt " ${_TEST_ROOT_DIR } " /* -requirements.txt " ${_TEST_ROOT_DIR } /requirements.txt" 2> /dev/null) || EXIT_CODE=3 ;
392- [[ ( $EXIT_CODE -eq 0 ) ]] || { python3 -B .github/tools/cioutput.py -l error --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " GIT" " Failed to list requirements files using git ls-tree" >&2 ; }
412+ FILES_TO_CHECK=$( git ls-tree -r --full-tree --name-only HEAD -- " ${GIT_ROOT_DIR } " /test/requirements.txt " ${GIT_ROOT_DIR } " /* -requirements.txt " ${GIT_ROOT_DIR } /requirements.txt" 2> /dev/null) || EXIT_CODE=3 ;
413+ [[ ( $EXIT_CODE -eq 0 ) ]] || { python3 -B " ${CIOUTPUT_PY} " -l error --file " ${SCRIPT_FILE} " --line ${BASH_LINENO:- 0} --title " GIT" " Failed to list requirements files using git ls-tree" >&2 ; }
393414
394415 if [[ (" ${EXIT_CODE} " -eq 0) ]] ; then
395416 # THIS IS THE ACTUAL TEST
396- python3 -B .github/tools/cioutput.py --log-level debug -l debug " Starting checks ..." ;
417+ python3 -B " ${CIOUTPUT_PY} " --log-level debug -l debug " Starting checks ..." ;
397418 # Iterate over files and run checks
398419 for req_file in ${FILES_TO_CHECK} ; do
399- python3 -B .github/tools/cioutput.py --group " Checking ${req_file} " ;
420+ python3 -B " ${CIOUTPUT_PY} " --group " Checking ${req_file} " ;
400421 if [[ ( -x $( command -v pip-audit) ) ]] && [[ (" ${EXIT_CODE} " -eq 0) ]] ; then
401- python3 -B .github/tools/cioutput.py --log-level debug -l debug " Auditing ${req_file} for security vulnerabilities ..."
422+ python3 -B " ${CIOUTPUT_PY} " --log-level debug -l debug " Auditing ${req_file} for security vulnerabilities ..."
402423 # shellcheck disable=SC2086
403424 { pip-audit $AUDIT_OPTIONS " ${req_file} " || EXIT_CODE=4 ; } ; wait ;
404425 fi ;
405426 if [[ (" ${EXIT_CODE} " -eq 0) ]] ; then
406- python3 -B .github/tools/cioutput.py --log-level debug -l debug " Checking licenses in $req_file ..." ;
427+ python3 -B " ${CIOUTPUT_PY} " --log-level debug -l debug " Checking licenses in $req_file ..." ;
407428 # filter for only pkg from requirements file
408429 check_package_licenses " $req_file " ; EXIT_CODE=$?
409430 else
410- python3 -B .github/tools/cioutput.py -l error --file " ${req_file} " --line 1 --title " REQUIREMENTS" " FAIL: Found requirements errors." >&2 ;
431+ python3 -B " ${CIOUTPUT_PY} " -l error --file " ${req_file} " --line 1 --title " REQUIREMENTS" " FAIL: Found requirements errors." >&2 ;
411432 fi
412- python3 -B .github/tools/cioutput.py --group ;
433+ python3 -B " ${CIOUTPUT_PY} " --group ;
413434 done
414435 fi
415436
416- python3 -B .github/tools/cioutput.py --log-level debug -l debug " Summary reporting ..." ;
437+ python3 -B " ${CIOUTPUT_PY} " --log-level debug -l debug " Summary reporting ..." ;
417438 report_summary
418439fi
419440
420- python3 -B .github/tools/cioutput.py --log-level debug -l debug " Cleaning up ..." ;
441+ python3 -B " ${CIOUTPUT_PY} " --log-level debug -l debug " Cleaning up ..." ;
421442cleanup || rm -f " ${LOCK_FILE} " 2> /dev/null || : ;
422443
423444# unset when done
424- unset _TEST_ROOT_DIR 2> /dev/null || : ;
445+ unset GIT_ROOT_DIR 2> /dev/null || : ;
425446unset AUDIT_OPTIONS 2> /dev/null || : ;
426447unset ALLOW_LICENSES 2> /dev/null || : ;
427448unset LICENSE_OPTIONS 2> /dev/null || : ;
449+ unset CIOUTPUT_PY 2> /dev/null || : ;
428450
429451wait ;
430452python3 -B .github/tools/cioutput.py --log-level debug -l debug " Check-pip done." ;
0 commit comments