COIN-OR::LEMON - Graph Library

Ticket #138: scripts_f0ce608e320e.patch

File scripts_f0ce608e320e.patch, 8.7 KB (added by Peter Kovacs, 13 years ago)
  • scripts/unify-sources.sh

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1224320772 -7200
    # Node ID f0ce608e320e360021419137604c66a09d66762f
    # Parent  6dbd5184c6a99e936a7de899e64c114e30c0f94f
    Reworked unifier script (ticket #138)
    
    The new script can be used either to check and to unify the source
    files. It can also be used as a hook script that checks all the modified
    source files. For more information run the script with --help option.
    
    Other changes:
     - The Makefile.am files are also checked/unified.
     - Long lines are checked only for .h and .cc files.
     - The way how we enumerate the code files is changed because the
       manifest command also enumerates the removed files, which raised
       error in the script.
     - Add --check and --modified options for checking.
     - Add --help option for getting help.
    
    diff --git a/scripts/unify-sources.sh b/scripts/unify-sources.sh
    a b  
    2525 *
    2626 */
    2727"
    28         awk 'BEGIN { pm=0; }
     28    awk 'BEGIN { pm=0; }
    2929     pm==3 { print }
    3030     /\/\* / && pm==0 { pm=1;}
    3131     /[^:blank:]/ && (pm==0 || pm==2) { pm=3; print;}
    3232     /\*\// && pm==1 { pm=2;}
    3333    ' $1
    34         ) >$TMP_FILE
    35 
    36     HEADER_CH=`diff -q $TMP_FILE $FILE_NAME >/dev/null&&echo NO||echo YES`
    37 
    38     rm $FILE_NAME
    39     mv $TMP_FILE $FILE_NAME
     34       ) >$TMP_FILE
     35       
     36    if [[ $CHECK_MODE -gt 0 ]];
     37    then
     38        if ! diff -q $TMP_FILE $FILE_NAME >/dev/null ;
     39        then
     40            echo "header failed"
     41            FILE_FAILED=YES
     42        fi
     43    else
     44        HEADER_CH=`diff -q $TMP_FILE $FILE_NAME >/dev/null&&echo NO||echo YES`
     45        rm $FILE_NAME
     46        mv $TMP_FILE $FILE_NAME
     47    fi
    4048}
    4149
    4250function update_tabs() {
    43     TMP_FILE=`mktemp`
    44     FILE_NAME=$1
     51    if echo $1 | grep -q -v -E 'Makefile\.am$' ;
     52    then
     53        OLD_PATTERN=`echo -e '\t'`
     54        NEW_PATTERN='        '
     55    else
     56        OLD_PATTERN='        '
     57        NEW_PATTERN=`echo -e '\t'`
     58    fi
    4559
    46     cat $1 |
    47     sed -e 's/\t/        /g' >$TMP_FILE
    48 
    49     TABS_CH=`diff -q $TMP_FILE $FILE_NAME >/dev/null&&echo NO||echo YES`
    50 
    51     rm $FILE_NAME
    52     mv $TMP_FILE $FILE_NAME
     60    if [[ $CHECK_MODE -gt 0 ]];
     61    then
     62        if grep -q -E "$OLD_PATTERN" $1 ;
     63        then
     64            echo "tabs failed:"
     65            grep -n -E "$OLD_PATTERN" $1
     66            FILE_FAILED=YES
     67        fi
     68    else
     69        TMP_FILE=`mktemp`
     70        cat $1 | sed -e "s/$OLD_PATTERN/$NEW_PATTERN/g" >$TMP_FILE
     71        TABS_CH=`diff -q $TMP_FILE $1 >/dev/null&&echo NO||echo YES`
     72        rm $1
     73        mv $TMP_FILE $1
     74    fi
    5375}
    5476
    55 function remove_trailing_space() {
    56     TMP_FILE=`mktemp`
    57     FILE_NAME=$1
    58 
    59     cat $1 |
    60     sed -e 's/ \+$//g' >$TMP_FILE
    61 
    62     SPACES_CH=`diff -q $TMP_FILE $FILE_NAME >/dev/null&&echo NO||echo YES`
    63 
    64     rm $FILE_NAME
    65     mv $TMP_FILE $FILE_NAME
     77function remove_trailing_spaces() {
     78    if [[ $CHECK_MODE -gt 0 ]];
     79    then
     80        if grep -q -E ' $' $1 ;
     81        then
     82            echo "trailing spaces failed:"
     83            grep -n -E ' $' $1
     84            FILE_FAILED=YES
     85        fi
     86    else
     87        TMP_FILE=`mktemp`
     88        cat $1 | sed -e 's/ \+$//g' >$TMP_FILE
     89        SPACES_CH=`diff -q $TMP_FILE $1 >/dev/null&&echo NO||echo YES`
     90        rm $1
     91        mv $TMP_FILE $1
     92    fi
    6693}
    6794
    6895function long_line_test() {
    69     cat $1 |grep -q -E '.{81,}'
     96    cat $1 | grep -q -E '.{81,}'
    7097}
    7198
    7299function update_file() {
    73     echo -n '    update' $i ...
     100    echo -n '    update' $1...
    74101
    75     update_header $1
     102    if echo $1 | grep -q -v -E 'Makefile\.am$' ;
     103    then
     104        update_header $1
     105    fi
    76106    update_tabs $1
    77     remove_trailing_space $1
     107    remove_trailing_spaces $1
    78108
    79     CHANGED=NO;
     109    CHANGED=NO
    80110    if [[ $HEADER_CH = YES ]];
    81111    then
    82         echo -n '  [header updated]'
    83         CHANGED=YES;
     112        echo -n '  [header updated]'
     113        CHANGED=YES
    84114    fi
    85115    if [[ $TABS_CH = YES ]];
    86116    then
    87         echo -n ' [tabs removed]'
    88         CHANGED=YES;
     117        if echo $1 | grep -q -v -E 'Makefile\.am$' ;
     118        then
     119            echo -n ' [tabs removed]'
     120        else
     121            echo -n ' [spaces replaced with tabs]'
     122        fi
     123        CHANGED=YES
    89124    fi
    90125    if [[ $SPACES_CH = YES ]];
    91126    then
    92         echo -n ' [trailing spaces removed]'
    93         CHANGED=YES;
     127        echo -n ' [trailing spaces removed]'
     128        CHANGED=YES
    94129    fi
    95     if long_line_test $1 ;
     130    if echo $1 | grep -q -v -E '\.dox|Makefile\.am$' ;
    96131    then
    97         echo -n ' [LONG LINES]'
    98         ((LONG_LINE_FILES++))
     132        if long_line_test $1 ;
     133        then
     134            echo -n ' [LONG LINES]'
     135            ((LONG_LINE_FILES++))
     136        fi
    99137    fi
    100138    echo
    101139    if [[ $CHANGED = YES ]];
    102140    then
    103         ((CHANGED_FILES++))
     141        ((CHANGED_FILES++))
    104142    fi
     143}
     144
     145function check_file() {
     146    echo '    check' $1...
     147
     148    FILE_FAILED=NO
     149    if echo $1 | grep -q -v -E 'Makefile\.am$' ;
     150    then
     151        update_header $1
     152    fi
     153    update_tabs $1
     154    remove_trailing_spaces $1
     155    if echo $1 | grep -q -v -E '\.dox|Makefile\.am$' ;
     156    then
     157        if long_line_test $1 ;
     158        then
     159            echo 'long lines failed:'
     160            grep -n -E '.{81,}' $1
     161            FILE_FAILED=YES
     162        fi
     163    fi
     164
     165    if [[ $FILE_FAILED = YES ]];
     166    then
     167        GLOBAL_FAILED=YES
     168        ((FAILED_FILES++))
     169    fi
     170}
     171
     172function list_files() {
     173    hg status -a -m -c |
     174    cut -d ' ' -f 2 | grep -E  '(\.(cc|h|dox)$|Makefile\.am$)' | sort | uniq
     175}
     176
     177function changed_files() {
     178    {
     179        if [ -n "$HG_PARENT1" ]
     180        then
     181            hg status --rev $HG_PARENT1:$HG_NODE -a -m
     182        fi
     183        if [ -n "$HG_PARENT2" ]
     184        then
     185            hg status --rev $HG_PARENT2:$HG_NODE -a -m
     186        fi
     187    } | cut -d ' ' -f 2 | grep -E '(\.(cc|h|dox)$|Makefile\.am$)' | sort | uniq
    105188}
    106189
    107190CHANGED_FILES=0
    108191TOTAL_FILES=0
    109192LONG_LINE_FILES=0
    110 if [ $# == 0 ]; then
     193FAILED_FILES=0
     194GLOBAL_FAILED=NO
     195CHECK_MODE=0
     196
     197if [ $# == 0 ];
     198then
    111199    echo Update all source files...
    112     for i in `hg manifest|grep -E  '\.(cc|h|dox)$'`
     200    while read i
    113201    do
    114         update_file $HGROOT/$i
    115         ((TOTAL_FILES++))
    116     done
     202        update_file "$HGROOT/$i"
     203        ((TOTAL_FILES++))
     204    done < <(list_files)
    117205    echo '  done.'
     206elif [[ ($1 == "--modified") || ($1 == "-modified") || ($1 == "-m") ]];
     207then
     208    CHECK_MODE=2
     209    echo Check all modified source files...
     210    while read i
     211    do
     212        check_file "$HGROOT/$i"
     213        ((TOTAL_FILES++))
     214    done < <(changed_files)
     215    echo '  done.'
     216elif [[ ($1 == "--check") || ($1 == "-check") || ($1 == "-c") ]];
     217then
     218    CHECK_MODE=1
     219    if [[ $# == 1 ]];
     220    then
     221        echo Check all source files...
     222        while read i
     223        do
     224            check_file "$HGROOT/$i"
     225            ((TOTAL_FILES++))
     226        done < <(list_files)
     227        echo '  done.'
     228    else
     229        echo Check files...
     230        shift
     231        for i in $@
     232        do
     233            check_file $i
     234            ((TOTAL_FILES++))
     235        done
     236    fi
     237elif [[ ($1 == "--help") || ($1 == "-help") || ($1 == "-h") ]];
     238then
     239    echo "Usage:
     240  $0 [(--check|-c)|(--modified|-m)|(--help|-h)] [files]
     241Where:
     242  --check|-c
     243     Check the given files, but do not modify them.
     244  --modified|-m
     245     Check only the modified source files, but do not modify them.
     246     This option can be used as a hg hook script. E.g. to check all your
     247     commited source files with this script add the following section to
     248     the appropriate .hg/hgrc file.
     249
     250       [hooks]
     251       pretxncommit.checksources = scripts/unify-sources.sh --modified
     252
     253  --help|-h
     254     Print this help message.
     255  files
     256     The files to check/unify. If no file names are given, all source
     257     will be checked/unified (except for using --modified, which checks
     258     only the modified source files).
     259     If no option is given, the script unifies the given files, but if
     260     --check or --modified is given, it only checks them."
     261    exit
    118262else
    119     for i in $*
     263    echo Update files...
     264    for i in $@
    120265    do
    121         update_file $i
    122         ((TOTAL_FILES++))
     266        update_file $i
     267        ((TOTAL_FILES++))
    123268    done
    124269fi
    125 echo $CHANGED_FILES out of $TOTAL_FILES files has been changed.
    126 if [[ $LONG_LINE_FILES -gt 1 ]]; then
     270
     271if [[ $CHECK_MODE -gt 0 ]];
     272then
     273    if [[ $FAILED_FILES -gt 0 ]];
     274    then
     275        echo $FAILED_FILES out of $TOTAL_FILES files has failed.
     276    else
     277        echo All $TOTAL_FILES files passed.
     278    fi
     279else
     280    echo $CHANGED_FILES out of $TOTAL_FILES files has been changed.
     281fi
     282
     283if [[ $LONG_LINE_FILES -gt 1 ]];
     284then
    127285    echo
    128     echo WARNING: $LONG_LINE_FILES files contains long lines!   
     286    echo WARNING: $LONG_LINE_FILES files contain long lines!
    129287    echo
    130 elif [[ $LONG_LINE_FILES -gt 0 ]]; then
     288elif [[ $LONG_LINE_FILES -gt 0 ]];
     289then
    131290    echo
    132291    echo WARNING: a file contains long lines!
    133292    echo
    134293fi
     294 
     295[[ $GLOBAL_FAILED == NO ]]