COIN-OR::LEMON - Graph Library

source: lemon-main/scripts/unify-sources.sh @ 621:b536eaacb39b

Last change on this file since 621:b536eaacb39b was 554:6b9c1f6eb1e5, checked in by Peter Kovacs <kpeter@…>, 16 years ago

Bug fix in unify-sources.sh (#245)

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