COIN-OR::LEMON - Graph Library

source: lemon-main/scripts/unify-sources.sh @ 1211:a278d16bd2d0

Last change on this file since 1211:a278d16bd2d0 was 733:abf31e4af617, checked in by Alpar Juttner <alpar@…>, 15 years ago

Copyright notices added to scripts

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