]> git.donarmstrong.com Git - debbugs.git/blob - cgi/version.cgi
add suport for width/height to version.cgi
[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 width height)],
38                              default => {package       => 'xterm',
39                                          found         => [],
40                                          fixed         => [],
41                                          ignore_boring => 1,
42                                          format        => 'png',
43                                          width         => undef,
44                                          height        => undef,
45                                         },
46                             );
47
48 # we want to first load the appropriate file,
49 # then figure out which versions are there in which architectures,
50 my %versions;
51 my %version_to_dist;
52 for my $dist (@{$config{distributions}}) {
53      $versions{$dist} = [getversions($cgi_var{package},$dist)];
54      # make version_to_dist
55      foreach my $version (@{$versions{$dist}}){
56           push @{$version_to_dist{$version}}, $dist;
57      }
58 }
59
60 if (defined $cgi_var{width}) {
61      $cgi_var{width} =~ /(\d+)/;
62      $cgi_var{width} = $1;
63 }
64 if (defined $cgi_var{height}) {
65      $cgi_var{height} =~ /(\d+)/;
66      $cgi_var{height} = $1;
67 }
68
69 # then figure out which are affected.
70 # turn found and fixed into full versions
71 @{$cgi_var{found}} = makesourceversions($cgi_var{package},undef,@{$cgi_var{found}});
72 @{$cgi_var{fixed}} = makesourceversions($cgi_var{package},undef,@{$cgi_var{fixed}});
73 my @interesting_versions = makesourceversions($cgi_var{package},undef,keys %version_to_dist);
74
75 # We need to be able to rip out leaves which the versions that do not affect the current versions of unstable/testing
76 my %sources;
77 @sources{map {m{(.+)/}; $1} @{$cgi_var{found}}} = (1) x @{$cgi_var{found}};
78 @sources{map {m{(.+)/}; $1} @{$cgi_var{fixed}}} = (1) x @{$cgi_var{fixed}};
79 @sources{map {m{(.+)/}; $1} @interesting_versions} = (1) x @interesting_versions;
80 my $version = Debbugs::Versions->new(\&Debbugs::Versions::Dpkg::vercmp);
81 foreach my $source (keys %sources) {
82      my $srchash = substr $source, 0, 1;
83      my $version_fh = new IO::File "$config{version_packages_dir}/$srchash/$source", 'r';
84      $version->load($version_fh);
85 }
86 # Here, we need to generate a short version to full version map
87 my %version_map;
88 foreach my $key (keys %{$version->{parent}}) {
89      my ($short_version) = $key =~ m{/(.+)$};
90      next unless length $short_version;
91      # we let the first short version have presidence.
92      $version_map{$short_version} = $key if not exists $version_map{$short_version};
93 }
94 # Turn all short versions into long versions
95 for my $found_fixed (qw(found fixed)) {
96      $cgi_var{$found_fixed} =
97           [
98            map {
99                 if ($_ !~ m{/}) { # short version
100                      ($version_map{$_});
101                 }
102                 else { # long version
103                      ($_);
104                 }
105            } @{$cgi_var{$found_fixed}}
106           ];
107 }
108 my %all_states = $version->allstates($cgi_var{found},$cgi_var{fixed});
109
110 my $dot = "digraph G {\n";
111 if (defined $cgi_var{width} and defined $cgi_var{height}) {
112      $dot .= qq(size="$cgi_var{width},$cgi_var{height}";\n);
113 }
114 my %state = (found  => ['fillcolor="salmon"',
115                         'style="filled"',
116                         'shape="diamond"',
117                        ],
118              absent => ['fillcolor="grey"',
119                         'style="filled"',
120                        ],
121              fixed  => ['fillcolor="chartreuse"',
122                         'style="filled"',
123                         'shape="rect"',
124                        ],
125             );
126 foreach my $key (keys %all_states) {
127      my ($short_version) = $key =~ m{/(.+)$};
128      next if $cgi_var{ignore_boring} and (not defined $all_states{$key}
129                                           or $all_states{$key} eq 'absent');
130      next if $cgi_var{ignore_boring} and not version_relevant($version,$key,\@interesting_versions);
131      my @attributes = @{$state{$all_states{$key}}};
132      if (length $short_version and exists $version_to_dist{$short_version}) {
133           push @attributes, 'label="'.$key.'\n'."(".join(', ',@{$version_to_dist{$short_version}}).")\"";
134      }
135      my $node_attributes = qq("$key" [).join(',',@attributes).qq(]\n);
136      $dot .= $node_attributes;
137 }
138 foreach my $key (keys %{$version->{parent}}) {
139      next if not defined $version->{parent}{$key};
140      next if $cgi_var{ignore_boring} and $all_states{$key} eq 'absent';
141      next if $cgi_var{ignore_boring} and (not defined $all_states{$version->{parent}{$key}}
142                                           or $all_states{$version->{parent}{$key}} eq 'absent');
143      # Ignore branches which are not ancestors of a currently distributed version
144      next if $cgi_var{ignore_boring} and not version_relevant($version,$key,\@interesting_versions);
145      $dot .= qq("$key").'->'.qq("$version->{parent}{$key}" [dir="back"])."\n" if defined $version->{parent}{$key};
146 }
147 $dot .= "}\n";
148
149 my $temp_dir = tempdir(CLEANUP => 1);
150
151 if (not defined $cgi_var{dot}) {
152      my $dot_fh = new IO::File "$temp_dir/temp.dot",'w' or
153           die "Unable to open $temp_dir/temp.dot for writing: $!";
154      print {$dot_fh} $dot or die "Unable to print output to the dot file: $!";
155      close $dot_fh or die "Unable to close the dot file: $!";
156      system('dot','-T'.$cgi_var{format},"$temp_dir/temp.dot",'-o',"$temp_dir/temp.$cgi_var{format}") == 0
157           or print "Content-Type: text\n\nDot failed." and die "Dot failed: $?";
158      my $img_fh = new IO::File "$temp_dir/temp.$cgi_var{format}", 'r' or
159           die "Unable to open $temp_dir/temp.$cgi_var{format} for reading: $!";
160      print "Content-Type: $img_types{$cgi_var{format}}\n\n";
161      print <$img_fh>;
162      close $img_fh;
163 }
164 else {
165      print "Content-Type: text\n\n";
166      print $dot;
167 }
168
169
170 my %_version_relevant_cache;
171 sub version_relevant {
172      my ($version,$test_version,$relevant_versions) = @_;
173      for my $dist_version (@{$relevant_versions}) {
174           print STDERR "Testing $dist_version against $test_version\n";
175           return 1 if $version->isancestor($test_version,$dist_version);
176      }
177      return 0;
178 }
179
180