COIN-OR::LEMON - Graph Library

Ticket #138: scripts_158ef3b71f5c.patch

File scripts_158ef3b71f5c.patch, 5.2 KB (added by Peter Kovacs, 13 years ago)
  • new file scripts/check-sources.sh

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1221724928 -7200
    # Node ID 158ef3b71f5c43f3110581a3e21a376642b642bb
    # Parent  983d8c23aff82750de15e5d76dd699b8e5b19593
    Several improvements in code checking and unification (ticket #138)
    
    The new check-sources.sh script checks the same things as unify-sources.sh,
    but it does not modify the files, just checks the conformity. It can be used
    as commit checker hg hook (for instructions see the comments in the file).
    
    By default check-sources.sh checks only the modified files, but using the
    "--all" (or "-a") option it checks all source files (as unify-sources.sh).
    
    The way how we enumerate the code files is changed in unify-sources.sh
    because the manifest command enumerates the already removed files, which
    raise error in the script.
    
    The Makefile.am files are included into the checking and unification.
    
    diff --git a/scripts/check-sources.sh b/scripts/check-sources.sh
    new file mode 100755
    - +  
     1#!/bin/bash
     2
     3# Add to your .hg/hgrc the following section.
     4# [hooks]
     5# pretxncommit.checksources = scripts/check-sources.sh
     6
     7YEAR=`date +2003-%Y`
     8HGROOT=`hg root`
     9
     10TAB=`echo -e '\t'`
     11
     12function check_header() {
     13    HEADER=\
     14"/* -*- mode: C++; indent-tabs-mode: nil; -*-
     15 *
     16 * This file is a part of LEMON, a generic C++ optimization library.
     17 *
     18 * Copyright (C) "$YEAR"
     19 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     20 * (Egervary Research Group on Combinatorial Optimization, EGRES).
     21 *
     22 * Permission to use, modify and distribute this software is granted
     23 * provided that this copyright notice appears in all copies. For
     24 * precise terms see the accompanying LICENSE file.
     25 *
     26 * This software is provided \"AS IS\" with no warranty of any kind,
     27 * express or implied, and with no claim as to its suitability for any
     28 * purpose.
     29 *
     30 */"
     31
     32    HEADER_LENGTH=$(echo "$HEADER" | wc -l)
     33    FILE_HEADER=$(cat $1 | head -n $HEADER_LENGTH)
     34
     35    [[ "$FILE_HEADER" != "$HEADER" ]]
     36}
     37
     38function check_file() {
     39    echo '    check' $1 ...
     40
     41    FILE_FAILED=NO
     42
     43    if echo $1 | grep -q -v -E 'Makefile\.am$'
     44    then
     45        if check_header $1
     46        then
     47            echo "header failed"
     48            FILE_FAILED=YES
     49            GLOBAL_FAILED=YES
     50        fi
     51    fi
     52
     53    if grep -q -E "$TAB" $1
     54    then
     55        echo "tabs failed:"
     56        grep -n -E "$TAB" $1
     57        FILE_FAILED=YES
     58        GLOBAL_FAILED=YES
     59    fi
     60
     61    if grep -q -E ' $' $1
     62    then
     63        echo "trailing spaces failed:"
     64        grep -n -E ' $' $1
     65        FILE_FAILED=YES
     66        GLOBAL_FAILED=YES
     67    fi
     68
     69    if grep -q -E '.{81,}' $1
     70    then
     71        echo "long lines failed:"
     72        grep -n -E '.{81,}' $1
     73        if [ $GLOBAL_FAILED != YES ]
     74        then
     75            echo -n "Abort commit? (yes/no) "
     76            read RESULT
     77            if [ "$RESULT" != "no" ]
     78            then
     79                FILE_FAILED=YES
     80                GLOBAL_FAILED=YES
     81            fi
     82        fi
     83    fi
     84
     85    if [[ $FILE_FAILED = YES ]];
     86    then
     87        ((FAILED_FILES++))
     88    fi
     89}
     90
     91FAILED_FILES=0
     92TOTAL_FILES=0
     93GLOBAL_FAILED=NO
     94
     95function list_files() {
     96    hg status -a -m -c |
     97    cut -d ' ' -f 2 | grep -E  '(\.(cc|h|dox)$|Makefile\.am$)' | sort | uniq
     98}
     99
     100function changed_files() {
     101    {
     102        if [ -n "$HG_PARENT1" ]
     103        then
     104            hg status --rev $HG_PARENT1:$HG_NODE -a -m
     105        fi
     106        if [ -n "$HG_PARENT2" ]
     107        then
     108            hg status --rev $HG_PARENT2:$HG_NODE -a -m
     109        fi
     110    } | cut -d ' ' -f 2 | grep -E '(\.(cc|h|dox)$|Makefile\.am$)' | sort | uniq
     111}
     112
     113if [ $# == 0 ]; then
     114    echo Check all modified files...
     115    while read FILENAME
     116    do
     117        check_file "$HGROOT/$FILENAME"
     118        ((TOTAL_FILES++))
     119    done < <(changed_files)
     120    echo '  done.'
     121elif [[ ($# == 1) && (($1 == "--all") || ($1 == "-a")) ]]; then
     122    echo Check all source files...
     123    while read FILENAME
     124    do
     125        check_file "$HGROOT/$FILENAME"
     126        ((TOTAL_FILES++))
     127    done < <(list_files)
     128    echo '  done.'
     129else
     130    for i in $@
     131    do
     132        check_file $i
     133        ((TOTAL_FILES++))
     134    done
     135fi
     136
     137echo $FAILED_FILES out of $TOTAL_FILES files has failed.
     138
     139[[ $GLOBAL_FAILED == NO ]]
  • scripts/unify-sources.sh

    diff --git a/scripts/unify-sources.sh b/scripts/unify-sources.sh
    a b  
    7272function update_file() {
    7373    echo -n '    update' $i ...
    7474
    75     update_header $1
     75    if echo $1 | grep -q -v -E 'Makefile\.am$'
     76    then
     77        update_header $1
     78    fi
    7679    update_tabs $1
    7780    remove_trailing_space $1
    7881
     
    104107    fi
    105108}
    106109
     110function list_files() {
     111    hg status -a -m -c |
     112    cut -d ' ' -f 2 | grep -E  '(\.(cc|h|dox)$|Makefile\.am$)' | sort | uniq
     113}
     114
    107115CHANGED_FILES=0
    108116TOTAL_FILES=0
    109117LONG_LINE_FILES=0
    110118if [ $# == 0 ]; then
    111119    echo Update all source files...
    112     for i in `hg manifest|grep -E  '\.(cc|h|dox)$'`
     120    while read i
    113121    do
    114122        update_file $HGROOT/$i
    115123        ((TOTAL_FILES++))
    116     done
     124    done < <(list_files)
    117125    echo '  done.'
    118126else
    119     for i in $*
     127    for i in $@
    120128    do
    121129        update_file $i
    122130        ((TOTAL_FILES++))
     
    125133echo $CHANGED_FILES out of $TOTAL_FILES files has been changed.
    126134if [[ $LONG_LINE_FILES -gt 1 ]]; then
    127135    echo
    128     echo WARNING: $LONG_LINE_FILES files contains long lines!   
     136    echo WARNING: $LONG_LINE_FILES files contains long lines!
    129137    echo
    130138elif [[ $LONG_LINE_FILES -gt 0 ]]; then
    131139    echo