All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sbndcode/test/fcl/testFHiCLfiles.sh
Go to the documentation of this file.
1 #!/usr/bin/env bash
2 #
3 # Checks all FHiCL files under the specified directory.
4 #
5 # Usage:
6 #
7 # testFHiCLfiles.sh [file or directory ...]
8 #
9 # By default all FHiCL files under the current directory are tested.
10 #
11 #
12 
13 SCRIPTNAME="$(basename "$0")"
14 SCRIPTDIR="$(dirname "$0")"
15 
16 : ${DefaultTest:='dump'}
17 : ${DEBUG:=0}
18 
19 ################################################################################
20 declare -A ColorCodes
21 function SetupColors() {
22  local UseColors="${1:-1}"
23  local -r Escape=$'\e['
24  if [[ $UseColors != 0 ]]; then
25  ColorCodes=(
26  ['reset']="${Escape}0m"
27  ['red']="${Escape}31m"
28  ['yellow']="${Escape}1;33m"
29  ['bright red']="${Escape}1;31m"
30  ['green']="${Escape}32m"
31  ['bright green']="${Escape}1;32m"
32  ['cyan']="${Escape}36m"
33  )
34  fi
35 } # SetupColors()
36 
37 function GetColor() {
38  local Key="$1"
39  echo -e "${ColorCodes[$Key]}"
40 }
41 
42 function ApplyColor() {
43  local Color="$1"
44  shift
45  echo -e "$(GetColor "$Color")$*$(GetColor 'reset')"
46 } # ApplyColor()
47 
48 function isDebugging() {
49  local -i Level="${1:-1}"
50  [[ "$DEBUG" -ge "$Level" ]]
51 }
52 function DBGN() {
53  local -i Level="$1"
54  isDebugging "$Level" || return
55  shift
56  STDERR 'green' "DBG[${Level}]| $*" ;
57 }
58 function DBG() { DBGN 1 "$@" ; }
59 function INFO() { [[ "$Quiet" == 0 ]] && ApplyColor 'cyan' "$*" ; }
60 function SUCCESS() { ApplyColor 'bright green' "$*" ; }
61 function STDERR() {
62  local Color="$1"
63  shift
64  ApplyColor "$Color" "$*" >&2
65 } # STDERR()
66 function ERROR() { STDERR 'red' "ERROR: $*" ; }
67 function FATAL() {
68  local -i Code="$1"
69  shift
70  STDERR 'bright red' "FATAL (${Code}): $*"
71  exit $Code
72 } # FATAL()
73 function LASTFATAL() {
74  local -i res="$?"
75  [[ $res == 0 ]] || FATAL "$res" "$@"
76 } # LASTFATAL()
77 
78 
79 function DebugTest() {
80  local -ir MaxLevel="${1:-10}"
81  local -i Level
82  for (( Level = 1 ; Level < 10 ; ++Level )); do
83  DBGN "$Level" "Debug level ${Level} shown"
84  done
85 } # DebugTest()
86 
87 
88 ################################################################################
89 function printHelp() {
90  cat <<EOH
91 Performs checks on the specified FHiCL files.
92 
93 Usage: ${SCRIPTNAME} [options] [-|--] [Input ...]
94 
95 A test is performed on every file specified as "Input" on the command line.
96 If an input specification is a file, that file is tested; if the input is a
97 directory, all files with suffix '.fcl' under that directory and its
98 subdirectories are tested.
99 If no input is specified, the current directory is used as starting point.
100 
101 Options:
102 --listtests , -L
103  lists all the supported tests
104 --test=TEST ["${DefaultTest}"]
105  choose which test to perform; use \`--listtests\` to see the supported tests
106 --dump
107  shortcut for \`--test=dump\`
108 --validate
109  shortcut for \`--test=validate\`
110 --exclude=PATTERN
111  exclude all files and directories that match this pattern (Bash regex)
112 --exclude-from=FILE
113  equivalent to \`--exclude=PATTERN\` for all patterns in FILE; each line
114  in FILE is interpreted as a pattern, except for the lines starting with
115  a '#' (no spaces allowed before it)
116 --quiet
117  will print only errors or a single success message
118 --debug[=LEVEL]
119  sets the verbosity to the specified LEVEL for debugging purposes
120 --color | --no-color [default: enabled]
121  enables or disables ANSI color codes in the output
122 --help , -h , -?
123  shows this usage information
124 
125 EOH
126 
127 } # printHelp()
128 
129 
130 ################################################################################
131 declare -Ar TestDescriptions=(
132  ['dump']='FHiCL syntax check (`fhicl-dump`)'
133  ['validate']='ask `art` to perform validation (`lar --validate-config`)'
134 )
135 
136 function printTests() {
137  local TestKey
138  for TestKey in "${!TestDescriptions[@]}" ; do
139  local TestDescription="${TestDescriptions[$TestKey]}"
140  cat <<EOM
141 ${TestKey}
142  ${TestDescription}
143 EOM
144  done
145 } # printTests()
146 
147 
148 function isTestType() {
149  local Key="$1"
150  for TestKey in "${!TestDescriptions[@]}" ; do
151  [[ "$TestKey" == "$Key" ]] && return 0
152  done
153  return 1
154 } # isTestType()
155 
156 
157 function SelectTest() {
158  local Type="${1:-${TestType}}"
159 
160  case "$Type" in
161  # add non-standard types here
162  ( * )
163  if isTestType "$Type" ; then
164  TestProc="Test_${Type}"
165  TestPrep="${TestProc}_prep"
166  DBG "Selected test '${Type}'."
167  return 0
168  fi
169  return 1
170  ;;
171  esac
172 
173 } # SelectTest()
174 
175 
176 function isExcluded() {
177 
178  local Path="$1"
179 
180  local ExcludePattern
181  for ExcludePattern in "${ExcludedPatterns[@]}" ; do
182  [[ "$Path" =~ $ExcludePattern ]] && return 0
183  done
184  return 1
185 
186 } # isExcluded()
187 
188 
189 function ExcludeFromFile() {
190 
191  local File="$1"
192 
193  [[ -r "$File" ]] || FATAL 2 "Can't read exclusion file '${File}'!"
194 
195  local -i nExcluded=0
196  local Pattern
197  while read Pattern ; do
198  [[ -z "$Pattern" ]] && continue
199  [[ "${Pattern:0:1}" == '#' ]] && continue
200  DBGN 2 "Exclude pattern: '${Pattern}'"
201  ExcludedPatterns+=( "$Pattern" )
202  let ++nExcluded
203  done < "$File"
204 
205  DBGN 1 "${nExcluded} exclusion patterns loaded from '${File}'."
206 
207 } # ExcludeFromFile()
208 
209 
210 ################################################################################
211 function Test_dump_prep() {
212 
213  if [[ -z "$fhicldump" ]]; then
214  fhicldump="$(which 'fhicl-dump' 2> /dev/null)"
215  [[ -x "$fhicldump" ]] || FATAL 1 "Couldn't find 'fhicl-dump' executable!"
216  fi
217 
218 } # Test_dump_prep()
219 
220 
221 function Test_dump() {
222 
223  local FHiCLpath="$1"
224 
225  INFO "Testing: '${FHiCLpath}'"
226 
227  local WithPath=0
228  [[ "$FHiCLpath" =~ / ]] && WithPath=1
229 
230  local -a Options
231  [[ "$WithPath" == 1 ]] && Options+=( '-l' 'after1' )
232 
233  $fhicldump --quiet "${Options[@]}" --config "$FHiCLpath"
234  local -i res=$?
235 
236  [[ $res == 0 ]] || ERROR "File '${FHiCLpath}' failed verification (code: ${res})."
237  return $res
238 } # Test_dump()
239 
240 
241 ################################################################################
242 function Test_validate_prep() {
243 
244  if [[ -z "$lar" ]]; then
245  lar="$(which 'lar' 2> /dev/null)"
246  [[ -x "$lar" ]] || FATAL 1 "Couldn't find 'lar' executable!"
247  fi
248 
249 } # Test_validate_prep()
250 
251 
252 function Test_validate() {
253 
254  local FHiCLpath="$1"
255 
256  INFO "Validating: '${FHiCLpath}'"
257 
258  local WithPath=0
259  [[ "$FHiCLpath" =~ / ]] && WithPath=1
260 
261  local -a Options
262 
263  local Cwd="$(pwd)"
264  local -a Cmd=( $lar --validate-config '/dev/null' "${Options[@]}" --config "$FHiCLpath" )
265  "${Cmd[@]}" > /dev/null
266  local -i res=$?
267 
268  if [[ $res != 0 ]]; then
269  ERROR "File '${FHiCLpath}' failed validation (code: ${res})."
270  INFO "Command was:\n${Cwd}\$ ${Cmd[@]}"
271  fi
272  return $res
273 } # Test_validate()
274 
275 
276 ################################################################################
277 function TestFHiCLfile() {
278 
279  local FilePath="$1"
280 
281  if isExcluded "$FilePath" ; then
282  echo "Skipping '${FilePath}' (excluded)"
283  return
284  fi
285 
286  "$TestProc" "$FilePath"
287  local -i res=$?
288  if [[ $res != 0 ]]; then
289  Errors["$FilePath"]="$res"
290  return 1
291  fi
292  return 0
293 } # TestFHiCLfile()
294 
295 
296 function TestFHiCLdirectory() {
297 
298  local DirPath="$1"
299 
300  if [[ ! -d "$DirPath" ]]; then
301  ERROR "Path '${DirPath}' is not a directory."
302  return 1
303  fi
304 
305  if isExcluded "$DirPath" ; then
306  echo "Skipping '${DirPath}' and subdirectories (excluded)"
307  return
308  fi
309 
310  local -i nErrors=0
311  local -i res
312  local FilePath
313  echo "Testing FHiCL files in '${DirPath}' and subdirectories"
314  while read FilePath ; do
315  TestFHiCLfile "$FilePath" || let ++nErrors
316  done < <( find "$DirPath" -name "*.fcl" )
317 
318  [[ $nErrors -gt 0 ]] # return value
319 } # TestFHiCLfile()
320 
321 
322 function TestFHiCLpath() {
323  local Path="$1"
324 
325  if [[ -d "$Path" ]]; then
326  TestFHiCLdirectory "$Path"
327  else
328  TestFHiCLfile "$Path"
329  fi
330 } # TestFHiCLpath()
331 
332 ################################################################################
333 ###
334 ### argument parsing
335 ###
336 declare -a InputPaths
337 declare -a ExcludedPatterns
338 declare TestType="$DefaultTest"
339 declare -i DoHelp=0 DoListTests=0 UseColors=1 Quiet=0
340 declare -i NoMoreOptions=0
341 for (( iParam = 1 ; iParam <= $# ; ++iParam )); do
342  Param="${!iParam}"
343  if [[ $NoMoreOptions == 0 ]] && [[ "${Param:0:1}" == '-' ]]; then
344  case "$Param" in
345  ( '--test='* ) TestType="${Param#--*=}" ;;
346  ( '--exclude='* ) ExcludedPatterns+=( "${Param#--*=}" ) ;;
347  ( '--exclude-from='* ) ExcludedPatternFiles+=( "${Param#--*=}" ) ;;
348  ( '--listtests' | '-L' ) DoListTests=1 ;;
349  ( '--color' ) UseColors=1 ;;
350  ( '--no-color' ) UseColors=0 ;;
351  ( '--quiet' | '-q' ) Quiet=1 ;;
352  ( '--debug' | '-d' ) DEBUG=1 ;;
353  ( '--debug='* ) DEBUG="${Param#--*=}" ;;
354  ( '--help' | '-h' | '-?' ) DoHelp=1 ;;
355  ( '--' | '-' ) NoMoreOptions=1 ;;
356  ( '--'* )
357  if isTestType "${Param#--}" ; then
358  TestType="${Param#--}"
359  else
360  FATAL 1 "Unsupported option: '${Param}'."
361  fi
362  esac
363  [[ $DoHelp == 1 ]] && break
364  else
365  InputPaths+=( "$Param" )
366  fi
367 done
368 
369 for File in "${ExcludedPatternFiles[@]}" ; do
370  ExcludeFromFile "$File"
371 done
372 
373 ################################################################################
374 
375 SetupColors "$UseColors"
376 DebugTest
377 
378 if [[ $DoHelp == 1 ]]; then
379  printHelp
380  exit
381 fi
382 
383 if [[ $DoListTests == 1 ]]; then
384  echo "Supported tests are:"
385  printTests
386  exit
387 fi
388 
389 #
390 # test selection and preparation
391 #
392 if ! SelectTest "$TestType" ; then
393  ERROR "Unrecognised test: '${TestType}'. Supported tests are:"
394  printTests
395  FATAL 1 "Unrecognised test: '${TestType}'."
396 fi
397 
398 
399 [[ "${#InputPaths[@]}" == 0 ]] && InputPaths=( '.' )
400 
401 if [[ -n "$TestPrep" ]]; then
402  "$TestPrep"
403  LASTFATAL 1 "Preparation to test '${TestType}' failed."
404 fi
405 
406 #
407 # the test
408 #
409 declare -A Errors
410 for InputPath in "${InputPaths[@]}" ; do
411 
412  TestFHiCLpath "$InputPath"
413 
414 done
415 
416 if [[ "${#Errors[@]}" -gt 0 ]]; then
417  STDERR 'bright red' "${#Errors[@]} errors found in test '${TestType}':"
418  for Failed in "${!Errors[@]}" ; do
419  STDERR 'bright red' "- '${Failed}' (code: ${Errors[$Failed]})"
420  done
421  exit 1
422 else
423  SUCCESS "All tested files passed the verification."
424  exit 0
425 fi
426 
427 ################################################################################
process_name opflash particleana ie ie ie z
bool printHelp
listtests L DoListTests
then echo FATAL
process_name opflash particleana ie x
function LASTFATAL()
Definition: utilities.sh:25
then echo ERROR
Definition: grid_setup.sh:42
esac done echo Signal files are
Definition: TrainMVA.sh:25
* file
Definition: file_to_url.sh:69
std::string Escape(const std::string &s)
#define the
NoMoreOptions
do one_file $F done echo for F in find $TOP name CMakeLists txt print
SCRIPTNAME
Definition: publish.sh:21
function STDERR()
Definition: utilities.sh:17
shift
Definition: fcl_checks.sh:26
usage
Definition: doGit.sh:21
then echo Sam station was not specified(use option--sam_station)." exit 1 fi if [ x$SAM_GROUP
process_name gaushit a
while getopts h
then local
return match has_match and(match.match_pdg==11 or match.match_pdg==-11)
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 see the discussion of warn common echo in *Note Linker such as a global int variable echo as opposed to a large global array echo echo I echo The symbol is an indirect reference to another symbol This echo is a GNU extension to the a out object file format which is echo rarely used echo echo N echo The symbol is a debugging symbol echo echo R echo The symbol is in a read only data section echo echo S echo The symbol is in an uninitialized data section for small echo objects echo echo T echo The symbol is in the the normal defined echo symbol is used with no error When a weak undefined symbol echo is linked and the symbol is not the value of the echo weak symbol becomes zero with no error echo echo W echo The symbol is a weak symbol that has not been specifically echo tagged as a weak object symbol When a weak defined symbol echo is linked with a normal defined the normal defined echo symbol is used with no error When a weak undefined symbol echo is linked and the symbol is not the value of the echo weak symbol becomes zero with no error echo echo echo The symbol is a stabs symbol in an a out object file In echo this the next values printed are the stabs other echo the stabs desc and the stab type Stabs symbols are echo used to hold debugging information For more information
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
decoded decode D Type
BEGIN_PROLOG sequence::SlidingWindowTriggerPatternsOppositeWindows END_PROLOG simSlidingORM6O6 effSlidingORW output
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
temporary value
BEGIN_PROLOG triggeremu_data_config_icarus settings sequence::triggeremu_data_config_icarus settings PMTADCthresholds sequence::triggeremu_data_config_icarus settings PMTADCthresholds sequence::triggeremu_data_config_icarus settings PMTADCthresholds Pattern
BEGIN_PROLOG hitmakerfive clustermakerfour pfparticlemakerthree showermakertwo END_PROLOG hitmakerfive clustermakerfour pfparticlemakerthree sequence::inline_paths sequence::inline_paths sequence::inline_paths showermakers test
float A
Definition: dedx.py:137
esac echo uname r
do Param
#define SUCCESS
Definition: DBScan3DAlg.h:39