scripts/unify-sources.sh
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 03 Apr 2009 18:59:15 +0200
changeset 655 6ac5d9ae1d3d
parent 353 f8832dc16d45
child 601 6b9c1f6eb1e5
permissions -rwxr-xr-x
Support real types + numerical stability fix in NS (#254)

- Real types are supported by appropriate inicialization.
- A feature of the XTI spanning tree structure is removed to ensure
numerical stability (could cause problems using integer types).
The node potentials are updated always on the lower subtree,
in order to prevent overflow problems.
The former method isn't notably faster during to our tests.
alpar@38
     1
#!/bin/bash
alpar@38
     2
alpar@38
     3
YEAR=`date +2003-%Y`
alpar@38
     4
HGROOT=`hg root`
alpar@38
     5
deba@336
     6
# file enumaration modes
deba@336
     7
deba@336
     8
function all_files() {
deba@336
     9
    hg status -a -m -c |
deba@336
    10
    cut -d ' ' -f 2 | grep -E '(\.(cc|h|dox)$|Makefile\.am$)' |
deba@336
    11
    while read file; do echo $HGROOT/$file; done
deba@336
    12
}
deba@336
    13
deba@336
    14
function modified_files() {
deba@336
    15
    hg status -a -m |
deba@336
    16
    cut -d ' ' -f 2 | grep -E  '(\.(cc|h|dox)$|Makefile\.am$)' |
deba@336
    17
    while read file; do echo $HGROOT/$file; done
deba@336
    18
}
deba@336
    19
deba@336
    20
function changed_files() {
deba@336
    21
    {
deba@336
    22
        if [ -n "$HG_PARENT1" ]
deba@336
    23
        then
deba@336
    24
            hg status --rev $HG_PARENT1:$HG_NODE -a -m
deba@336
    25
        fi
deba@336
    26
        if [ -n "$HG_PARENT2" ]
deba@336
    27
        then
deba@336
    28
            hg status --rev $HG_PARENT2:$HG_NODE -a -m
deba@336
    29
        fi
deba@336
    30
    } | cut -d ' ' -f 2 | grep -E '(\.(cc|h|dox)$|Makefile\.am$)' | 
deba@336
    31
    sort | uniq |
deba@336
    32
    while read file; do echo $HGROOT/$file; done
deba@336
    33
}
deba@336
    34
deba@336
    35
function given_files() {
deba@336
    36
    for file in $GIVEN_FILES
deba@336
    37
    do
deba@336
    38
	echo $file
deba@336
    39
    done
deba@336
    40
}
deba@336
    41
deba@336
    42
# actions
deba@336
    43
deba@336
    44
function update_action() {
deba@336
    45
    if ! diff -q $1 $2 >/dev/null
deba@336
    46
    then
deba@336
    47
	echo -n " [$3 updated]"
deba@336
    48
	rm $2
deba@336
    49
	mv $1 $2
deba@336
    50
	CHANGED=YES
deba@336
    51
    fi
deba@336
    52
}
deba@336
    53
deba@336
    54
function update_warning() {
deba@336
    55
    echo -n " [$2 warning]"
deba@336
    56
    WARNED=YES
deba@336
    57
}
deba@336
    58
deba@336
    59
function update_init() {
deba@336
    60
    echo Update source files...
deba@336
    61
    TOTAL_FILES=0
deba@336
    62
    CHANGED_FILES=0
deba@336
    63
    WARNED_FILES=0
deba@336
    64
}
deba@336
    65
deba@336
    66
function update_done() {
deba@336
    67
    echo $CHANGED_FILES out of $TOTAL_FILES files has been changed.
alpar@337
    68
    echo $WARNED_FILES out of $TOTAL_FILES files triggered warnings.
deba@336
    69
}
deba@336
    70
deba@336
    71
function update_begin() {
deba@336
    72
    ((TOTAL_FILES++))
deba@336
    73
    CHANGED=NO
deba@336
    74
    WARNED=NO
deba@336
    75
}
deba@336
    76
deba@336
    77
function update_end() {
deba@336
    78
    if [ $CHANGED == YES ]
deba@336
    79
    then
deba@336
    80
	((++CHANGED_FILES))
deba@336
    81
    fi
deba@336
    82
    if [ $WARNED == YES ]
deba@336
    83
    then
deba@336
    84
	((++WARNED_FILES))
deba@336
    85
    fi
deba@336
    86
}
deba@336
    87
deba@336
    88
function check_action() {
kpeter@353
    89
    if [ "$3" == 'tabs' ]
kpeter@353
    90
    then
kpeter@353
    91
        PATTERN=$(echo -e '\t')
kpeter@353
    92
    elif [ "$3" == 'trailing spaces' ]
kpeter@353
    93
    then
kpeter@353
    94
        PATTERN='\ +$'
kpeter@353
    95
    else
kpeter@353
    96
        PATTERN='*'
kpeter@353
    97
    fi
kpeter@353
    98
deba@336
    99
    if ! diff -q $1 $2 >/dev/null
deba@336
   100
    then
kpeter@353
   101
        if [ "$PATTERN" == '*' ]
kpeter@353
   102
        then
kpeter@353
   103
            diff $1 $2 | grep '^[0-9]' | sed "s|^\(.*\)c.*$|$2:\1: check failed: $3|g" |
kpeter@353
   104
              sed "s/:\([0-9]*\),\([0-9]*\):\(.*\)$/:\1:\3 (until line \2)/g"
kpeter@353
   105
        else
kpeter@353
   106
            grep -n -E "$PATTERN" $2 | sed "s|^\([0-9]*\):.*$|$2:\1: check failed: $3|g"
kpeter@353
   107
        fi
kpeter@353
   108
        FAILED=YES
deba@336
   109
    fi
deba@336
   110
}
deba@336
   111
deba@336
   112
function check_warning() {
kpeter@341
   113
    if [ "$2" == 'long lines' ]
kpeter@341
   114
    then
kpeter@353
   115
        grep -n -E '.{81,}' $1 | sed "s|^\([0-9]*\):.*$|$1:\1: warning: $2|g"
kpeter@341
   116
    else
kpeter@353
   117
        echo "$1: warning: $2"
kpeter@341
   118
    fi
deba@336
   119
    WARNED=YES
deba@336
   120
}
deba@336
   121
deba@336
   122
function check_init() {
deba@336
   123
    echo Check source files...
deba@336
   124
    FAILED_FILES=0
deba@336
   125
    WARNED_FILES=0
deba@336
   126
    TOTAL_FILES=0
deba@336
   127
}
deba@336
   128
deba@336
   129
function check_done() {
deba@336
   130
    echo $FAILED_FILES out of $TOTAL_FILES files has been failed.
alpar@337
   131
    echo $WARNED_FILES out of $TOTAL_FILES files triggered warnings.
deba@336
   132
kpeter@411
   133
    if [ $WARNED_FILES -gt 0 -o $FAILED_FILES -gt 0 ]
deba@336
   134
    then
deba@336
   135
	if [ "$WARNING" == 'INTERACTIVE' ]
deba@336
   136
	then
kpeter@411
   137
	    echo -n "Are the files with errors/warnings acceptable? (yes/no) "
deba@336
   138
	    while read answer
deba@336
   139
	    do
deba@336
   140
		if [ "$answer" == 'yes' ]
deba@336
   141
		then
deba@336
   142
		    return 0
deba@336
   143
		elif [ "$answer" == 'no' ]
deba@336
   144
		then
deba@336
   145
		    return 1
deba@336
   146
		fi
kpeter@411
   147
		echo -n "Are the files with errors/warnings acceptable? (yes/no) "
deba@336
   148
	    done
deba@336
   149
	elif [ "$WARNING" == 'WERROR' ]
deba@336
   150
	then
deba@336
   151
	    return 1
deba@336
   152
	fi
deba@336
   153
    fi
deba@336
   154
}
deba@336
   155
deba@336
   156
function check_begin() {
deba@336
   157
    ((TOTAL_FILES++))
deba@336
   158
    FAILED=NO
deba@336
   159
    WARNED=NO
deba@336
   160
}
deba@336
   161
deba@336
   162
function check_end() {
deba@336
   163
    if [ $FAILED == YES ]
deba@336
   164
    then
deba@336
   165
	((++FAILED_FILES))
deba@336
   166
    fi
deba@336
   167
    if [ $WARNED == YES ]
deba@336
   168
    then
deba@336
   169
	((++WARNED_FILES))
deba@336
   170
    fi
deba@336
   171
}
deba@336
   172
deba@336
   173
deba@336
   174
deba@336
   175
# checks
deba@336
   176
deba@336
   177
function header_check() {
deba@336
   178
    if echo $1 | grep -q -E 'Makefile\.am$'
deba@336
   179
    then
deba@336
   180
	return
deba@336
   181
    fi
deba@336
   182
alpar@38
   183
    TMP_FILE=`mktemp`
alpar@38
   184
alpar@208
   185
    (echo "/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@38
   186
 *
alpar@208
   187
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@38
   188
 *
alpar@38
   189
 * Copyright (C) "$YEAR"
alpar@38
   190
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@38
   191
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@38
   192
 *
alpar@38
   193
 * Permission to use, modify and distribute this software is granted
alpar@38
   194
 * provided that this copyright notice appears in all copies. For
alpar@38
   195
 * precise terms see the accompanying LICENSE file.
alpar@38
   196
 *
alpar@38
   197
 * This software is provided \"AS IS\" with no warranty of any kind,
alpar@38
   198
 * express or implied, and with no claim as to its suitability for any
alpar@38
   199
 * purpose.
alpar@38
   200
 *
alpar@38
   201
 */
alpar@38
   202
"
deba@336
   203
    awk 'BEGIN { pm=0; }
alpar@38
   204
     pm==3 { print }
alpar@38
   205
     /\/\* / && pm==0 { pm=1;}
alpar@38
   206
     /[^:blank:]/ && (pm==0 || pm==2) { pm=3; print;}
alpar@38
   207
     /\*\// && pm==1 { pm=2;}
alpar@38
   208
    ' $1
deba@336
   209
    ) >$TMP_FILE
alpar@208
   210
deba@336
   211
    "$ACTION"_action "$TMP_FILE" "$1" header
alpar@38
   212
}
alpar@38
   213
deba@336
   214
function tabs_check() {
deba@336
   215
    if echo $1 | grep -q -v -E 'Makefile\.am$'
deba@336
   216
    then
deba@336
   217
        OLD_PATTERN=$(echo -e '\t')
deba@336
   218
        NEW_PATTERN='        '
deba@336
   219
    else
deba@336
   220
        OLD_PATTERN='        '
deba@336
   221
        NEW_PATTERN=$(echo -e '\t')
deba@336
   222
    fi
alpar@208
   223
    TMP_FILE=`mktemp`
deba@336
   224
    cat $1 | sed -e "s/$OLD_PATTERN/$NEW_PATTERN/g" >$TMP_FILE
alpar@38
   225
deba@336
   226
    "$ACTION"_action "$TMP_FILE" "$1" 'tabs'
alpar@208
   227
}
alpar@208
   228
deba@336
   229
function spaces_check() {
alpar@208
   230
    TMP_FILE=`mktemp`
deba@336
   231
    cat $1 | sed -e 's/ \+$//g' >$TMP_FILE
alpar@208
   232
kpeter@340
   233
    "$ACTION"_action "$TMP_FILE" "$1" 'trailing spaces'
alpar@208
   234
}
alpar@208
   235
deba@336
   236
function long_lines_check() {
deba@336
   237
    if cat $1 | grep -q -E '.{81,}'
alpar@208
   238
    then
deba@336
   239
	"$ACTION"_warning $1 'long lines'
alpar@208
   240
    fi
alpar@208
   241
}
alpar@208
   242
deba@336
   243
# process the file
deba@336
   244
deba@336
   245
function process_file() {
kpeter@353
   246
    if [ "$ACTION" == 'update' ]
kpeter@353
   247
    then
kpeter@353
   248
        echo -n "    $ACTION $1..."
kpeter@353
   249
    else
kpeter@353
   250
        echo "	  $ACTION $1..."
kpeter@353
   251
    fi
deba@336
   252
deba@336
   253
    CHECKING="header tabs spaces long_lines"
deba@336
   254
deba@336
   255
    "$ACTION"_begin $1
deba@336
   256
    for check in $CHECKING
alpar@38
   257
    do
deba@336
   258
	"$check"_check $1
alpar@38
   259
    done
deba@336
   260
    "$ACTION"_end $1
kpeter@353
   261
    if [ "$ACTION" == 'update' ]
kpeter@353
   262
    then
kpeter@353
   263
        echo
kpeter@353
   264
    fi
deba@336
   265
}
deba@336
   266
deba@336
   267
function process_all {
deba@336
   268
    "$ACTION"_init
deba@336
   269
    while read file
alpar@38
   270
    do
deba@336
   271
	process_file $file
deba@336
   272
    done < <($FILES)
deba@336
   273
    "$ACTION"_done
deba@336
   274
}
deba@336
   275
deba@336
   276
while [ $# -gt 0 ]
deba@336
   277
do
deba@336
   278
    
deba@336
   279
    if [ "$1" == '--help' ] || [ "$1" == '-h' ]
deba@336
   280
    then
deba@336
   281
	echo -n \
deba@336
   282
"Usage:
deba@336
   283
  $0 [OPTIONS] [files]
deba@336
   284
Options:
deba@336
   285
  --dry-run|-n
alpar@337
   286
     Check the files, but do not modify them.
deba@336
   287
  --interactive|-i
alpar@337
   288
     If --dry-run is specified and the checker emits warnings,
alpar@337
   289
     then the user is asked if the warnings should be considered
alpar@337
   290
     errors.
deba@336
   291
  --werror|-w
alpar@337
   292
     Make all warnings into errors.
deba@336
   293
  --all|-a
kpeter@340
   294
     Check all source files in the repository.
deba@336
   295
  --modified|-m
alpar@337
   296
     Check only the modified (and new) source files. This option is
alpar@337
   297
     useful to check the modification before making a commit.
deba@336
   298
  --changed|-c
deba@336
   299
     Check only the changed source files compared to the parent(s) of
alpar@337
   300
     the current hg node.  This option is useful as hg hook script.
alpar@337
   301
     To automatically check all your changes before making a commit,
alpar@337
   302
     add the following section to the appropriate .hg/hgrc file.
deba@336
   303
deba@336
   304
       [hooks]
deba@336
   305
       pretxncommit.checksources = scripts/unify-sources.sh -c -n -i
deba@336
   306
deba@336
   307
  --help|-h
deba@336
   308
     Print this help message.
deba@336
   309
  files
kpeter@340
   310
     The files to check/unify. If no file names are given, the modified
kpeter@340
   311
     source files will be checked/unified (just like using the
kpeter@340
   312
     --modified|-m option).
deba@336
   313
"
deba@336
   314
        exit 0
deba@336
   315
    elif [ "$1" == '--dry-run' ] || [ "$1" == '-n' ]
deba@336
   316
    then
kpeter@340
   317
	[ -n "$ACTION" ] && echo "Conflicting action options" >&2 && exit 1
deba@336
   318
	ACTION=check
deba@336
   319
    elif [ "$1" == "--all" ] || [ "$1" == '-a' ]
deba@336
   320
    then
kpeter@340
   321
	[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
deba@336
   322
	FILES=all_files
deba@336
   323
    elif [ "$1" == "--changed" ] || [ "$1" == '-c' ]
deba@336
   324
    then
kpeter@340
   325
	[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
deba@336
   326
	FILES=changed_files
deba@336
   327
    elif [ "$1" == "--modified" ] || [ "$1" == '-m' ]
deba@336
   328
    then
kpeter@340
   329
	[ -n "$FILES" ] && echo "Conflicting target options" >&2 && exit 1
deba@336
   330
	FILES=modified_files
deba@336
   331
    elif [ "$1" == "--interactive" ] || [ "$1" == "-i" ]
deba@336
   332
    then
kpeter@340
   333
	[ -n "$WARNING" ] && echo "Conflicting warning options" >&2 && exit 1
deba@336
   334
	WARNING='INTERACTIVE'
deba@336
   335
    elif [ "$1" == "--werror" ] || [ "$1" == "-w" ]
deba@336
   336
    then
kpeter@340
   337
	[ -n "$WARNING" ] && echo "Conflicting warning options" >&2 && exit 1
deba@336
   338
	WARNING='WERROR'
kpeter@340
   339
    elif [ $(echo x$1 | cut -c 2) == '-' ]
deba@336
   340
    then
deba@336
   341
	echo "Invalid option $1" >&2 && exit 1
deba@336
   342
    else
deba@336
   343
	[ -n "$FILES" ] && echo "Invalid option $1" >&2 && exit 1
deba@336
   344
	GIVEN_FILES=$@
deba@336
   345
	FILES=given_files
deba@336
   346
	break
deba@336
   347
    fi
deba@336
   348
    
deba@336
   349
    shift
deba@336
   350
done
deba@336
   351
deba@336
   352
if [ -z $FILES ]
deba@336
   353
then
deba@336
   354
    FILES=modified_files
alpar@38
   355
fi
deba@336
   356
deba@336
   357
if [ -z $ACTION ]
deba@336
   358
then
deba@336
   359
    ACTION=update
alpar@208
   360
fi
deba@336
   361
deba@336
   362
process_all