6 # Hack to work on merkel where suexec is in place
8 if (defined $ENV{HTTP_HOST} and $ENV{HTTP_HOST} eq 'merkel.debian.org') {
9 unshift @INC, qw(/home/don/perl/usr/share/perl5 /home/don/perl/usr/lib/perl5 /home/don/source);
10 $ENV{DEBBUGS_CONFIG_FILE}="/home/don/config_internal";
14 # if we're running out of git, we want to use the git base directory as the
15 # first INC directory. If you're not running out of git, don't do that.
16 use File::Basename qw(dirname);
21 abs_path(dirname(abs_path(__FILE__)) . '/../');
22 # clear the taint; we'll assume that the absolute path to __FILE__ is the
23 # right path if there's a .git directory there
24 ($debbugs_dir) = $debbugs_dir =~ /([[:print:]]+)/;
25 if (defined $debbugs_dir and
26 -d $debbugs_dir . '/.git/') {
30 # if the first directory in @INC is not an absolute directory, assume that
31 # someone has overridden us via -I.
32 if ($INC[0] !~ /^\//) {
35 use if defined $debbugs_dir, lib => $debbugs_dir;
39 # by default send this message nowhere
40 # use CGI::Alert q(nobody@example.com);
42 use Debbugs::Config qw(:config);
46 use Debbugs::CGI qw(htmlize_packagelinks html_escape cgi_parameters munge_url :cache);
47 use Debbugs::Versions;
48 use Debbugs::Versions::Dpkg;
49 use Debbugs::Packages qw(get_versions makesourceversions);
50 use HTML::Entities qw(encode_entities);
51 use File::Temp qw(tempdir);
56 if (defined $config{database}) {
57 $s = Debbugs::DB->connect($config{database}) or
58 die "Unable to connect to DB";
59 @schema_arg = ('schema',$s);
62 my %img_types = (svg => 'image/svg+xml',
66 my $q = CGI::Simple->new();
68 my %cgi_var = cgi_parameters(query => $q,
69 single => [qw(package format ignore_boring width height collapse info)],
70 default => {package => 'spamass-milter',
81 my $this = munge_url('version.cgi?',
85 if (defined $cgi_var{width}) {
86 $cgi_var{width} =~ /(\d+)/;
89 if (defined $cgi_var{height}) {
90 $cgi_var{height} =~ /(\d+)/;
91 $cgi_var{height} = $1;
94 if (defined $cgi_var{format}) {
95 $cgi_var{format} =~ /(png|svg|jpg|gif)/;
96 $cgi_var{format} = $1 || 'png';
99 $cgi_var{format} = 'png';
103 if ($cgi_var{info} and not defined $cgi_var{dot}) {
104 $etag = etag_does_not_match(cgi=>$q,
105 additional_data=>[grep {defined $_ ? $_ : ()}
106 @cgi_var{(qw(package ignore_boring),
113 print $q->header(-status => 304);
114 print "304: Not modified\n";
117 print $q->header(-status => 200,
118 -cache_control => 'public, max-age=86400',
120 -content_type => 'text/html',
123 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
126 print '<head><title>'.html_escape($cgi_var{package}).' Version Graph</title></head>'."\n";
128 print '<a href="'.html_escape(munge_url($this,ignore_boring=>$cgi_var{ignore_boring}?0:1)).
129 '">['.($cgi_var{ignore_boring}?"Don't i":'I').'gnore boring]</a> ';
130 print '<a href="'.html_escape(munge_url($this,collapse=>$cgi_var{collapse}?0:1)).
131 '">['.($cgi_var{collapse}?"Don't c":'C').'ollapse]</a> ';
132 print '<a href="'.html_escape(munge_url($this,dot=>1)).
134 print '<img src="'.html_escape(munge_url($this,info=>0)).'">';
142 # we want to first load the appropriate file,
143 # then figure out which versions are there in which architectures,
146 for my $dist (@{$config{distributions}}) {
147 $versions{$dist} = [get_versions(package => [split /\s*,\s*/, $cgi_var{package}],
152 # make version_to_dist
153 foreach my $version (@{$versions{$dist}}){
154 push @{$version_to_dist{$version}}, $dist;
158 # then figure out which are affected.
159 # turn found and fixed into full versions
160 @{$cgi_var{found}} = map {makesourceversions($_,undef,@{$cgi_var{found}})} split/\s*,\s*/, $cgi_var{package};
161 @{$cgi_var{fixed}} = map {makesourceversions($_,undef,@{$cgi_var{fixed}})} split/\s*,\s*/, $cgi_var{package};
162 my @interesting_versions = map {makesourceversions($_,undef,keys %version_to_dist)} split/\s*,\s*/, $cgi_var{package};
164 # We need to be able to rip out leaves which the versions that do not affect the current versions of unstable/testing
166 @sources{map {m{(.+)/}; $1} @{$cgi_var{found}}} = (1) x @{$cgi_var{found}};
167 @sources{map {m{(.+)/}; $1} @{$cgi_var{fixed}}} = (1) x @{$cgi_var{fixed}};
168 @sources{map {m{(.+)/}; $1} @interesting_versions} = (1) x @interesting_versions;
170 # at this point, we know enough to calculate the etag.
173 foreach my $source (keys %sources) {
174 my $srchash = substr $source, 0, 1;
175 next unless -e "$config{version_packages_dir}/$srchash/$source";
176 push @versions_files, "$config{version_packages_dir}/$srchash/$source";
179 $etag = etag_does_not_match(cgi=>$q,
180 additional_data=>[@cgi_var{(qw(package ignore_boring),
185 files => [@versions_files
189 print $q->header(-status => 304);
190 print "304: Not modified\n";
194 my $version = Debbugs::Versions->new(\&Debbugs::Versions::Dpkg::vercmp);
195 for my $version_fn (@versions_files) {
196 my $version_fh = IO::File->new($version_fn, 'r') or
197 warn "Unable to open $version_fn for reading: $!";
198 $version->load($version_fh);
200 # Here, we need to generate a short version to full version map
202 foreach my $key (keys %{$version->{parent}}) {
203 my ($short_version) = $key =~ m{/(.+)$};
204 next unless length $short_version;
205 # we let the first short version have presidence.
206 $version_map{$short_version} = $key if not exists $version_map{$short_version};
208 # Turn all short versions into long versions
209 for my $found_fixed (qw(found fixed)) {
210 $cgi_var{$found_fixed} =
213 if ($_ !~ m{/}) { # short version
216 else { # long version
219 } @{$cgi_var{$found_fixed}}
222 my %all_states = $version->allstates($cgi_var{found},$cgi_var{fixed});
224 my $dot = "digraph G {\n";
225 if (defined $cgi_var{width} and defined $cgi_var{height}) {
226 $dot .= qq(size="$cgi_var{width},$cgi_var{height}";\n);
228 my %state = (found => ['fillcolor="salmon"',
232 absent => ['fillcolor="grey"',
236 fixed => ['fillcolor="chartreuse"',
241 # TODO: Allow collapsing versions which are at the same state and not
245 if ($cgi_var{collapse}) {
247 foreach my $key (keys %{$version->{parent}}) {
248 next if $cgi_var{ignore_boring} and (not defined $all_states{$key}
249 or $all_states{$key} eq 'absent');
250 next if $cgi_var{ignore_boring} and not version_relevant($version,$key,\@interesting_versions);
251 if (defined $version->{parent}{$key}) {
252 next if $cgi_var{ignore_boring} and (not defined $all_states{$version->{parent}{$key}}
253 or $all_states{$version->{parent}{$key}} eq 'absent');
254 next if $cgi_var{ignore_boring} and not version_relevant($version,$version->{parent}{$key},\@interesting_versions);
255 push @{$reversed_nodes{$version->{parent}{$key}}},$key;
258 $reversed_nodes{$key} ||=[];
261 # nodes that can be collapsed are those that have one child
262 # are in the same state as their parent, and are not in a suite
263 foreach my $key (keys %reversed_nodes) {
264 if (not exists $version_to_dist{$key}
265 and @{$reversed_nodes{$key}} <= 1
266 and defined $version->{parent}{$key}
267 and $all_states{$key} eq $all_states{$version->{parent}{$key}}
269 # check to see if there is an entry for the parent or child of this node
271 if ((@{$reversed_nodes{$key}} and exists $collapsed_nodes{$reversed_nodes{$key}[0]})) {
272 $group_node = $collapsed_nodes{$reversed_nodes{$key}[0]};
273 if ($group_nodes{$group_node}{parent} eq $key) {
274 $group_nodes{$group_node}{parent} = $version->{parent}{$key};
277 if (defined $version->{parent}{$key} and exists $collapsed_nodes{$version->{parent}{$key}}) {
278 if (defined $group_node) {
280 my $collapser = $group_nodes{$collapsed_nodes{$version->{parent}{$key}}};
281 push @{$collapser->{collapsed_nodes}},@{$group_nodes{$group_node}{collapsed_nodes}},$group_node;
282 foreach (@{$collapser->{collapsed_nodes}}) {
283 if (exists $group_nodes{$_}) {
284 $group_nodes{$_} = $collapser;
288 $group_node = $collapsed_nodes{$version->{parent}{$key}};
290 if (not defined $group_node) {
291 $group_node = "group_$key";
292 $group_nodes{$group_node} = {attr => qq("$group_node" [).join(',','label="some versions"',
293 @{$state{$all_states{$key}}},
294 'style="bold,filled"',
297 parent => $version->{parent}{$key},
298 collapsed_nodes => [],
301 $collapsed_nodes{$key} = $group_node;
305 foreach my $group (values %group_nodes) {
306 next if $used_node{$group->{name}};
307 $used_node{$group->{name}} = 1;
308 $dot .= $group->{attr};
312 foreach my $key (keys %all_states) {
313 next if exists $collapsed_nodes{$key};
314 next if $cgi_var{ignore_boring} and (not defined $all_states{$key}
315 or $all_states{$key} eq 'absent');
316 next if $cgi_var{ignore_boring} and not version_relevant($version,$key,\@interesting_versions);
317 my @attributes = @{$state{$all_states{$key}}};
318 if (exists $version_to_dist{$key}) {
319 push @attributes, 'label="'.$key.'\n'."(".join(', ',@{$version_to_dist{$key}}).")\"";
321 my $node_attributes = qq("$key" [).join(',',@attributes).qq(]\n);
322 $dot .= $node_attributes;
325 foreach my $key (keys %{$version->{parent}}) {
326 next if not defined $version->{parent}{$key};
327 next if $cgi_var{ignore_boring} and $all_states{$key} eq 'absent';
328 next if $cgi_var{ignore_boring} and (not defined $all_states{$version->{parent}{$key}}
329 or $all_states{$version->{parent}{$key}} eq 'absent');
330 # Ignore branches which are not ancestors of a currently distributed version
331 next if $cgi_var{ignore_boring} and not version_relevant($version,$key,\@interesting_versions);
332 next if exists $collapsed_nodes{$key};
333 $dot .= qq("$key").'->'.q(").
334 (exists $collapsed_nodes{$version->{parent}{$key}}?
335 $group_nodes{$collapsed_nodes{$version->{parent}{$key}}}{name}:$version->{parent}{$key}).
336 qq(" [dir="back"])."\n" if defined $version->{parent}{$key};
338 if ($cgi_var{collapse}) {
340 foreach my $group (values %group_nodes) {
341 next if $used_node{$group->{name}};
342 next if not defined $group->{parent};
343 $used_node{$group->{name}} = 1;
344 $dot .= qq("$group->{name}").'->'.q(").
345 (exists $collapsed_nodes{$group->{parent}}?
346 $group_nodes{$collapsed_nodes{$group->{parent}}}{name}:$group->{parent}).
347 qq(" [dir="back"])."\n";
352 my $temp_dir = tempdir(CLEANUP => 1);
354 if (not defined $cgi_var{dot}) {
355 my $dot_fh = IO::File->new("$temp_dir/temp.dot",'w') or
356 die "Unable to open $temp_dir/temp.dot for writing: $!";
357 print {$dot_fh} $dot or die "Unable to print output to the dot file: $!";
358 close $dot_fh or die "Unable to close the dot file: $!";
359 system('dot','-T'.$cgi_var{format},"$temp_dir/temp.dot",'-o',"$temp_dir/temp.$cgi_var{format}") == 0
360 or print "Content-Type: text\n\nDot failed." and die "Dot failed: $?";
361 my $img_fh = IO::File->new("$temp_dir/temp.$cgi_var{format}", 'r') or
362 die "Unable to open $temp_dir/temp.$cgi_var{format} for reading: $!";
363 print $q->header(-status => 200,
364 -cache_control => 'public, max-age=300',
366 -content_type => $img_types{$cgi_var{format}},
372 print $q->header(-status => 200,
373 -cache_control => 'public, max-age=300',
375 -content_type => 'text',
381 our %_version_relevant_cache;
382 sub version_relevant {
383 my ($version,$test_version,$relevant_versions) = @_;
384 for my $dist_version (@{$relevant_versions}) {
385 if (exists $_version_relevant_cache{$dist_version}{$test_version}{$version}) {
386 return 1 if $_version_relevant_cache{$dist_version}{$test_version}{$version};
389 my $rel = $version->isancestor($test_version,$dist_version);
390 $_version_relevant_cache{$dist_version}{$test_version}{$version} = $rel;