import_commitlog.in 5.28 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2004-2013 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26 27 28
#
use English;
use Getopt::Std;
use POSIX;

#
29 30 31
# Yuck. Take the cvs log for the testbed, and pull out the last N
# months of entries, reversing them in the process. Then write the
# file into the www directory!
32 33 34 35 36 37 38 39 40 41
#
# This script should be run from crontab on the main emulab site only.
#
# 12  2  *  *  *     root    /usr/testbed/sbin/import_commitlog
#
sub usage()
{
    print STDERR "Usage: import_commitlog\n";
    exit(-1);
}
42 43 44

sub fatal($);

45 46 47 48 49 50
my $optlist  = "d";
my $debug    = 0;

#
# Only root can run this script.
#
Leigh Stoller's avatar
Leigh Stoller committed
51
if ($UID) {
52 53 54 55 56 57 58 59 60
    die("*** $0:\n".
	"    Only root can run this script\n");
}

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
61
my $GIT         = "/usr/local/bin/git";
62
my $GITDIR      = "/z/GIT/emulab-devel";
63
my $OUTPUTLOG   = "$TB/www/doc/ChangeLog.txt";
64 65
my $MONTHROOT   = "$TB/www/doc/ChangeLog";
my $HTMLFILE    = "$TB/www/doc/changelog-months.html";
66
my $STARTDATE   = "2003-10";
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
my $tempname    = "/tmp/import.$$";
my @entries     = ();

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

#
# Turn off line buffering on output
#
$| = 1;

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:";

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{d})) {
    $debug = 1;
}
98 99
chdir($GITDIR) or
    fatal("Could not chdir to $GITDIR");
100

101 102 103 104 105 106 107
#
# Pull down latest.
#
system("$GIT pull -q") == 0
    or fatal("Could not pull down latest repo");

if (!open(INPUT, "$GIT log ".
108 109
	  "--pretty=medium --numstat --date=iso --reverse |")) {
    fatal("Could not start $GIT log process!");
110 111 112 113 114 115 116 117 118
}
if (!open(OUTPUT, "> $tempname")) {
    fatal("Could not open $tempname for writing!");
}

#
# Skip to approximate start date.
# 
while (<INPUT>) {
119 120
    if ($_ =~
	/^Date:\s*(\d\d\d\d\-\d\d)\-\d\d\s*\d\d:\d\d:\d\d\s*[+-]?\w\w\w\w$/) {
121 122 123 124 125
        if ($1 eq $STARTDATE) {
	    last;
	}
    }
}
126 127 128 129 130
while (<INPUT>) {
    if ($_ =~ /^commit\s*[\w]*$/) {
	last;
    }
}
131 132 133
my $entry = $_;

#
134
# Reverse the rest of the entries, saving them up, month by month
135
#
136
my $index       = 0;
137 138
my @month_index = ();
my %month_data  = ();
139
my $month;
140

141
while (<INPUT>) {
142 143
    if ($_ =~
	/^Date:\s*(\d\d\d\d\-\d\d)\-\d\d\s*\d\d:\d\d:\d\d\s*[+-]?\w\w\w\w$/) {
144
	$this_month = $1;
145 146
    }
    elsif ($_ =~ /^commit\s*[\w]*$/) {
147
	if (! exists($month_data{$this_month})) {
148
	    $month_data{$this_month} = [];
149
	    $month_index[$index++]   = $this_month;
150
	}
151 152
	unshift(@{ $month_data{$this_month} }, $entry);
	$entry = $_;
153 154 155 156
	next;
    }
    $entry .= $_;
}
157
close(INPUT);
158 159

#
160
# Write out the list in one file (reverse order of course).
161
# 
162
for (my $i = $index - 1; $i >= 0; $i--) {
163 164 165 166 167
    my @entries = @{ $month_data{$month_index[$i]} };

    foreach my $entry (@entries) {
	print OUTPUT $entry;
    }
168 169 170 171 172 173 174 175 176
}
close(OUTPUT);

#
# Move the new file into the www directory.
#
if (system("mv -f $tempname $OUTPUTLOG")) {
    fatal("Could not mv $tempname to $OUTPUTLOG");
}
177 178 179 180 181 182
unlink($tempname)
    if (-e $tempname);

#
# Now write a bunch of per-month files.
#
183
for (my $i = 0; $i < $index; $i++) {
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    my @entries = @{ $month_data{$month_index[$i]} };
    my $month   = $month_index[$i];
    $month =~ s/\//-/g;
    my $target  = $MONTHROOT . "-" . $month . ".txt";

    if (!open(OUTPUT, "> $tempname")) {
	fatal("Could not open $tempname for writing!");
    }

    foreach my $entry (@entries) {
	print OUTPUT $entry;
    }
    close(OUTPUT);

    if (system("mv -f $tempname $target")) {
	fatal("Could not mv $tempname to $target");
    }
    
}
unlink($tempname)
    if (-e $tempname);

#
# Now write a little html file to include.
#
if (!open(OUTPUT, "> $tempname")) {
    fatal("Could not open $tempname for writing!");
}

213
for (my $i = $index - 1; $i >= 0; $i--) {
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    my $month   = $month_index[$i];
    $month =~ s/\//-/g;
    my $target  = "ChangeLog-" . $month . ".txt";

    print OUTPUT "<tr><td class=stealth align=center>";
    print OUTPUT "<a href='docwrapper.php3?docname=$target'>";
    print OUTPUT $month_index[$i];
    print OUTPUT "</a></td></tr>\n";
}
close(OUTPUT);

if (system("mv -f $tempname $HTMLFILE")) {
    fatal("Could not mv $tempname to $HTMLFILE");
}
unlink($tempname)
    if (-e $tempname);
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

exit(0);

sub fatal($)
{
    my($mesg) = $_[0];

    print "$mesg\n";

    #
    # Send a message to the testbed list. 
    #
    SENDMAIL($TBOPS,
	     "Failure importing testbed commitlog",
	     $mesg);
    
    if (defined($tempname)) {
	unlink("$tempname");
    }
    exit(-1);
}