~ubuntu-branches/ubuntu/precise/dbacl/precise

« back to all changes in this revision

Viewing changes to src/mailtoe.in

  • Committer: Bazaar Package Importer
  • Author(s): Clint Adams
  • Date: 2005-05-07 12:59:53 UTC
  • Revision ID: james.westby@ubuntu.com-20050507125953-xzy2bwkb2qamglwm
Tags: upstream-1.9
ImportĀ upstreamĀ versionĀ 1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
 
3
# Copyright (C) 2002 Laird Breyer
 
4
#  
 
5
# This program is free software; you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License as published by
 
7
# the Free Software Foundation; either version 2 of the License, or
 
8
# (at your option) any later version.
 
9
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU General Public License for more details.
 
14
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program; if not, write to the Free Software
 
17
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
 
19
# Author:   Laird Breyer <laird@lbreyer.com>
 
20
#
 
21
 
 
22
PROGNAME="$0"
 
23
PROGNAME2=`basename $0`
 
24
VERSION="$PROGNAME version @VERSION@\nTrain On Error simulator"
 
25
MXDIR="$PWD/mailtoe.d"
 
26
ALOG="$MXDIR/log/activity.log"
 
27
CLOG="$MXDIR/log/toe.log"
 
28
SLOG="$MXDIR/log/summary.log"
 
29
TEMPDIR="$MXDIR/tmp"
 
30
BOOTSTRAP="@PKGDATADIR@/testsuite"
 
31
FILTERS="$MXDIR/filters"
 
32
 
 
33
# use this for debugging
 
34
# set -x
 
35
 
 
36
usage() {
 
37
    echo "
 
38
Usage: 
 
39
 
 
40
    $PROGNAME2 prepare size
 
41
    $PROGNAME2 add category [MBOX]...
 
42
    $PROGNAME2 run
 
43
    $PROGNAME2 summarize [LEVEL]
 
44
    $PROGNAME2 plot [ps|logscale]
 
45
    $PROGNAME2 review TRUECAT PREDCAT
 
46
    $PROGNAME2 clean
 
47
    $PROGNAME2 killall
 
48
 
 
49
    $PROGNAME2 testsuite select [FILTER]...
 
50
    $PROGNAME2 testsuite deselect [FILTER]...
 
51
    $PROGNAME2 testsuite list
 
52
    $PROGNAME2 testsuite status
 
53
    $PROGNAME2 testsuite run [plots]
 
54
    $PROGNAME2 testsuite summarize
 
55
"
 
56
    exit 1
 
57
}
 
58
 
 
59
mbox_multiplex() {
 
60
    perl -e '
 
61
# usage: $0 seqno seed [MBOX]... -s COMMAND ARGS 
 
62
use strict; 
 
63
$SIG{PIPE} = "IGNORE"; 
 
64
my %mbox; 
 
65
my %line; 
 
66
my $cmd = ""; 
 
67
my $args = ""; 
 
68
my $n = shift;
 
69
srand shift; 
 
70
 
 
71
foreach my $g (@ARGV) { 
 
72
  if( "$g" eq "-s" ) { 
 
73
    $cmd = "|"; 
 
74
  } elsif( $cmd ne "" ) { 
 
75
    $cmd .= " $g"; 
 
76
  } else { 
 
77
    if( open($mbox{$g}, "<$g") ) {
 
78
      $args .= " $g"; 
 
79
    } else {
 
80
      delete $mbox{$g}; 
 
81
    }
 
82
  } 
 
83
 
84
 
 
85
$args =~ s/\.mbox//g;
 
86
$args =~ s|/mbox/|/$n/|g;
 
87
$| = 1; 
 
88
 
 
89
while( scalar keys %mbox > 0 ) { 
 
90
  my $j = int(rand scalar keys %mbox); 
 
91
  foreach my $f (keys %mbox) { 
 
92
    if( $j-- <= 0 ) { 
 
93
      if( eof($mbox{$f}) ) { 
 
94
        close($mbox{$f}); 
 
95
        delete $mbox{$f}; 
 
96
      } else { 
 
97
        my $c = $f;
 
98
        $c =~ s|\.mbox$||;
 
99
        $c =~ s|/mbox/|/$n/|;
 
100
 
 
101
        open(CPIPE, "$cmd $c $args"); 
 
102
        while( ($line{$f} !~ /^From /) && !eof($mbox{$f}) ) { 
 
103
          $line{$f} = readline $mbox{$f}; 
 
104
        } 
 
105
 
 
106
        my $fromline = $line{$f};
 
107
        $fromline =~ s/^From//;
 
108
        $c =~ s|^.*/||;
 
109
 
 
110
        print "$n $c ";
 
111
        print CPIPE $line{$f}; 
 
112
        $line{$f} = readline $mbox{$f}; 
 
113
        while( !eof($mbox{$f}) && ($line{$f} !~ /^From /) ) { 
 
114
          print CPIPE $line{$f}; 
 
115
          $line{$f} = readline $mbox{$f}; 
 
116
        } 
 
117
        close(CPIPE); 
 
118
        # expect the piped command to output result without trailing newline
 
119
        print $fromline;
 
120
      } 
 
121
      last; 
 
122
    } 
 
123
  } 
 
124
 
125
' "$@"
 
126
}
 
127
 
 
128
# this is the default filter
 
129
if [ -z "$MAILTOE_FILTER" ]; then
 
130
    MAILTOE_FILTER="@PKGDATADIR@/dbaclB toe"
 
131
fi
 
132
 
 
133
 
 
134
# begin mailtest.functions
 
135
# end mailtest.functions
 
136
 
 
137
# check this for environment variable overrrides
 
138
[ -e $HOME/.mailtoerc ] && . $HOME/.mailtoerc
 
139
 
 
140
export TEMPDIR
 
141
# main switch statement - this processes commands
 
142
case $1 in
 
143
 
 
144
    '-V')
 
145
        echo $VERSION
 
146
        ;;
 
147
 
 
148
    clean) # delete working tree
 
