From e07f8a6be24d4944322c952520afb1225b29e37f Mon Sep 17 00:00:00 2001 From: Ron Stone Date: Wed, 23 Oct 2024 15:07:37 +0000 Subject: [PATCH] Pass tox context Allow passing tox context via env var and content meta. This makes certian contitional build steps easier for users to maintain. Change-Id: I578ac6e7ed758cee04f7e2985a14065ffc99177d Signed-off-by: Ron Stone --- _utils.sh | 92 ++++++++++++++++++++++++++++++++++++++++++- hide-table-columns.py | 10 +++++ tox.ini | 1 + 3 files changed, 102 insertions(+), 1 deletion(-) diff --git a/_utils.sh b/_utils.sh index 3b90c6cce..a3399efc1 100644 --- a/_utils.sh +++ b/_utils.sh @@ -10,6 +10,8 @@ confirmation () { message $GR$@$NC; } warn () { message $OG$@$NC; } error () { message $RED$0:$?: $@$NC; exit 1; } +declare meta_ver=":MM(.)mm:" + # Check for and exit if file dependancies are not met. Takes a list of full or # relative paths. check_file_deps () { @@ -17,6 +19,7 @@ check_file_deps () { do if [ ! -f "${filereq}" ] && [ ! -L "${filereq}" ]; then error "${filereq} not found. Quiting."; exit 1; fi done + return 0 } # Check for and exit if command dependancies are not met. Takes a list of @@ -111,4 +114,91 @@ check_abbr_dups () { } -declare utils_loaded=1 \ No newline at end of file +# Return this product version +# +# Finds and echos out this-ver value from _this.txt +# Calling context is responsible for parsing major and +# minor numbers into vars (g_YY and g_MM by convention) +get_this_version() { + + local this_file="_this.txt" + local ver_file=$(find doc* -name $this_file) + [[ ! -f ${ver_file} ]] && \ + error "$this_file not found. Quiting." + + local _ver + local regex_ver="\.\.\s+\|this-ver\|\s+replace::\s+([0-9]{2}\.[0-9]{2})\s*" + + local _this=$(grep this-ver $ver_file) + + if [[ "${_this}" =~ $regex_ver ]] + then + _ver="${BASH_REMATCH[1]}" + else + error "Can't find local product version" + fi + echo $_ver + +} + + +# List hash of separator characters used in ":MMmm:" style +# placeholders +# +# Takes path to the file to scan. +# Sets global hash 'spacers'. Note that BRE special characters will be escaped. + +get_version_spacers() { + + local _sep + check_file_deps $1 && \ + local _f=$1 + + spacers=() + while read l; do + _sep='' + if [[ $l =~ ${meta_ver} ]]; then + [[ ${BASH_REMATCH[1]} == [\$\*\.\\\[\]] ]] && \ + _sep="\\${BASH_REMATCH[1]}" || \ + _sep="${BASH_REMATCH[1]}" + spacers["${_sep}"]=0 + fi + done < $_f + +} + +# Replace ":MMmm:" placeholders in a target file. +# +# Takes path to file to manipulate and overwrites on disk +# Returns 0 on success +# Note: global hash 'spacers' must be set; see get_version_spacers + +replace_version_placeholders() { + + check_file_deps $1 && \ + local _f=$1 + local _rs + + for sp in "${!spacers[@]}"; do + [[ $sp == '^' ]] && _rs="" || _rs=$sp + sed -i "s/:MM${sp}mm:/${g_YY}$_rs${g_MM}/g" $_f + done + + return 0 + +} + +# Return the name of the current default branch +get_this_branch() { + + local _regex_br="defaultbranch=(.*)\s*$" + local _config="$PWD/.gitreview" + + if [[ $(cat $_config) =~ $_regex_br ]]; then + echo "${BASH_REMATCH[1]}" + else + error "$PWD is not a branch" + fi +} + +declare utils_loaded=1 diff --git a/hide-table-columns.py b/hide-table-columns.py index 6ac7ecc0d..46d7f5eba 100644 --- a/hide-table-columns.py +++ b/hide-table-columns.py @@ -1,10 +1,20 @@ import sys +import os from bs4 import BeautifulSoup +BUILD_CONTEXT=os.environ.get('DOCS_BUILD_CONTEXT') + def remove_column_from_tables(file_path): with open(file_path, 'r', encoding='utf-8') as file: soup = BeautifulSoup(file, 'lxml') + if BUILD_CONTEXT: + context_tag = soup.find('meta', attrs={'name': 'docs-build-context'}) + if context_tag: + if context_tag.get('content') != BUILD_CONTEXT: + print("Not in", context_tag.get('content'), "- skipping", file_path) + return + # Find column to delete column_tag = soup.find('meta', attrs={'name': 'remove-column-from-html-table'}) if column_tag: diff --git a/tox.ini b/tox.ini index 30cf1baea..52064e62f 100644 --- a/tox.ini +++ b/tox.ini @@ -10,6 +10,7 @@ setenv = VIRTUAL_ENV={envdir} OS_STDERR_CAPTURE=1 OS_TEST_TIMEOUT=60 LC_ALL=C + DOCS_BUILD_CONTEXT=starlingx deps = -r{toxinidir}/test-requirements.txt [testenv:prebuild-docs]