summaryrefslogtreecommitdiff
path: root/bin/nightlybuilds/clean_logs.pl
blob: c31ee0c88f0e70779c38b577bab0a1218dff1ef9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# $Id$

# The purpose of this script is to clean out a log directory that
# is used usually with the scoreboard and show_log_dir stuff.

use DirHandle;

$keptlogs = 20;
$debug = 0;

while ($#ARGV >= 0)
{
    if ($ARGV[0] =~ m/^-debug/i) {
        $debug = 1;
    }
    elsif ($ARGV[0] =~ m/^-keptlogs/i) {
        $keptlogs = $ARGV[1];
        shift;
    }    
    elsif ($ARGV[0] =~ m/^-(\?|h)/i) {     # Help information
        print "clean_logs.pl [-debug] [-keptlogs <num>] (dirs...)\n";
        print "\n";
        print "Cleans out directories of YYYY_MM_DD_HH_MM logs\n";
        print "\n";
        print "Options\n";
        print "-debug           - Do not delete, just print\n";
        print "-keptlogs <num>  - Delete all but <num> sets of logs (default:20)\n";
        exit;
    }
    elsif ($ARGV[0] =~ m/^-/) {
        warn "$0:  unknown option $ARGV[0]\n";
        exit 1;
    }
    else {
        my $dir = $ARGV[0];
        $dir =~ s/\\/\//g;
        push @dirs, $dir;
    }
    shift;
}

if (!defined @dirs) {
    push @dirs , ".";
}

foreach $dir (@dirs) {
    my @existing;
    $d = new DirHandle ($dir);

    if (defined $d) {
        while (defined($_ = $d->read)) { 
            if ($_ =~ m/^(....)_(..)_(..)_(..)_(..)/) {
                push @existing, $dir . '/' . $_;
            }
        }
        undef $d;
    }

    @existing = reverse sort @existing;

    for ($i = 0; $i < $keptlogs; ++$i) {
        shift @existing;  
        shift @existing;  
        shift @existing;  
    }

    foreach my $file (@existing) {
        print "        Removing $file\n";
        if ($debug == 0) {
            unlink $file;
        }
    }
}