From eb238ecd71a9302011879952f5225f9de33c660c Mon Sep 17 00:00:00 2001 From: Alain Reguera Delgado Date: May 07 2011 14:24:58 +0000 Subject: Update cli_getRepoName: - Start using getopt to handle options and non-option arguments. --- diff --git a/Scripts/Functions/cli_getRepoName.sh b/Scripts/Functions/cli_getRepoName.sh index aaa3198..ba4c0c9 100755 --- a/Scripts/Functions/cli_getRepoName.sh +++ b/Scripts/Functions/cli_getRepoName.sh @@ -1,9 +1,10 @@ #!/bin/bash # -# cli_getRepoName.sh -- This function sets naming convenction. Inside -# CentOS Artowrk Repository, regular files are written in lower case +# cli_getRepoName.sh -- This function standardize file and directories +# name convenction inside the working copy of CentOS Artowrk +# Repository. As convenction, regular files are written in lower case # and directories are written in lower case but with the first letter -# in upper case. Use this function to sanitate the name of regular +# in upper case. Use this function to sanitate the name of regular # files and directory components of paths you work with. # # Copyright (C) 2009, 2010, 2011 The CentOS Project @@ -28,125 +29,130 @@ function cli_getRepoName { - local NAME="$1" - local TYPE="$2" - local DIRS='' - local DIR='' - local CLEANDIRS='' - local PREFIXDIR='' - - case $TYPE in - - 'f' | 'basename' ) - - # Reduce the path passed to use just the non-directory - # part of it (i.e., the last component in the path; _not_ - # the last "real" directory in the path). - NAME=$(basename $NAME) - - # Clean value. - NAME=$(echo $NAME \ - | tr -s ' ' '_' \ - | tr '[:upper:]' '[:lower:]') - ;; - - 'd' | 'dirname' ) - - # Reduce path information passed to use just the directory - # part of it. Of course, this is applied only if there is - # a directory part in the path. However, if there is no - # directory part but there is a non-empty value in the - # path, assume that value as directory part and clean it - # up. - if [[ $NAME =~ '.+/.+' ]];then - - # When path information is reduced, we need to take - # into account that absolute path may be provided. - # Absolute paths include directory structures outside - # the repository directory structure we don't want to - # sanitate (e.g., /home/, /home/centos/, - # /home/centos/artwork, /home/centos/artwork/turnk/, - # trunk/, etc.). In these cases, it is required that - # those path component remain untouched. So, in the - # sake of keeping path components, outside repository - # directory structure untouched, we use the PREFIXDIR - # variable to temporarly store the prefix directory - # structure we don't want to sanitate. - PREFIXDIR=$(echo $NAME \ - | sed -r "s,^((${HOME}/artwork/)?(trunk|branches|tags)/).+$,\1,") - - # ... and remove it from the path information we do - # want to sanitate. - DIRS=$(dirname "$NAME" \ - | sed -r "s!^${PREFIXDIR}!!" \ - | tr '/' ' ') - - else - - # At this point, there is not directory part in the - # information passed, so use the value passed as - # directory part as such. - DIRS=$NAME + # Define short options. + local ARGSS='f,d' - fi + # Define long options. + local ARGSL='basename,dirname' - for DIR in $DIRS;do + # Initialize arguments with an empty value and set it as local + # variable to this function scope. + local ARGUMENTS='' - # Sanitate directory component. - if [[ $DIR =~ '^[a-z]' ]];then - DIR=$(echo ${DIR} \ - | tr -s ' ' '_' \ - | tr '[:upper:]' '[:lower:]' \ - | sed -r 's/^([[:alpha:]])/\u\1/') - fi + # Redefine ARGUMENTS variable using current positional parameters. + cli_doParseArgumentsReDef "$@" + + # Redefine ARGUMENTS variable using getopt output. + cli_doParseArguments + + # Redefine positional parameters using ARGUMENTS variable. + eval set -- "$ARGUMENTS" - # Rebuild path using sanitated values. - CLEANDIRS="${CLEANDIRS}/$DIR" + # Define the name we want to apply verifications to. + local NAME=$(echo $@ | sed -r 's!^.*--[[:space:]](.+)$!\1!') - done + # Look for options passed through positional parameters. + while true;do - # Redefine path using sanitated values. - NAME=$(echo ${CLEANDIRS} | sed -r "s!^/!!") + case "$1" in - # Add prefix directory information to sanitated path - # information. - if [[ "$PREFIXDIR" != '' ]];then - NAME=${PREFIXDIR}${NAME} - fi - ;; + -f|--basename ) - 'fd' | 'basename-to-dirname' ) + # Reduce the path passed to use just the non-directory + # part of it (i.e., the last component in the path; + # _not_ the last "real" directory in the path). + NAME=$(basename $NAME) - # Retrive non-directory part. - NAME=$(cli_getRepoName $NAME 'f') + # Clean value. + NAME=$(echo $NAME \ + | tr -s ' ' '_' \ + | tr '[:upper:]' '[:lower:]') - # Retrive cleaned directory part from non-directory part. - NAME=$(cli_getRepoName $NAME 'd') - ;; + shift 1 + ;; - 'df' | 'dirname-to-basename' ) + -d|--dirname ) - # Retrive cleaned directory part from non-directory part. - NAME=$(cli_getRepoName $NAME 'd') + local DIR='' + local DIRS='' + local CLEANDIRS='' + local PREFIXDIR='' - # Retrive non-directory part. - NAME=$(cli_getRepoName $NAME 'f') - ;; - - 'dfd' | 'dirname-to-basename-to-dirname' ) + # In order to sanitate each directory in a path, it is + # required to break off the path string so each + # component can be worked out individually and later + # combine them back to create a clean path string. + + # Reduce path information passed to use the directory + # part of it only. Of course, this is applied if + # there is a directory part in the path. Assuming + # there is no directory part but a non-empty value in + # the path, use that value as directory part and clean + # it up. + if [[ $NAME =~ '.+/.+' ]];then + + # When path information is reduced, we need to + # consider that absolute paths contain some + # directories outside the working copy directory + # structure that shouldn't be sanitated (e.g., + # /home, /home/centos, /home/centos/artwork, + # /home/centos/artwork/turnk, trunk, etc.) + # So, we keep them unchaged for later use. + PREFIXDIR=$(echo $NAME \ + | sed -r "s,^(($(cli_getRepoTLDir)/)?(trunk|branches|tags)/).+$,\1,") + + # ... and remove them from the path information we + # do want to sanitate. + DIRS=$(dirname "$NAME" \ + | sed -r "s!^${PREFIXDIR}!!" \ + | tr '/' ' ') + + else + + # At this point, there is not directory part in + # the information passed, so use the value passed + # as directory part as such. + DIRS=$NAME + + fi + + for DIR in $DIRS;do + + # Sanitate directory component. + if [[ $DIR =~ '^[a-z]' ]];then + DIR=$(echo ${DIR} \ + | tr -s ' ' '_' \ + | tr '[:upper:]' '[:lower:]' \ + | sed -r 's/^([[:alpha:]])/\u\1/') + fi + + # Rebuild path using sanitated values. + CLEANDIRS="${CLEANDIRS}/$DIR" + + done + + # Redefine path using sanitated values. + NAME=$(echo ${CLEANDIRS} | sed -r "s!^/!!") + + # Add prefix directory information to sanitated path + # information. + if [[ "$PREFIXDIR" != '' ]];then + NAME=${PREFIXDIR}${NAME} + fi - # Retrive cleaned directory part from non-directory part. - NAME=$(cli_getRepoName $NAME 'd') + shift 1 + ;; - # Retrive non-directory part. - NAME=$(cli_getRepoName $NAME 'f') + -- ) + shift 1 + break + ;; + + esac - # Retrive cleaned directory part from non-directory part. - NAME=$(cli_getRepoName $NAME 'd') - ;; - esac + done - # Output clean path information. + # Print out the clean path string. echo $NAME }