All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
updateVersionList.sh
Go to the documentation of this file.
1 #!/usr/bin/env bash
2 #
3 # Run to update the HTML page with the available versions.
4 #
5 # Used HTML classes:
6 #
7 # aside : for messages of lesser importance
8 # reponame : a code repository name
9 # GITbranch : GIT hash or branch tag
10 # version : a code version
11 # currentversion: the current code version
12 # updateline : (block) the "last updated" line
13 # warning : a message about possible failure
14 #
15 #
16 # Changes
17 # --------
18 #
19 # 202004xx (petrillo@slac.stanford.edu) [v1.0]
20 # original version
21 # 20210412 (petrillo@slac.stanford.edu) [v1.1]
22 # added support for command line options
23 #
24 
25 # -- BEGIN -- boilerplate settings and library loading -------------------------
26 SCRIPTNAME="$(basename "$0")"
27 SCRIPTDIR="$(dirname "$0")"
28 
29 declare LibraryToLoad
30 for LibraryToLoad in 'settings.sh' 'utilities.sh' ; do
31 
32  source "${SCRIPTDIR%/}/${LibraryToLoad}" || exit $?
33 
34 done
35 unset LibraryToLoad
36 # -- END -- boilerplate settings and library loading ---------------------------
37 
38 # ------------------------------------------------------------------------------
39 SCRIPTVERSION="1.1"
40 
41 # additional settings
42 declare -r RepoDir="./"
43 
44 # template for the version list HTML file, relative to the script directory
45 declare -r DefaultTemplateFileName='versionlisttemplate.html'
46 
47 # the first line with this tag in the template will be replaced with the list
48 declare -r DefaultVersionListTag='VERSIONLISTTAG'
49 # the first line with this tag in the template will be replaced with update info
50 declare -r DefaultUpdateTag='UPDATETAG'
51 
52 
53 # ------------------------------------------------------------------------------
54 function printHelp() {
55 
56  cat <<EOH
57 
58 Updates the HTML file presenting the list of available documentation of
59 ${ExperimentName:-the currently configured experiment}.
60 
61 Usage: ${SCRIPTNAME} [options]
62 
63 Supported options:
64 --experiment=EXPERIMENTNAME [${ExperimentName:-autodetect}]
65  the name of the experiment (e.g. 'MicroBooNE')
66 --outputfile=OUTPUTHTML
67  sets the list file to be created (by default, the repository name under the
68  configured PublishBaseDir${PublishBaseDir:+", i.e. '${PublishBaseDir}'"})
69 --reponame=REPONAME [${ExperimentCodeName:-autodetect}]
70  the name of the repository (e.g. 'uboonecode')
71 --version , -V
72  prints the script version and exits (hint: it's version ${SCRIPTVERSION}
73 --help , -h , -?
74  prints this usage message and exits
75 
76 EOH
77 
78 } # printHelp()
79 
80 
81 function printVersion() {
82  echo "${SCRIPTNAME} version ${SCRIPTVERSION}."
83 } # printVersion()
84 
85 
86 # ------------------------------------------------------------------------------
87 function FillVersionsList() {
88 
89  local -r DocDir="${1%/}/"
90 
91  [[ -d "$DocDir" ]] || FATAL 2 "Can't find documentation base directory '${DocDir}'."
92 
93  DocVersions=() # global
94 
95  local VersionCandidatePath
96  for VersionCandidatePath in "${DocDir}"* ; do
97  [[ -d "$VersionCandidatePath" ]] || continue
98  local VersionCandidate="$(basename "$VersionCandidatePath")"
99  [[ "$VersionCandidate" =~ ^v[[:digit:]]{2}_[[:digit:]]{2}_[[:digit:]]{2}(_[[:digit:]]{2})?$ ]] || continue
100  DocVersions+=( "$VersionCandidate" )
101 
102  done
103 
104 } # FillVersionsList()
105 
106 
107 function MakeVersionItem() {
108 
109  local -r DocVersion="$1"
110  local -r DocPath="$2"
111  local -r Indent="${3:-" "}"
112 
113  local ExpCodeName="$ExperimentCodeName"
114  local Version="$DocVersion"
115  local DocDate
116  local DocGenExitCode
117 
118  local -r MetadataFile="${DocPath%/}/${MetadataFileRelPath}"
119  if [[ -r "$MetadataFile" ]]; then
120  # metadata file is currently (version 1.0) created by UpdateFiles.sh
121  local MetadataVersion
122  MetadataVersion="$(ExtractFromMetadata 'MetadataVersion' "$MetadataFile")"
123 
124  ExpCodeName="$(ExtractFromMetadata 'ExperimentCode' "$MetadataFile")" # maybe overwrite
125  Version="$(ExtractFromMetadata 'CodeVersion' "$MetadataFile")" # maybe overwrite
126  DocDate="$(ExtractFromMetadata 'Date' "$MetadataFile")" # maybe overwrite
127  DocGenExitCode="$(ExtractFromMetadata 'ExitCode' "$MetadataFile")" # maybe overwrite
128  DocCodeGITreference="$(ExtractFromMetadata 'GITreference' "$MetadataFile")" # maybe overwrite
129 
130  fi
131 
132  local Msg="<span class=\"reponame\">${ExpCodeName}</span> <span class=\"version\">${Version}</span>"
133  if [[ -z "$DocGenExitCode" ]]; then
134  Msg+=' <span class="aside">(no additional information available)</span>'
135  else
136  Msg+=" (documentation from ${DocDate}"
137  if [[ -n "$DocCodeGITreference" ]] && [[ "$DocCodeGITreference" != "$Version" ]]; then
138  Msg+=" for GIt reference <span class=\"GITbranch\">${DocCodeGITreference}</span>'"
139  fi
140  Msg+=')'
141  if [[ "$DocGenExitCode" != 0 ]]; then
142  Msg+=" <span class=\"warning\">[it may be incomplete: generation exited with code ${DocGenExitCode}]</span>"
143  fi
144  fi
145 
146  echo "${Indent}<li><a href=\"${DocVersion}\">${Msg}</a></li>"
147 
148 } # MakeVersionItem()
149 
150 
151 function MakeHTMLlist() {
152 
153  local -r DocDir="$1"
154 
155  [[ -d "$DocDir" ]] || FATAL 2 "Can't find documentation base directory '${DocDir}'."
156 
157  # check for the special link
158  if [[ -h "${DocDir%/}/${LatestLinkName}" ]]; then
159  local -r LatestVersion="$(readlink "${DocDir%/}/${LatestLinkName}")"
160  echo " <li><a href=\"${LatestLinkName}\" class=\"currentversion\">latest documented version (${LatestVersion})</a></li>"
161  fi
162 
163  # look for the actual versions
164  local VersionCandidatePath
165  for VersionCandidatePath in "${DocDir%/}/"* ; do
166 
167  [[ -d "$VersionCandidatePath" ]] || continue
168  local VersionCandidate="$(basename "$VersionCandidatePath")"
169  [[ "$VersionCandidate" =~ ^v[[:digit:]]{2}_[[:digit:]]{2}_[[:digit:]]{2}(_[[:digit:]]{2})?$ ]] || continue
170 
171  MakeVersionItem "$VersionCandidate" "$VersionCandidatePath"
172 
173  done
174 
175 } # MakeHTMLlist()
176 
177 
178 function UpdatingRemarks() {
179  local -r Indent="${1:-" "}"
180  cat <<EOF
181 ${Indent}
182 ${Indent}<hr>
183 ${Indent}<div class="updateline">Last list refresh: $(date)</div>
184 ${Indent}
185 EOF
186 } # UpdatingRemarks()
187 
188 
189 function MakeVersionListFile() {
190  # Writes a HTML file on screen
191 
192  local -r TemplateFilePath="$1"
193  local -r DocDir="$2"
194 
195  #
196  # write a header comment
197  #
198  cat <<EOC
199 <!--
200  This HTML file was automatically generated:
201 
202  Date: $(date)
203  Host: $(hostname)
204  Working directory: '$(pwd)'
205  Command: ${0} ${@}
206  Script version: ${SCRIPTVERSION}
207 
208  -->
209 EOC
210 
211  #
212  # "parse" the template
213  #
214  # the horror...
215  local Line
216  local OldIFS="$IFS"
217  IFS=''
218  while read Line ; do
219  if [[ "$Line" =~ ${VersionListTag} ]]; then # replace this line
220  MakeHTMLlist "$DocDir"
221  elif [[ "$Line" =~ ${UpdateTag} ]]; then # replace this line
222  UpdatingRemarks
223  else
224  # trying hard to avoid any reinterpretation; `echo` *might* have sufficed
225  cat <<< "$Line"
226  fi
227  done < "$TemplateFilePath"
228  IFS="$OldIFS"
229 
230 } # MakeVersionListFile()
231 
232 
233 # ------------------------------------------------------------------------------
234 
235 #
236 # argument parsing
237 #
238 declare -i NoMoreOptions
239 declare -i DoVersion=0 DoHelp=0
240 declare -i CleanOutputArea=0 UpdateOutputArea=0
241 declare TemplateFileName="$DefaultTemplateFileName"
242 declare VersionListTag="$DefaultVersionListTag"
243 declare UpdateTag="$DefaultUpdateTag"
244 
245 
246 declare ExitWithCode
247 declare Param
248 for (( iParam = 1 ; iParam <= $# ; ++iParam )); do
249  Param="${!iParam}"
250  if isFlagUnset NoMoreOptions && [[ "${Param:0:1}" == '-' ]]; then
251  case "$Param" in
252  ( '--experiment='* ) ExperimentName="${Param#--*=}" ;;
253  ( '--outputfile='* ) DestFile="${Param#--*=}" ;;
254  ( '--reponame='* ) ExperimentCodeName="${Param#--*=}" ;;
255  ( '--version' | '-V' ) DoVersion=1 ;;
256  ( '--help' | '-h' | '-?' ) DoHelp=1 ;;
257  ( '-' | '--' ) NoMoreOptions=1 ;;
258  ( * )
259  ERROR "Unknown option: '${Param}'."
260  ExitWithCode=1
261  esac
262  else
263  PositionalArguments+=( "$Param" )
264  fi
265 done
266 
267 [[ "${#PositionalArguments[@]}" -gt 0 ]] && FATAL 1 "Found ${#PositionalArguments[@]} spurious arguments on command line: ${PositionalArguments[@]}."
268 
269 : ${ExperimentName:="$(FindExperiment)"}
270 : ${ExperimentCodeName:="$(ExperimentCodeProduct "$ExperimentName")"}
271 
272 if isFlagSet DoVersion ; then
273  printVersion
274  [[ -z "$ExitWithCode" ]] && ExitWithCode=0
275 fi
276 if isFlagSet DoHelp ; then
277  printHelp
278  [[ -z "$ExitWithCode" ]] && ExitWithCode=0
279 fi
280 
281 [[ -n "$ExitWithCode" ]] && exit "$ExitWithCode"
282 
283 
284 #
285 # checks
286 #
287 # (some checks are delayed because they are not fatal when just printing help)
288 #
289 [[ -n "$ExperimentName" ]] || FATAL 1 "Can't detect the experiment name."
290 
291 [[ -n "$ExperimentCodeName" ]] || FATAL 1 "Can't put together the experiment code repository name for '${ExperimentName}'."
292 
293 declare TemplateFilePath="${SCRIPTDIR%/}/${TemplateFileName}"
294 [[ -r "$TemplateFilePath" ]] || FATAL 2 "Can't find template file '${TemplateFileName}' in '${SCRIPTDIR}'."
295 
296 # not controlled by option because used only to compose `DestFile`:
297 PublishDir="${PublishBaseDir%/}/${ExperimentCodeName}"
298 [[ -r "$PublishDir" ]] || FATAL 2 "Can't find publishing base directory '${PublishDir}'."
299 
300 # set a default destination if needed
301 : ${DestFile:="${PublishDir%/}/${VersionListFile}"}
302 
303 #
304 # write the HTML file
305 #
306 if [[ -e "$DestFile" ]]; then
307  echo "Overwriting: '${DestFile}'"
308 else
309  echo "Writing: '${DestFile}'"
310 fi
311 
312 MakeVersionListFile "$TemplateFilePath" "$PublishDir" > "$DestFile"
313 
version V DoVersion
process_name opflash particleana ie ie ie z
then Msg
bool printHelp
then echo FATAL
do source
* file
Definition: file_to_url.sh:69
#define the
NoMoreOptions
SCRIPTNAME
Definition: publish.sh:21
then source cvmfs dune opensciencegrid org products dune setup_dune sh exit elif[-f/grid/fermiapp/products/dune/setup_dune_fermiapp.sh]
while getopts h
then local
BEGIN_PROLOG V
return match has_match and(match.match_pdg==11 or match.match_pdg==-11)
then shift fi
then echo Work directory not specified exit fi echo Work directory
if &&[-z"$BASH_VERSION"] then echo Attempting to switch to bash bash shellSwitch exit fi &&["$1"= 'shellSwitch'] shift declare a IncludeDirectives for Dir in
help h DoHelp
replace invalid UTF-8 sequences with U+FFFD
then echo echo For and will not be changed by echo further linking echo echo B echo The symbol is in the uninitialized data multiple common symbols may appear with the echo same name If the symbol is defined the common echo symbols are treated as undefined references For more echo details on common see the discussion of warn common echo in *Note Linker options
do i e
then echo fcl name
echo Invalid option
Definition: TrainMVA.sh:17
esac echo uname r
do Param
function isFlagUnset()
Definition: utilities.sh:12
list
Definition: file_to_url.sh:28
experiment
Definition: ffttest.sh:3