chiark / gitweb /
devscripts (2.10.69+squeeze4) stable-security; urgency=high
[devscripts.git] / scripts / plotchangelog.pl
1 #! /usr/bin/perl
2 #
3 # Plot the history of a debian package from the changelog, displaying
4 # when each release of the package occurred, and who made each release.
5 # To make the graph a little more interesting, the debian revision of the
6 # package is used as the y axis.
7 #
8 # Pass this program the changelog(s) you wish to be plotted.
9 #
10 # Copyright 1999 by Joey Hess <joey@kitenet.net>
11 # Modifications copyright 2003 by Julian Gilbey <jdg@debian.org>
12
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25
26 use 5.006;
27 use strict;
28 use FileHandle;
29 use File::Basename;
30 use File::Temp qw/ tempfile /;
31 use Fcntl;
32 use Getopt::Long;
33
34 BEGIN {
35     eval { require Date::Parse; import Date::Parse (); };
36     if ($@) {
37         my $progname = basename($0);
38         if ($@ =~ /^Can\'t locate Date\/Parse\.pm/) {
39             die "$progname: you must have the libtimedate-perl package installed\nto use this script\n";
40         } else {
41             die "$progname: problem loading the Date::Parse module:\n  $@\nHave you installed the libtimedate-perl package?\n";
42         }
43     }
44 }
45
46
47 my $progname = basename($0);
48 my $modified_conf_msg;
49
50 sub usage {
51     print <<"EOF";
52 Usage: plotchangelog [options] changelog ...
53         -v        --no-version    Do not show package version information.
54         -m        --no-maint      Do not show package maintainer information.
55         -u        --urgency       Use larger points for higher urgency uploads.
56         -l        --linecount     Make the Y axis be number of lines in the
57                                   changelog.
58         -b        --bugcount      Make the Y axis be number of bugs closed
59                                   in the changelog.
60         -c        --cumulative    With -l or -b, graph the cumulative number
61                                   of lines or bugs closed.
62         -g "commands"             Pass "commands" on to gnuplot, they will be
63         --gnuplot="commands"      added to the gnuplot script that is used to 
64                                   generate the graph.
65         -s file   --save=file     Save the graph to the specified file in
66                                   postscript format.
67         -d        --dump          Dump gnuplot script to stdout.
68                   --verbose       Outputs the gnuplot script.
69                   --help          Show this message.
70                   --version       Display version and copyright information.
71                   --noconf        Don\'t read devscripts configuration files
72
73   At most one of -l and -b (or their long equivalents) may be used.
74
75 Default settings modified by devscripts configuration files:
76 $modified_conf_msg
77 EOF
78 }
79
80 my $versioninfo = <<"EOF";
81 This is $progname, from the Debian devscripts package, version ###VERSION###
82 This code is copyright 1999 by Joey Hess <joey\@kitenet.net>.
83 Modifications copyright 1999-2003 by Julian Gilbey <jdg\@debian.org>
84 This program comes with ABSOLUTELY NO WARRANTY.
85 You are free to redistribute this code under the terms of the
86 GNU General Public License, version 2 or later.
87 EOF
88
89 my ($no_version, $no_maintainer, $gnuplot_commands, $dump,
90     $save_filename, $verbose, $linecount, $bugcount, $cumulative,
91     $help, $showversion, $show_urgency, $noconf)="";
92
93 # Handle config file unless --no-conf or --noconf is specified
94 # The next stuff is boilerplate
95 my $extra_gnuplot_commands='';
96 if (@ARGV and $ARGV[0] =~ /^--no-?conf$/) {
97     $modified_conf_msg = "  (no configuration files read)";
98     shift;
99 } else {
100     my @config_files = ('/etc/devscripts.conf', '~/.devscripts');
101     my %config_vars = (
102                        'PLOTCHANGELOG_OPTIONS' => '',
103                        'PLOTCHANGELOG_GNUPLOT' => '',
104                        );
105     my %config_default = %config_vars;
106
107     my $shell_cmd;
108     # Set defaults
109     foreach my $var (keys %config_vars) {
110         $shell_cmd .= "$var='$config_vars{$var}';\n";
111     }
112     $shell_cmd .= 'for file in ' . join(" ",@config_files) . "; do\n";
113     $shell_cmd .= '[ -f $file ] && . $file; done;' . "\n";
114     # Read back values
115     foreach my $var (keys %config_vars) { $shell_cmd .= "echo \$$var;\n" }
116     my $shell_out = `/bin/bash -c '$shell_cmd'`;
117     @config_vars{keys %config_vars} = split /\n/, $shell_out, -1;
118     
119     foreach my $var (sort keys %config_vars) {
120         if ($config_vars{$var} ne $config_default{$var}) {
121             $modified_conf_msg .= "  $var=$config_vars{$var}\n";
122         }
123     }
124     $modified_conf_msg ||= "  (none)\n";
125     chomp $modified_conf_msg;
126
127     if ($config_vars{'PLOTCHANGELOG_OPTIONS'}) {
128         unshift @ARGV, split(' ', $config_vars{'PLOTCHANGELOG_OPTIONS'});
129     }
130     $extra_gnuplot_commands=$config_vars{'PLOTCHANGELOG_GNUPLOT'};
131 }
132
133 GetOptions(
134            "no-version|v", \$no_version,
135            "no-maint|m", \$no_maintainer,
136            "gnuplot|g=s", \$gnuplot_commands,
137            "save|s=s", \$save_filename,
138            "dump|d", \$dump,
139            "urgency|u", \$show_urgency,
140            "verbose", \$verbose,
141            "l|linecount", \$linecount,
142            "b|bugcount", \$bugcount,
143            "c|cumulative", \$cumulative,
144            "help", \$help,
145            "version", \$showversion,
146            "noconf" => \$noconf,
147            "no-conf" => \$noconf,
148            )
149     or die "Usage: $progname [options] changelog ...\nRun $progname --help for more details\n";
150
151 if ($noconf) {
152     die "$progname: --no-conf is only acceptable as the first command-line option!\n";
153 }
154
155 if ($help) {
156     usage();
157     exit 0;
158 }
159
160 if ($showversion) {
161     print $versioninfo;
162     exit 0;
163 }
164
165 if ($bugcount && $linecount) {
166     die "$progname: can't use --bugcount and --linecount\nRun $progname --help for usage information.\n";
167 }
168
169 if ($cumulative && ! $bugcount && ! $linecount) {
170     warn "$progname: --cumulative without --bugcount or --linecount: ignoring\nRun $progname --help for usage information.\n";
171 }
172
173 if (! @ARGV) {
174     die "Usage: $progname [options] changelog ...\nRun $progname --help for more details\n";
175 }
176
177 my %data;
178 my ($package, $version, $maintainer, $date, $urgency)=undef;
179 my ($data_tmpfile, $script_tmpfile);
180 my ($data_fh, $script_fh);
181
182 if (! $dump) {
183     $data_fh = tempfile("plotdataXXXXXX", UNLINK => 1)
184         or die "cannot create temporary file: $!";
185     fcntl $data_fh, Fcntl::F_SETFD(), 0
186         or die "disabling close-on-exec for temporary file: $!";
187     $script_fh = tempfile("plotscriptXXXXXX", UNLINK => 1)
188         or die "cannot create temporary file: $!";
189     fcntl $script_fh, Fcntl::F_SETFD(), 0
190         or die "disabling close-on-exec for temporary file: $!";
191     $data_tmpfile='/dev/fd/'.fileno($data_fh);
192     $script_tmpfile='/dev/fd/'.fileno($script_fh);
193 }
194 else {
195     $data_tmpfile='-';
196 }
197 my %pkgcount;
198 my $c;
199
200 # Changelog parsing.
201 foreach (@ARGV) {
202     if (/\.gz$/) {
203         open F,"zcat $_|" || die "$_: $!";
204     }
205     else {
206         open F,$_ || die "$_: $!";
207     }
208
209     while (<F>) {
210         chomp;
211         # Note that some really old changelogs use priority, not urgency.
212         if (/^(\w+.*?)\s+\((.*?)\)\s+.*?;\s+(?:urgency|priority)=(.*)/i) {
213             $package=lc($1);
214             $version=$2;
215             if ($show_urgency) {
216                 $urgency=$3;
217                 if ($urgency=~/high/i) {
218                     $urgency=2;
219                 }
220                 elsif ($urgency=~/medium/i) {
221                     $urgency=1.5;
222                 }
223                 else {
224                     $urgency=1;
225                 }
226             }
227             else {
228                 $urgency=1;
229             }
230             undef $maintainer;
231             undef $date;
232             $c=0;
233         }
234         elsif (/^ -- (.*?)  (.*)/) {
235             $maintainer=$1;
236             $date=str2time($2);
237
238             # Strip email address.
239             $maintainer=~s/<.*>//;
240             $maintainer=~s/\(.*\)//;
241             $maintainer=~s/\s+$//;
242         }
243         elsif (/^(\w+.*?)\s+\((.*?)\)\s+/) {
244             print STDERR qq[Parse error on "$_"\n];
245         }
246         elsif ($linecount && /^  /) {
247             $c++; # count changelog size.
248         }
249         elsif ($bugcount && /^  /) {
250             # count bugs that were said to be closed.
251             my @bugs=m/#\d+/g;
252             $c+=$#bugs+1;
253         }
254
255         if (defined $package && defined $version &&
256             defined $maintainer && defined $date && defined $urgency) {
257             $data{$package}{$pkgcount{$package}++}=
258                 [$linecount || $bugcount ? $c : $version,
259                  $maintainer, $date, $urgency];
260             undef $package;
261             undef $version;
262             undef $maintainer;
263             undef $date;
264             undef $urgency;
265         }
266     }
267
268     close F;
269 }
270
271 if ($cumulative) {
272     # have to massage the data; based on some code from later on
273     foreach $package (keys %data) {
274         my $total = 0;
275         # It's crucial the output is sorted by date.
276         foreach my $i (sort {$data{$package}{$a}[2] <=> $data{$package}{$b}[2]}
277                        keys %{$data{$package}}) {
278             $total += $data{$package}{$i}[0];
279             $data{$package}{$i}[0] = $total;
280         }
281     }
282 }
283
284 my $header=q{
285 set key below title "key" box
286 set timefmt "%m/%d/%Y %H:%M"
287 set xdata time
288 set format x "%m/%y"
289 set yrange [0 to *]
290 };
291 if ($linecount) {
292     if ($cumulative) { $header.="set ylabel 'Cumulative changelog length'\n"; }
293     else { $header.="set ylabel 'Changelog length'\n"; }
294 }
295 elsif ($bugcount) {
296     if ($cumulative) { $header.="set ylabel 'Cumulative bugs closed'\n"; }
297     else { $header.="set ylabel 'Bugs closed'\n"; }
298 }
299 else {
300     $header.="set ylabel 'Debian version'\n";
301 }
302 if ($save_filename) {
303     $header.="set terminal postscript color solid\n";
304     $header.="set output '$save_filename'\n";
305 }
306 my $script="plot ";
307 my $data='';
308 my $index=0;
309 my %maintdata;
310
311 # Note that "lines" is used if we are also showing maintainer info,
312 # otherwise we use "linespoints" to make sure points show up for each
313 # release anyway.
314 my $style = $no_maintainer ? "linespoints" : "lines";
315
316 foreach $package (keys %data) {
317     my $oldmaintainer="";
318     my $oldversion="";
319     # It's crucial the output is sorted by date.
320     foreach my $i (sort {$data{$package}{$a}[2] <=> $data{$package}{$b}[2]}
321                    keys %{$data{$package}}) {
322         my $v=$data{$package}{$i}[0];
323         $maintainer=$data{$package}{$i}[1];
324         $date=$data{$package}{$i}[2];
325         $urgency=$data{$package}{$i}[3];
326
327         $maintainer=~s/"/\\"/g;
328
329         my $y;
330
331         # If it's got a debian revision, use that as the y coordinate.
332         if ($v=~m/(.*)-(.*)/) {
333             $y=$2;
334             $version=$1;
335         }
336         else {
337             $y=$v;
338         }
339
340         # Now make sure the version is a real number. This includes making
341         # sure it has no more than one decimal point in it, and getting rid of
342         # any nonnumeric stuff. Otherwise, the "set label" command below could
343         # fail. Luckily, perl's string -> num conversion is perfect for this job.
344         $y=$y+0;
345                 
346         if (lc($maintainer) ne lc($oldmaintainer)) {
347             $oldmaintainer=$maintainer;
348         }
349                 
350         my ($sec, $min, $hour, $mday, $mon, $year)=localtime($date);
351         my $x=($mon+1)."/$mday/".(1900+$year)." $hour:$min";
352         $data.="$x\t$y\n";
353         $maintdata{$oldmaintainer}{$urgency}.="$x\t$y\n";
354                 
355         if ($oldversion ne $version && ! $no_version) {
356             # Upstream version change. Label it.
357             $header.="set label '$version' at '$x',$y left\n";
358             $oldversion=$version;
359         }
360     }
361     $data.="\n\n"; # start new dataset
362     # Add to plot command.
363     $script.="'$data_tmpfile' index $index using 1:3 title '$package' with $style, ";
364     $index++;
365 }
366
367 # Add a title.
368 my $title.="set title '";
369 $title.=$#ARGV > 1 ? "Graphing Debian changelogs" :
370     "Graphing Debian changelog";
371 $title.="'\n";
372
373 if (! $no_maintainer) {
374     foreach $maintainer (sort keys %maintdata) {
375         foreach $urgency (sort keys %{$maintdata{$maintainer}}) {
376             $data.=$maintdata{$maintainer}{$urgency}."\n\n";
377             $script.="'$data_tmpfile' index $index using 1:3 title \"$maintainer\" with points pointsize ".(1.5 * $urgency).", ";
378             $index++;
379         }       
380     }
381 }
382
383 $script=~s/, $/\n/;
384 $script=qq{
385 $header
386 $title
387 $extra_gnuplot_commands
388 $gnuplot_commands
389 $script
390 };
391 $script.="pause -1 'Press Return to continue.'\n"
392     unless $save_filename || $dump;
393
394 if (! $dump) {
395     # Annoyingly, we have to use 2 temp files. I could just send everything to
396     # gnuplot on stdin, but then the pause -1 doesn't work.
397     open (DATA, ">$data_tmpfile") || die "$data_tmpfile: $!";
398     open (SCRIPT, ">$script_tmpfile") || die "$script_tmpfile: $!";
399 }
400 else {
401     open (DATA, ">&STDOUT");
402     open (SCRIPT, ">&STDOUT");
403 }
404
405 print SCRIPT $script;
406 print $script if $verbose && ! $dump;
407 print DATA $data;
408 close SCRIPT;
409 close DATA;
410
411 if (! $dump) {
412     unless (system("gnuplot",$script_tmpfile) == 0) {
413         die "gnuplot program failed (is the gnuplot package installed?): $!\n";
414     }
415 }