/
githubmirror
/
cmssw
Обзор
Документация
Войти
/
githubmirror
/
cmssw
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
FWCore/ParameterSet/scripts/edmConfigTree
172 строки
7 KB
Giulio Eulisse
Snapshot of CMSSW_6_2_0_pre8.
26 июн 2013, 18:12
26 июн 2013, 18:12
214ab73
Код
Авторство
О чём код?
#!/bin/bash # Deep-searches CMSSW config python files, and displays the tree structure of all imported files in it. # K. Banicz function findFileFor(){ # Return 0 if a file given by absolute path exists, or it converts a python name # to a file name and returns 0 if it finds it in CMSSW_SEARCH_PATH. # argument 1: the sought file, or the python name to seek the corresponding file for local FILE_NAME if [ ${1:0:1} == "/" ]; then [ -f $1 ] && FOUND_FILE=$1 && return 0 else FILE_NAME=$( echo $1 | sed -n -e 's:\([[:alnum:]_]\+\)[/.]\([[:alnum:]_]\+\)[/.]\([[:alnum:]_/.]\+\)$:\1/\2/python/\3:p' | sed -e 's:[\.]:/:g' ).py IFS=: for SEARCH_PATH in $CMSSW_SEARCH_PATH; do [ -f $SEARCH_PATH/$FILE_NAME ] && FOUND_FILE=$SEARCH_PATH/$FILE_NAME && return 0 done fi # Not found. FOUND_FILE="" return 1 } function findInFile(){ # Finds all occurrences of the sought expression in the file. # Invokes itself on all included files too. # argument 1: indent # argument 2: the searched file # argument 3: the sought expression # # Load 'import'-s and 'load'-s into an array (discard FWCore.ParameterSet as it leads to an infinite loop) # # sed'd regex below will extract the word in capital from the following example lines ('...' means any string): # import FINDS.THIS.WORD ... # from FINDS.THIS.WORD ... # execfile("FINDS/THIS/WORD") # process.load("FINDS.THIS/WORD") # And the same things inside an 'exec' statement, too: # exec 'import FINDS.THIS.WORD ...' # exec "from FINDS.THIS.WORD ..." # exec 'execfile("FINDS/THIS/WORD")' # exec 'process.load("FINDS.THIS/WORD")' IFS=" " local -a IMPORTS_AND_LOADS_ARRAY IMPORTS_AND_LOADS_ARRAY=( $( sed -n -e "s:^[[:space:]]*\(exec[[:space:]]\+[\"']\)\?\(from[[:space:]]\+\|import[[:space:]]\+\|process\.load[[:space:]]*([[:space:]]*[\"']\|execfile[[:space:]]*([[:space:]]*[\"']\)[[:space:]]*\(\([^[:space:])\"'#]\+[/.]\)\+[^[:space:])\"'#]\+\).*:\3:p" $2 | grep -v "^FWCore.ParameterSet" ) ) # # First list all occurrences in this file: # if [ ${#3} -ne 0 ]; then if [ $EDMCONFIGTREE_TREE == "false" ]; then grep -H -n $3 $2 else grep $3 $2 | while read LINE_WITH_MATCH; do if [ $EDMCONFIGTREE_COLOR == "false" ]; then [ ${#IMPORTS_AND_LOADS_ARRAY[@]} -gt 0 ] && echo -n "$1 |" || echo -n "$1 " echo " $LINE_WITH_MATCH" | grep $3 else [ ${#IMPORTS_AND_LOADS_ARRAY[@]} -gt 0 ] && echo -n "$1 [0;34m|[0m" || echo -n "$1 " echo " $LINE_WITH_MATCH" | grep --color $3 fi done fi fi # # Now find all 'import'-ed and 'process.load'-ed files and search them too # local I for (( I=0; I<${#IMPORTS_AND_LOADS_ARRAY[@]}; I++ )); do local PYTHON_NAME=${IMPORTS_AND_LOADS_ARRAY[$I]} local INDENT local II ((II=I+1)) if [ $EDMCONFIGTREE_TREE == "false" ]; then if findFileFor "$PYTHON_NAME"; then [ $SHOW_ABSOLUTE_PATH == "true" ] && echo "$FOUND_FILE" || echo "$PYTHON_NAME" findInFile "" "$FOUND_FILE" "$3" else echo "${PYTHON_NAME} *** not found in CMSSW_SEARCH_PATH ***" fi else if findFileFor "$PYTHON_NAME"; then [ $SHOW_ABSOLUTE_PATH == "true" ] && NAME_TO_SHOW=FOUND_FILE || NAME_TO_SHOW=PYTHON_NAME if [ $EDMCONFIGTREE_COLOR == "false" ]; then echo "$1 |- ${!NAME_TO_SHOW}" [ $II -ge ${#IMPORTS_AND_LOADS_ARRAY[@]} ] && INDENT="$1 " || INDENT="$1 |" else [[ $SHOW_ABSOLUTE_PATH == "true" || ${PYTHON_NAME:0:1} == "/" ]] && echo "$1 [1;44m [0m [33m${FOUND_FILE%%*([^/])}[0m${FOUND_FILE##*/}" || echo "$1 [1;44m [0m [33m${PYTHON_NAME%%*([^.])}[0m${PYTHON_NAME##*.}" [ $II -ge ${#IMPORTS_AND_LOADS_ARRAY[@]} ] && INDENT="$1 " || INDENT="$1 [0;34m|[0m" fi findInFile "$INDENT" "$FOUND_FILE" "$3" else if [ $EDMCONFIGTREE_COLOR == "false" ]; then echo "$1 |- ${PYTHON_NAME} *** not found in CMSSW_SEARCH_PATH ***" else [ ${PYTHON_NAME:0:1} == "/" ] && echo "$1 [1;44m [0m [33m${PYTHON_NAME%%*([^/])}[0m${PYTHON_NAME##*/}" || echo -n "$1 [1;44m [0m [33m${PYTHON_NAME%%*([^.])}[0m${PYTHON_NAME##*.} " echo "[43m not found in CMSSW_SEARCH_PATH [0m" fi fi fi done } # Take care of the relevant shell variables. ${EDMCONFIGTREE_COLOR:=true} ${EDMCONFIGTREE_TREE:=true} [ $EDMCONFIGTREE_TREE == "false" ] && EDMCONFIGTREE_COLOR=false # Invoked with a wrong number of arguments. Give help. if [ $# -eq 0 ] || [ $# -gt 2 ]; then cat <<EOF edmConfigTree displays the complete tree of imported files in a given CMSSW configuration file, and optionally searches them recursively for a regular expression. Usage: [34medmConfigTree [regex] configuration_file[0m regex : regular expression, as with grep configuration_file: CMSSW configuration python file (Give absolute path to display imported files' resolved absolute path.) Note that the shell variable CMSSW_SEARCH_PATH must be set before edmConfigTree is invoked. To suppress colors, set the shell variable EDMCONFIGTREE_COLOR=false To suppress the tree structure (and the colors), set the shell variable EDMCONFIGTREE_TREE=false Examples: Display the tree of all imported files in FullChainExample_cfg.py: [34medmConfigTree FullChainExample_cfg.py[0m Display with absolute paths the tree of all imported files in FullChainExample_cfg.py: [34medmConfigTree /full/path/to/FullChainExample_cfg.py[0m Recursively search all imported files for "Verbosity": [34medmConfigTree Verbosity FullChainExample_cfg.py[0m Recursively search all imported files for "Time" or "time": [34medmConfigTree "[Tt]ime" FullChainExample_cfg.py[0m EOF exit 1 fi # Exit if CMSSW_SEARCH_PATH is not set. if [ ${#CMSSW_SEARCH_PATH} -eq 0 ]; then echo "CMSSW_SEARCH_PATH is not set. Exiting." exit 1 fi # Set extglob for extended pattern matching features in parameter expansion. shopt -s extglob # If invoked with one argument, list files. if [ $# -eq 1 ]; then FILE=$1 [ ${FILE:0:1} == "/" ] && SHOW_ABSOLUTE_PATH=true || SHOW_ABSOLUTE_PATH=false [ $EDMCONFIGTREE_COLOR == "false" ] && echo "$FILE" || echo "[1;44m [0m [33m${FILE%%*([^/])}[0m${FILE##*/}" findInFile " " $1 fi # If invoked with two arguments, list files and search them. if [ $# -eq 2 ]; then FILE=$2 [ ${FILE:0:1} == "/" ] && SHOW_ABSOLUTE_PATH=true || SHOW_ABSOLUTE_PATH=false [ $EDMCONFIGTREE_COLOR == "false" ] && echo "$FILE" || echo "[1;44m [0m [33m${FILE%%*([^/])}[0m${FILE##*/}" findInFile " " $2 "$1" fi