| 1 |
#!/usr/bin/perl |
| 2 |
# build-versions-db builds the versions mldmb database |
| 3 |
# and is released under the terms of the GNU GPL version 3, or any |
| 4 |
# later version, at your option. See the file README and COPYING for |
| 5 |
# more information. |
| 6 |
# Copyright 2016 by Don Armstrong <[email protected]>. |
| 7 |
|
| 8 |
|
| 9 |
use warnings; |
| 10 |
use strict; |
| 11 |
|
| 12 |
use Getopt::Long; |
| 13 |
use Pod::Usage; |
| 14 |
|
| 15 |
=head1 NAME |
| 16 |
|
| 17 |
build-versions-db -- builds source and source maintainers file |
| 18 |
|
| 19 |
=head1 SYNOPSIS |
| 20 |
|
| 21 |
build-versions-db [options] versions.idx.new versions.idx.new \ |
| 22 |
/srv/bugs.debian.org/versions/indices/ftp |
| 23 |
|
| 24 |
Options: |
| 25 |
--debug, -d debugging level (Default 0) |
| 26 |
--help, -h display this help |
| 27 |
--man, -m display manual |
| 28 |
|
| 29 |
=head1 OPTIONS |
| 30 |
|
| 31 |
=over |
| 32 |
|
| 33 |
=item B<--update> |
| 34 |
|
| 35 |
Update an existing database; the default. B<--no-update> will regenerate an |
| 36 |
existing database from scratch. |
| 37 |
|
| 38 |
=item B<--debug, -d> |
| 39 |
|
| 40 |
Debug verbosity. (Default 0) |
| 41 |
|
| 42 |
=item B<--help, -h> |
| 43 |
|
| 44 |
Display brief usage information. |
| 45 |
|
| 46 |
=item B<--man, -m> |
| 47 |
|
| 48 |
Display this manual. |
| 49 |
|
| 50 |
=back |
| 51 |
|
| 52 |
=head1 EXAMPLES |
| 53 |
|
| 54 |
build-versions-db versions.idx.new versions.idx.new \ |
| 55 |
/srv/bugs.debian.org/versions/indices/ftp \ |
| 56 |
stable |
| 57 |
|
| 58 |
=cut |
| 59 |
|
| 60 |
|
| 61 |
use vars qw($DEBUG); |
| 62 |
use Debbugs::Versions::Dpkg; |
| 63 |
use Debbugs::Config qw(:config); |
| 64 |
use File::Copy; |
| 65 |
use MLDBM qw(DB_File Storable); |
| 66 |
use Fcntl; |
| 67 |
|
| 68 |
my %options = (debug => 0, |
| 69 |
help => 0, |
| 70 |
man => 0, |
| 71 |
update => 1, |
| 72 |
); |
| 73 |
|
| 74 |
GetOptions(\%options, |
| 75 |
'update!', |
| 76 |
'debug|d+','help|h|?','man|m'); |
| 77 |
|
| 78 |
pod2usage() if $options{help}; |
| 79 |
pod2usage({verbose=>2}) if $options{man}; |
| 80 |
|
| 81 |
$DEBUG = $options{debug}; |
| 82 |
|
| 83 |
my @USAGE_ERRORS; |
| 84 |
|
| 85 |
if (not @ARGV >= 4) { |
| 86 |
push @USAGE_ERRORS, |
| 87 |
"You must provide at least four arguments, two databases, ". |
| 88 |
"a top level directory and at least one suite"; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS; |
| 93 |
|
| 94 |
|
| 95 |
my $versions = shift @ARGV; |
| 96 |
my $versions_time = shift @ARGV; |
| 97 |
my $versions_new = $versions."_".$$."_".time; |
| 98 |
my $versions_time_new = $versions_time."_".$$."_".time; |
| 99 |
my $toplevel = shift @ARGV; |
| 100 |
my @suites = @ARGV; |
| 101 |
|
| 102 |
$MLDBM::DumpMeth=q(portable); |
| 103 |
|
| 104 |
my $time = time; |
| 105 |
|
| 106 |
my %db; |
| 107 |
my %db2; |
| 108 |
if ($options{update}) { |
| 109 |
copy($versions_time,$versions_time_new); |
| 110 |
} |
| 111 |
tie %db, "MLDBM", $versions_new, O_CREAT|O_RDWR, 0664 |
| 112 |
or die "tie $versions: $!"; |
| 113 |
tie %db2, "MLDBM", $versions_time_new,O_CREAT|O_RDWR, 0664 |
| 114 |
or die "tie $versions_time failed: $!"; |
| 115 |
|
| 116 |
update_versions_suites(\%db,\%db2,\@suites); |
| 117 |
versions_time_cleanup(\%db2) if $options{update}; |
| 118 |
|
| 119 |
move($versions_new,$versions); |
| 120 |
move($versions_time_new,$versions_time); |
| 121 |
|
| 122 |
sub open_compressed_file { |
| 123 |
my ($file) = @_; |
| 124 |
my $fh; |
| 125 |
my $mode = '<:encoding(UTF-8)'; |
| 126 |
my @opts; |
| 127 |
if ($file =~ /\.gz$/) { |
| 128 |
$mode = '-|:encoding(UTF-8)'; |
| 129 |
push @opts,'gzip','-dc'; |
| 130 |
} |
| 131 |
if ($file =~ /\.xz$/) { |
| 132 |
$mode = '-|:encoding(UTF-8)'; |
| 133 |
push @opts,'xz','-dc'; |
| 134 |
} |
| 135 |
if ($file =~ /\.bz2$/) { |
| 136 |
$mode = '-|:encoding(UTF-8)'; |
| 137 |
push @opts,'bzip2','-dc'; |
| 138 |
} |
| 139 |
open($fh,$mode,@opts,$file); |
| 140 |
return $fh; |
| 141 |
} |
| 142 |
|
| 143 |
# Read Package, Version, and Source fields from a Packages.gz file. |
| 144 |
sub read_packages { |
| 145 |
my ($db,$db2,$packages, $component,$arch,$dist) = @_; |
| 146 |
my $PACKAGES = open_compressed_file($packages) or |
| 147 |
die "Unable to open $packages for reading: $!"; |
| 148 |
local $_; |
| 149 |
local $/ = ''; # paragraph mode |
| 150 |
|
| 151 |
print STDERR "reading packages $packages\n" if $DEBUG; |
| 152 |
for (<$PACKAGES>) { |
| 153 |
/^Package: (.+)/im or next; |
| 154 |
my $pkg = $1; |
| 155 |
/^Version: (.+)/im or next; |
| 156 |
my $ver = $1; |
| 157 |
my $extra_source_only = 0; |
| 158 |
if (/^Extra-Source-Only: yes/im) { |
| 159 |
$extra_source_only = 1; |
| 160 |
} |
| 161 |
update_package_version($db,$db2,$dist,$arch,$pkg,$ver,$time) unless |
| 162 |
$extra_source_only; |
| 163 |
} |
| 164 |
close($PACKAGES) or |
| 165 |
die "Error while closing ${packages}: $!"; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
sub update_package_version { |
| 170 |
my ($db,$db2,$d,$a,$p,$v,$t) = @_; |
| 171 |
# see MLDBM(3pm)/BUGS |
| 172 |
my $tmp = $db->{$p}; |
| 173 |
# we allow multiple versions in an architecture now; this |
| 174 |
# should really only happen in the case of source, however. |
| 175 |
push @{$tmp->{$d}{$a}}, $v; |
| 176 |
$db->{$p} = $tmp; |
| 177 |
$tmp = $db2->{$p}; |
| 178 |
$tmp->{$d}{$a}{$v} = $time if not exists |
| 179 |
$tmp->{$d}{$a}{$v}; |
| 180 |
$db2->{$p} = $tmp; |
| 181 |
} |
| 182 |
|
| 183 |
sub update_versions_suites { |
| 184 |
my ($db,$db2,$suites) = @_; |
| 185 |
# Iterate through all Packages and Sources files. |
| 186 |
for my $suite (@{$suites}) { |
| 187 |
my $suitedir = "$toplevel/$suite"; |
| 188 |
|
| 189 |
for my $component ('main', 'main/debian-installer', |
| 190 |
'contrib', 'contrib/debian-installer', |
| 191 |
'non-free', 'non-free/debian-installer', |
| 192 |
'non-free-firmware', 'non-free-firmware/debian-installer', |
| 193 |
) { |
| 194 |
my $componentdir = "$suitedir/$component"; |
| 195 |
if (not -d $componentdir) { |
| 196 |
print STDERR "No directory $suitedir/$component\n" if $DEBUG; |
| 197 |
next; |
| 198 |
} |
| 199 |
my $COMPONENT; |
| 200 |
opendir $COMPONENT, $componentdir or die "opendir $componentdir: $!"; |
| 201 |
|
| 202 |
# debian-installer is really a section rather than a component |
| 203 |
# (ugh). |
| 204 |
my $viscomponent = $component; |
| 205 |
$viscomponent =~ s[/.*][]; |
| 206 |
|
| 207 |
my $sources = (grep { -f $_ } glob "$componentdir/source/Sources.*")[0]; |
| 208 |
if (not defined $sources) { |
| 209 |
print STDERR "No sources matching $componentdir/source/Sources.*\n" if $DEBUG; |
| 210 |
} else { |
| 211 |
read_packages($db,$db2,$sources, $viscomponent,'source',$suite); |
| 212 |
} |
| 213 |
for my $arch (readdir $COMPONENT) { |
| 214 |
next unless $arch =~ s/^binary-//; |
| 215 |
my $archdir = "$componentdir/binary-$arch"; |
| 216 |
|
| 217 |
my $packages = (grep { -f $_ } glob("$archdir/Packages.*"))[0]; |
| 218 |
if (not defined $packages) { |
| 219 |
print STDERR "No binary packages matching $archdir/Packages.*\n" if $DEBUG; |
| 220 |
next; |
| 221 |
} |
| 222 |
read_packages($db,$db2,$packages, $viscomponent,$arch,$suite); |
| 223 |
} |
| 224 |
|
| 225 |
closedir $COMPONENT or |
| 226 |
die "Unable to closedir $componentdir: $!"; |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
sub versions_time_cleanup { |
| 232 |
my ($db) = @_; |
| 233 |
my $time = time; |
| 234 |
for my $package (keys %{$db}) { |
| 235 |
my $temp = $db->{$package}; |
| 236 |
for my $dist (keys %{$temp}) { |
| 237 |
for my $arch (keys %{$temp->{$dist}}) { |
| 238 |
my @versions = (sort {$temp->{$dist}{$arch}{$a} <=> |
| 239 |
$temp->{$dist}{$arch}{$b} |
| 240 |
} |
| 241 |
keys %{$temp->{$dist}{$arch}}); |
| 242 |
next unless @versions > 1; |
| 243 |
for my $i (0 .. ($#versions-1)) { |
| 244 |
last if $temp->{$dist}{$arch}{$versions[$i+1]} > |
| 245 |
($time - $config{remove_age}*60*60*24); |
| 246 |
last if keys %{$temp->{$dist}{$arch}} <= 1; |
| 247 |
delete $temp->{$dist}{$arch}{$versions[$i]}; |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
$db->{$package} = $temp; |
| 252 |
} |
| 253 |
} |