149
        clean_working_tree
 
150
        ;;
 
151
 
 
152
    killall)
 
153
        prerequisite_command "killall" "killall"
 
154
        killall -9 -g mailtoe
 
155
        ;;
 
156
 
 
157
    prepare) # create directory tree
 
158
        shift
 
159
        prepare_working_tree "$@"
 
160
        NUM=`expr $1 - 1`
 
161
        # use parent process id to randomize
 
162
        RANDOM=$PPID
 
163
        for i in `seq 0 $NUM`; do 
 
164
            echo $RANDOM > "$MXDIR/$i/seed"
 
165
        done
 
166
        ;;
 
167
 
 
168
    add)
 
169
        shift
 
170
        CATNAME="$1"
 
171
        if [ -z "$CATNAME" ]; then
 
172
            echo "error: missing category name."
 
173
            usage
 
174
        fi
 
175
 
 
176
        prerequisite_command "formail" "mailutils"
 
177
        get_number_of_subsets
 
178
 
 
179
        echo "=== $PROGNAME $*" >> $ALOG
 
180
 
 
181
        shift
 
182
        # use formail to ensure mbox format is clean
 
183
        if [ -n "$*" ]; then
 
184
            cat "$@" | formail -s /bin/bash -c \
 
185
                "cat >> $MXDIR/mbox/$CATNAME.mbox"
 
186
        else
 
187
            formail -s /bin/bash -c \
 
188
                "cat >> $MXDIR/mbox/$CATNAME.mbox"
 
189
        fi
 
190
        ;;
 
191
 
 
192
    learn) 
 
193
        echo "This command is not meaningful."
 
194
        ;;
 
195
 
 
196
    run) 
 
197
        shift
 
198
        get_number_of_subsets
 
199
        NUM=`expr $NUM - 1` # we count from zero to NUM-1
 
200
        get_categories
 
201
        STUFF="$MXDIR/log/run.stuff"
 
202
 
 
203
        prerequisite_command "perl" "perl"
 
204
        prerequisite_command "sed" "sed"
 
205
        prerequisite_command "seq" "shellutils"
 
206
 
 
207
        echo "=== $PROGNAME run $*" >> $ALOG
 
208
 
 
209
        echo "# location | true | predicted | from" > $CLOG
 
210
 
 
211
        for i in `seq 0 $NUM`; do
 
212
 
 
213
            COMMAND="$MAILTOE_FILTER "
 
214
            SEED=`cat $MXDIR/$i/seed`
 
215
            CATPATHS=`for n in $CATS; do echo -ne "$MXDIR/mbox/$n "; done`
 
216
            echo "| $COMMAND" >> $ALOG
 
217
 
 
218
            mbox_multiplex  $i $SEED $CATPATHS -s $COMMAND >> $CLOG
 
219
 
 
220
            echo "    toe $COMMAND |" >> $ALOG
 
221
        done
 
222
        ;;
 
223
 
 
224
    summarize) 
 
225
        shift
 
226
        get_number_of_subsets # includes check that directory tree is present
 
227
        get_categories
 
228
 
 
229
        prerequisite_command "awk" "awk or equivalent"
 
230
 
 
231
        echo "=== $PROGNAME summarize $*" >> $ALOG
 
