COIN-OR::LEMON - Graph Library

Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • scripts/unify-sources.sh

    r411 r208  
    44HGROOT=`hg root`
    55
    6 # file enumaration modes
    7 
    8 function all_files() {
    9     hg status -a -m -c |
    10     cut -d ' ' -f 2 | grep -E '(\.(cc|h|dox)$|Makefile\.am$)' |
    11     while read file; do echo $HGROOT/$file; done
    12 }
    13 
    14 function modified_files() {
    15     hg status -a -m |
    16     cut -d ' ' -f 2 | grep -E  '(\.(cc|h|dox)$|Makefile\.am$)' |
    17     while read file; do echo $HGROOT/$file; done
    18 }
    19 
    20 function changed_files() {
    21     {
    22         if [ -n "$HG_PARENT1" ]
    23         then
    24             hg status --rev $HG_PARENT1:$HG_NODE -a -m
    25         fi
    26         if [ -n "$HG_PARENT2" ]
    27         then
    28             hg status --rev $HG_PARENT2:$HG_NODE -a -m
    29         fi
    30     } | cut -d ' ' -f 2 | grep -E '(\.(cc|h|dox)$|Makefile\.am$)' |
    31     sort | uniq |
    32     while read file; do echo $HGROOT/$file; done
    33 }
    34 
    35 function given_files() {
    36     for file in $GIVEN_FILES
    37     do
    38         echo $file
    39     done
    40 }
    41 
    42 # actions
    43 
    44 function update_action() {
    45     if ! diff -q $1 $2 >/dev/null
    46     then
    47         echo -n " [$3 updated]"
    48         rm $2
    49         mv $1 $2
    50         CHANGED=YES
    51     fi
    52 }
    53 
    54 function update_warning() {
    55     echo -n " [$2 warning]"
    56     WARNED=YES
    57 }
    58 
    59 function update_init() {
    60     echo Update source files...
    61     TOTAL_FILES=0
    62     CHANGED_FILES=0
    63     WARNED_FILES=0
    64 }
    65 
    66 function update_done() {
    67     echo $CHANGED_FILES out of $TOTAL_FILES files has been changed.
    68     echo $WARNED_FILES out of $TOTAL_FILES files triggered warnings.
    69 }
    70 
    71 function update_begin() {
    72     ((TOTAL_FILES++))
    73     CHANGED=NO
    74     WARNED=NO
    75 }
    76 
    77 function update_end() {
    78     if [ $CHANGED == YES ]
    79     then
    80         ((++CHANGED_FILES))
    81     fi
    82     if [ $WARNED == YES ]
    83     then
    84         ((++WARNED_FILES))
    85     fi
    86 }
    87 
    88 function check_action() {
    89     if [ "$3" == 'tabs' ]
    90     then
    91         PATTERN=$(echo -e '\t')
    92     elif [ "$3" == 'trailing spaces' ]
    93     then
    94         PATTERN='\ +$'
    95     else
    96         PATTERN='*'
    97     fi
    98 
    99     if ! diff -q $1 $2 >/dev/null
    100     then
    101         if [ "$PATTERN" == '*' ]
    102         then
    103             diff $1 $2 | grep '^[0-9]' | sed "s|^\(.*\)c.*$|$2:\1: check failed: $3|g" |
    104               sed "s/:\([0-9]*\),\([0-9]*\):\(.*\)$/:\1:\3 (until line \2)/g"
    105         else
    106             grep -n -E "$PATTERN" $2 | sed "s|^\([0-9]*\):.*$|$2:\1: check failed: $3|g"
    107         fi
    108         FAILED=YES
    109     fi
    110 }
    111 
    112 function check_warning() {
    113     if [ "$2" == 'long lines' ]
    114     then
    115         grep -n -E '.{81,}' $1 | sed "s|^\([0-9]*\):.*$|$1:\1: warning: $2|g"
    116     else
    117         echo "$1: warning: $2"
    118     fi
    119     WARNED=YES
    120 }
    121 
    122 function check_init() {
    123     echo Check source files...
    124     FAILED_FILES=0
    125     WARNED_FILES=0
    126     TOTAL_FILES=0
    127 }
    128 
    129 function check_done() {
    130     echo $FAILED_FILES out of $TOTAL_FILES files has been failed.
    131     echo $WARNED_FILES out of $TOTAL_FILES files triggered warnings.
    132 
    133     if [ $WARNED_FILES -gt 0 -o $FAILED_FILES -gt 0 ]
    134     then
    135         if [ "$WARNING" == 'INTERACTIVE' ]
    136         then
    137             echo -n "Are the files with errors/warnings acceptable? (yes/no) "
    138             while read answer
    139             do
    140                 if [ "$answer" == 'yes' ]
    141                 then
    142                     return 0
    143                 elif [ "$answer" == 'no' ]
    144                 then
    145                     return 1
    146                 fi
    147                 echo -n "Are the files with errors/warnings acceptable? (yes/no) "
    148             done
    149         elif [ "$WARNING" == 'WERROR' ]
    150         then
    151             return 1
    152         fi
    153     fi
    154 }
    155 
    156 function check_begin() {
    157     ((TOTAL_FILES++))
    158     FAILED=NO
    159     WARNED=NO
    160 }
    161 
    162 function check_end() {
    163     if [ $FAILED == YES ]
    164     then
    165         ((++FAILED_FILES))
    166     fi
    167     if [ $WARNED == YES ]
    168     then
    169         ((++WARNED_FILES))
    170     fi
    171 }
    172 
    173 
    174 
    175 # checks
    176 
    177 function header_check() {
    178     if echo $1 | grep -q -E 'Makefile\.am$'
    179     then
    180         return
    181     fi
    182 
     6function update_header() {
    1837    TMP_FILE=`mktemp`
     8    FILE_NAME=$1
    1849
    18510    (echo "/* -*- mode: C++; indent-tabs-mode: nil; -*-
     
    20126 */
    20227"
    203     awk 'BEGIN { pm=0; }
     28        awk 'BEGIN { pm=0; }
    20429     pm==3 { print }
    20530     /\/\* / && pm==0 { pm=1;}
     
    20732     /\*\// && pm==1 { pm=2;}
    20833    ' $1
    209     ) >$TMP_FILE
     34        ) >$TMP_FILE
    21035
    211     "$ACTION"_action "$TMP_FILE" "$1" header
     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
    21240}
    21341
    214 function tabs_check() {
    215     if echo $1 | grep -q -v -E 'Makefile\.am$'
    216     then
    217         OLD_PATTERN=$(echo -e '\t')
    218         NEW_PATTERN='        '
    219     else
    220         OLD_PATTERN='        '
    221         NEW_PATTERN=$(echo -e '\t')
    222     fi
     42function update_tabs() {
    22343    TMP_FILE=`mktemp`
    224     cat $1 | sed -e "s/$OLD_PATTERN/$NEW_PATTERN/g" >$TMP_FILE
     44    FILE_NAME=$1
    22545
    226     "$ACTION"_action "$TMP_FILE" "$1" 'tabs'
     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
    22753}
    22854
    229 function spaces_check() {
     55function remove_trailing_space() {
    23056    TMP_FILE=`mktemp`
    231     cat $1 | sed -e 's/ \+$//g' >$TMP_FILE
     57    FILE_NAME=$1
    23258
    233     "$ACTION"_action "$TMP_FILE" "$1" 'trailing spaces'
     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
    23466}
    23567
    236 function long_lines_check() {
    237     if cat $1 | grep -q -E '.{81,}'
     68function long_line_test() {
     69    cat $1 |grep -q -E '.{81,}'
     70}
     71
     72function update_file() {
     73    echo -n '    update' $i ...
     74
     75    update_header $1
     76    update_tabs $1
     77    remove_trailing_space $1
     78
     79    CHANGED=NO;
     80    if [[ $HEADER_CH = YES ]];
    23881    then
    239         "$ACTION"_warning $1 'long lines'
     82        echo -n '  [header updated]'
     83        CHANGED=YES;
     84    fi
     85    if [[ $TABS_CH = YES ]];
     86    then
     87        echo -n ' [tabs removed]'
     88        CHANGED=YES;
     89    fi
     90    if [[ $SPACES_CH = YES ]];
     91    then
     92        echo -n ' [trailing spaces removed]'
     93        CHANGED=YES;
     94    fi
     95    if long_line_test $1 ;
     96    then
     97        echo -n ' [LONG LINES]'
     98        ((LONG_LINE_FILES++))
     99    fi
     100    echo
     101    if [[ $CHANGED = YES ]];
     102    then
     103        ((CHANGED_FILES++))
    240104    fi
    241105}
    242106
    243 # process the file
    244 
    245 function process_file() {
    246     if [ "$ACTION" == 'update' ]
    247     then
    248         echo -n "    $ACTION $1..."
    249     else
    250         echo "    $ACTION $1..."
    251     fi
    252 
    253     CHECKING="header tabs spaces long_lines"
    254 
    255     "$ACTION"_begin $1
    256     for check in $CHECKING
     107CHANGED_FILES=0
     108TOTAL_FILES=0
     109LONG_LINE_FILES=0
     110if [ $# == 0 ]; then
     111    echo Update all source files...
     112    for i in `hg manifest|grep -E  '\.(cc|h|dox)$'`
    257113    do
    258         "$check"_check $1
     114        update_file $HGROOT/$i
     115        ((TOTAL_FILES++))
    259116    done
    260     "$ACTION"_end $1
    261     if [ "$ACTION" == 'update' ]
    262     then
    263         echo
    264     fi
    265 }
    266 
    267 function process_all {
    268     "$ACTION"_init
    269     while read file
     117    echo '  done.'
     118else
     119    for i in $*
    270120    do
    271         process_file $file
    272     done < <($FILES)
    273     "$ACTION"_done
    274 }
    275 
    276 while [ $# -gt 0 ]
    277 do
    278    
    279     if [ "$1" == '--help' ] || [ "$1" == '-h' ]
    280     then
    281         echo -n \
    282 "Usage:
    283   $0 [OPTIONS] [files]
    284 Options:
    285   --dry-run|-n
    286      Check the files, but do not modify them.
    287   --interactive|-i
    288      If --dry-run is specified and the checker emits warnings,
    289      then the user is asked if the warnings should be considered
    290      errors.
    291   --werror|-w
    292      Make all warnings into errors.
    293   --all|-a
    294      Check all source files in the repository.
    295   --modified|-m
    296      Check only the modified (and new) source files. This option is
    297      useful to check the modification before making a commit.
    298   --changed|-c
    299      Check only the changed source files compared to the parent(s) of
    300      the current hg node.  This option is useful as hg hook script.
    301      To automatically check all your changes before making a commit,
    302      add the following section to the appropriate .hg/hgrc file.
    303 
    304        [hooks]
    305        pretxncommit.checksources = scripts/unify-sources.sh -c -n -i
    306 
    307   --help|-h
    308      Print this help message.
    309   files
    310      The files to check/unify. If no file names are given, the modified
    311      source files will be checked/unified (just like using the
    312      --modified|-m option).
    313 "
    314         exit 0
    315     elif [ "$1" == '--dry-run' ] || [ "$1" == '-n' ]
    316     then
    317         [ -n "$ACTION" ] && echo "Conflicting action options" >&2 && exit 1
    318         ACTION=check
    319     elif [ "$1" == "--all" ] || [ "$1" == '-a' ]
    320     then
    321         [ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
    322         FILES=all_files
    323     elif [ "$1" == "--changed" ] || [ "$1" == '-c' ]
    324     then
    325         [ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
    326         FILES=changed_files
    327     elif [ "$1" == "--modified" ] || [ "$1" == '-m' ]
    328     then
    329         [ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
    330         FILES=modified_files
    331     elif [ "$1" == "--interactive" ] || [ "$1" == "-i" ]
    332     then
    333         [ -n "$WARNING" ] && echo "Conflicting warning options" >&2 && exit 1
    334         WARNING='INTERACTIVE'
    335     elif [ "$1" == "--werror" ] || [ "$1" == "-w" ]
    336     then
    337         [ -n "$WARNING" ] && echo "Conflicting warning options" >&2 && exit 1
    338         WARNING='WERROR'
    339     elif [ $(echo x$1 | cut -c 2) == '-' ]
    340     then
    341         echo "Invalid option $1" >&2 && exit 1
    342     else
    343         [ -n "$FILES" ] && echo "Invalid option $1" >&2 && exit 1
    344         GIVEN_FILES=$@
    345         FILES=given_files
    346         break
    347     fi
    348    
    349     shift
    350 done
    351 
    352 if [ -z $FILES ]
    353 then
    354     FILES=modified_files
     121        update_file $i
     122        ((TOTAL_FILES++))
     123    done
    355124fi
    356 
    357 if [ -z $ACTION ]
    358 then
    359     ACTION=update
     125echo $CHANGED_FILES out of $TOTAL_FILES files has been changed.
     126if [[ $LONG_LINE_FILES -gt 1 ]]; then
     127    echo
     128    echo WARNING: $LONG_LINE_FILES files contains long lines!   
     129    echo
     130elif [[ $LONG_LINE_FILES -gt 0 ]]; then
     131    echo
     132    echo WARNING: a file contains long lines!
     133    echo
    360134fi
    361 
    362 process_all
Note: See TracChangeset for help on using the changeset viewer.