]> git.donarmstrong.com Git - debbugs.git/blob - cgi/version.cgi
merge changes from mainline
[debbugs.git] / cgi / version.cgi
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 # Hack to work on merkel where suexec is in place
7 BEGIN{
8      if ($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";
11      }
12 }
13
14
15 use CGI::Simple;
16
17 use CGI::Alert 'don@donarmstrong.com';
18
19 use Debbugs::Config qw(:config);
20 use Debbugs::CGI qw(htmlize_packagelinks html_escape cgi_parameters);
21 use Debbugs::Versions;
22 use Debbugs::Versions::Dpkg;
23 use Debbugs::Packages qw(getversions makesourceversions);
24 use HTML::Entities qw(encode_entities);
25 use File::Temp qw(tempdir);
26 use IO::File;
27 use IO::Handle;
28
29
30 my %img_types = (svg => 'image/svg+xml',
31                  png => 'image/png',
32                 );
33
34 my $q = new CGI::Simple;
35
36 my %cgi_var = cgi_parameters(query   => $q,
37                              single  => [qw(package format ignore_boring)],
38                              default => {package       => 'xterm',
39                                          found         => [],
40                                          fixed         => [],
41                                          ignore_boring => 1,
42                                          format        => 'png',
43                                         },
44                             );
45
46 # we want to first load the appropriate file,
47 # then figure out which versions are there in which architectures,
48 my %versions;
49 my %version_to_dist;
50 for my $dist (@{$config{distributions}}) {
51      $versions{$dist} = [getversions($cgi_var{package},$dist)];
52      # make version_to_dist
53      foreach my $version (@{$versions{$dist}}){
54           push @{$version_to_dist{$version}}, $dist;
55      }
56 }
57 # then figure out which are affected.
58 # turn found and fixed into full versions
59 @{$cgi_var{found}} = makesourceversions($cgi_var{package},undef,@{$cgi_var{found}});
60 @{$cgi_var{fixed}} = makesourceversions($cgi_var{package},undef,@{$cgi_var{fixed}});
61 my @interesting_versions = makesourceversions($cgi_var{package},undef,keys %version_to_dist);
62
63 # We need to be able to rip out leaves which the versions that do not affect the current versions of unstable/testing
64 my %sources;
65 @sources{map {m{(.+)/}; $1} @{$cgi_var{found}}} = (1) x @{$cgi_var{found}};
66 @sources{map {m{(.+)/}; $1} @{$cgi_var{fixed}}} = (1) x @{$cgi_var{fixed}};
67 @sources{map {m{(.+)/}; $1} @interesting_versions} = (1) x @interesting_versions;
68 my $version = Debbugs::Versions->new(\&Debbugs::Versions::Dpkg::vercmp);
69 foreach my $source (keys %sources) {
70      my $srchash = substr $source, 0, 1;
71      my $version_fh = new IO::File "$config{version_packages_dir}/$srchash/$source", 'r';
72      $version->load($version_fh);
73 }
74 # Here, we need to generate a short version to full version map
75 my %version_map;
76 foreach my $key (keys %{$version->{parent}}) {
77      my ($short_version) = $key =~ m{/(.+)$};
78      next unless length $short_version;
79      # we let the first short version have presidence.
80      $version_map{$short_version} = $key if not exists $version_map{$short_version};
81 }
82 # Turn all short versions into long versions
83 for my $found_fixed (qw(found fixed)) {
84      $cgi_var{$found_fixed} =
85           [
86            map {
87                 if ($_ !~ m{/}) { # short version
88                      ($version_map{$_});
89                 }
90                 else { # long version
91                      ($_);
92                 }
93            } @{$cgi_var{$found_fixed}}
94           ];
95 }
96 my %all_states = $version->allstates($cgi_var{found},$cgi_var{fixed});
97
98 my $dot = "digraph G {\n";
99 my %state = (found  => ['fillcolor="salmon"',
100                         'style="filled"',
101                         'shape="diamond"',
102                        ],
103              absent => ['fillcolor="grey"',
104                         'style="filled"',
105                        ],
106              fixed  => ['fillcolor="chartreuse"',
107                         'style="filled"',
108                         'shape="rect"',
109                        ],
110             );
111 foreach my $key (keys %all_states) {
112      my ($short_version) = $key =~ m{/(.+)$};
113      next if $cgi_var{ignore_boring} and (not defined $all_states{$key}
114                                           or $all_states{$key} eq 'absent');
115      next if $cgi_var{ignore_boring} and not version_relevant($version,$key,\@interesting_versions);
116      my @attributes = @{$state{$all_states{$key}}};
117      if (length $short_version and exists $version_to_dist{$short_version}) {
118           push @attributes, 'label="'.$key.'\n'."(".join(', ',@{$version_to_dist{$short_version}}).")\"";
119      }
120      my $node_attributes = qq("$key" [).join(',',@attributes).qq(]\n);
121      $dot .= $node_attributes;
122 }
123 foreach my $key (keys %{$version->{parent}}) {
124      next if not defined $version->{parent}{$key};
125      next if $cgi_var{ignore_boring} and $all_states{$key} eq 'absent';
126      next if $cgi_var{ignore_boring} and (not defined $all_states{$version->{parent}{$key}}
127                                           or $all_states{$version->{parent}{$key}} eq 'absent');
128      # Ignore branches which are not ancestors of a currently distributed version
129      next if $cgi_var{ignore_boring} and not version_relevant($version,$key,\@interesting_versions);
130      $dot .= qq("$key").'->'.qq("$version->{parent}{$key}" [dir="back"])."\n" if defined $version->{parent}{$key};
131 }
132 $dot .= "}\n";
133
134 my $temp_dir = tempdir(CLEANUP => 1);
135
136 if (not defined $cgi_var{dot}) {
137      my $dot_fh = new IO::File "$temp_dir/temp.dot",'w' or
138           die "Unable to open $temp_dir/temp.dot for writing: $!";
139      print {$dot_fh} $dot or die "Unable to print output to the dot file: $!";
140      close $dot_fh or die "Unable to close the dot file: $!";
141      system('dot','-T'.$cgi_var{format},"$temp_dir/temp.dot",'-o',"$temp_dir/temp.$cgi_var{format}") == 0
142           or print "Content-Type: text\n\nDot failed." and die "Dot failed: $?";
143      my $img_fh = new IO::File "$temp_dir/temp.$cgi_var{format}", 'r' or
144           die "Unable to open $temp_dir/temp.$cgi_var{format} for reading: $!";
145      print "Content-Type: $img_types{$cgi_var{format}}\n\n";
146      print <$img_fh>;
147      close $img_fh;
148 }
149 else {
150      print "Content-Type: text\n\n";
151      print $dot;
152 }
153
154
155 my %_version_relevant_cache;
156 sub version_relevant {
157      my ($version,$test_version,$relevant_versions) = @_;
158      for my $dist_version (@{$relevant_versions}) {
159           print STDERR "Testing $dist_version against $test_version\n";
160           return 1 if $version->isancestor($test_version,$dist_version);
161      }
162      return 0;
163 }
164
165