232
 
 
233
        if [ -s $CLOG ]; then
 
234
            cat $CLOG | summarize_log
 
235
        else
 
236
            echo "Error: No results found. You must run the TOE simulations first."
 
237
            usage
 
238
        fi
 
239
        ;;
 
240
 
 
241
    review) 
 
242
        shift
 
243
        prerequisite_command "formail" "mailutils"
 
244
        prerequisite_command "grep" "grep"
 
245
 
 
246
        if [ -z "$1" -o -z "$2" ]; then
 
247
            echo "Error: Missing category, e.g. $PROGNAME review notspam spam"
 
248
        else
 
249
            review_misclassified "$MXDIR/tmp/save_msg.sh" "$1" "$2"
 
250
        fi
 
251
        ;;
 
252
 
 
253
    plot)
 
254
        shift
 
255
        prerequisite_command "gnuplot" "gnuplot"
 
256
        
 
257
        if [ -e "$MXDIR/log/toe.log" ]; then
 
258
            plot_errors "$MXDIR/log/toe.log" "Misclassifications over time in TOE simulation\n$MAILTOE_FILTER" "$@"
 
259
        fi
 
260
        ;;
 
261
 
 
262
    testsuite)
 
263
        shift
 
264
        case $1 in
 
265
            list)
 
266
                testsuite_list_wrappers
 
267
                ;;
 
268
 
 
269
            deselect)
 
270
                testsuite_deselect_wrappers "$@"
 
271
                ;;
 
272
 
 
273
            select)
 
274
                testsuite_select_wrappers "$@"
 
275
                ;;
 
276
 
 
277
            status)
 
278
                echo -e "The following categories are ready to be TOE tested:\n"
 
279
                get_categories
 
280
                for c in $CATS; do
 
281
                    echo -n "$c - counting... "
 
282
                    NUM=`grep '^From ' $MXDIR/*/$c | wc -l`
 
283
                    echo "$NUM messages"
 
284
                done
 
285
 
 
286
                echo -e "\nThe following classifiers are ready to be TOE tested:\n"
 
287
                get_filters
 
288
                for f in $FILTS; do
 
289
                    echo "$f - `$FILTERS/$f describe`"
 
290
                done
 
291
                ;;
 
292
 
 
293
            run)
 
294
                get_filters
 
295
                get_categories
 
296
                get_number_of_subsets
 
297
                NUM=`expr $NUM - 1`
 
298
                make_dummy_mbox
 
299
                for f in $FILTS; do
 
300
 
 
301
                    echo -ne "Now testing: "
 
302
                    "$FILTERS/$f" describe
 
303
 
 
304
                    echo "Cleanup."
 
305
                    "$FILTERS/$f" clean "$MXDIR"
 
306
                    
 
307
                    # before we can classify, we need to create all the 
 
308
                    # category databases - we use a dummy mailbox for this
 
309
                    for i in `seq 0 $NUM`; do
 
310
                        for j in $CATS; do
 
311
                            cat $MXDIR/mbox/dummy.mailbox | "$FILTERS/$f" learn "$MXDIR/$i/${j/.mbox/}"
 
312
                        done
 
313
                    done
 
314
 
 
315
                    export MAILTOE_FILTER="$FILTERS/$f toe"
 
316
 
 
317
                    echo "Running."
 
318
                    time "$PROGNAME" run
 
319
 
 
320
                    echo "Writing results."
 
321
                    echo -e "\n---------------" >> "$SLOG"
 
322
                    "$FILTERS/$f" describe >> "$SLOG"
 
323
                    date >> "$CLOG"
 
324
                    echo "---------------" >> "$SLOG"
 
325
                    "$PROGNAME" summarize >> "$SLOG"
 
326
 
 
327
                    if [ "$2" = "plots" ]; then
 
328
                        prerequisite_command "gnuplot" "gnuplot"
 
329
                        plot_errors "$MXDIR/plots/$f.toe.ps" "Misclassifications over time in TOE simulation\n$f" ps
 
330
                    fi
 
331
 
 
332
                done
 
333
                ;;
 
334
 
 
335
            summarize) 
 
336
                if [ -s "$SLOG" ]; then
 
337
                    cat "$SLOG"
 
338
                else
 
339
                    echo "Error: No results found. You must run the testsuite first."
 
340
                fi
 
341
                ;;
 
342
 
 
343
            *)
 
344
                usage
 
345
                ;;
 
346
        esac
 
347
        ;;
 
348
 
 
349
    *) 
 
350
        usage
 
351
        ;;
 
352
esac
 
353
 
 
354
exit 0
 
 
b'\\ No newline at end of file'