]> git.donarmstrong.com Git - debbugs.git/blob - examples/debian/versions/build-versions-db
remove compprefix
[debbugs.git] / examples / debian / versions / build-versions-db
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 <don@donarmstrong.com>.
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<--debug, -d>
34
35 Debug verbosity. (Default 0)
36
37 =item B<--help, -h>
38
39 Display brief usage information.
40
41 =item B<--man, -m>
42
43 Display this manual.
44
45 =back
46
47 =head1 EXAMPLES
48
49      build-versions-db versions.idx.new versions.idx.new \
50            /srv/bugs.debian.org/versions/indices/ftp \
51            stable
52
53 =cut
54
55
56 use vars qw($DEBUG);
57 use Debbugs::Versions::Dpkg;
58 use IO::Uncompress::AnyUncompress;
59 use MLDBM qw(DB_File Storable);
60 use Fcntl;
61
62 my %options = (debug           => 0,
63                help            => 0,
64                man             => 0,
65               );
66
67 GetOptions(\%options,
68            'debug|d+','help|h|?','man|m');
69
70 pod2usage() if $options{help};
71 pod2usage({verbose=>2}) if $options{man};
72
73 $DEBUG = $options{debug};
74
75 my @USAGE_ERRORS;
76
77 if (not @ARGV >= 4) {
78     push @USAGE_ERRORS,
79         "You must provide at least four arguments, two databases, ".
80         "a top level directory and at least one suite";
81 }
82
83 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
84
85
86 my $versions = shift @ARGV;
87 my $versions_time = shift @ARGV;
88 my $toplevel = shift @ARGV;
89 my @suites = @ARGV;
90
91 $MLDBM::DumpMeth=q(portable);
92
93 my $time = time;
94
95 my %db;
96 my %db2;
97 tie %db, "MLDBM", $versions, O_CREAT|O_RDWR, 0664
98     or die "tie $versions: $!";
99 tie %db2, "MLDBM", $versions_time,O_CREAT|O_RDWR, 0664
100      or die "tie $versions_time failed: $!";
101
102 # Read Package, Version, and Source fields from a Packages.gz file.
103 sub read_packages {
104     my ($packages, $component,$arch,$dist) = @_;
105     my $PACKAGES = IO::Uncompress::AnyUncompress->new($packages) or
106         die "Unable to open $packages for reading: $!";
107     local $_;
108     local $/ = '';      # paragraph mode
109
110     print STDERR "reading packages $packages\n" if $DEBUG;
111     for (<$PACKAGES>) {
112         /^Package: (.+)/im or next;
113         my $pkg = $1;
114         /^Version: (.+)/im or next;
115         my $ver = $1;
116         my $extra_source_only = 0;
117         if (/^Extra-Source-Only: yes/im) {
118             $extra_source_only = 1;
119         }
120         update_package_version($dist,$arch,$pkg,$ver,$time) unless
121             $extra_source_only;
122     }
123 }
124
125
126 sub update_package_version {
127     my ($d,$a,$p,$v,$t) = @_;
128     # see MLDBM(3pm)/BUGS
129     my $tmp = $db{$p};
130     # we allow multiple versions in an architecture now; this
131     # should really only happen in the case of source, however.
132     push @{$tmp->{$d}{$a}}, $v;
133     $db{$p} = $tmp;
134     $tmp = $db2{$p};
135     $tmp->{$d}{$a}{$v} = $time if not exists
136         $tmp->{$d}{$a}{$v};
137     $db2{$p} = $tmp;
138 }
139
140 # Iterate through all Packages and Sources files.
141 for my $suite (@suites) {
142     my $suitedir = "$toplevel/$suite";
143
144     for my $component ('main', 'main/debian-installer',
145                        'contrib', 'non-free') {
146         my $componentdir = "$suitedir/$component";
147         next unless -d $componentdir;
148         my $COMPONENT;
149         opendir $COMPONENT, $componentdir or die "opendir $componentdir: $!";
150
151         # debian-installer is really a section rather than a component
152         # (ugh).
153         (my $viscomponent = $component) =~ s[/.*][];
154
155         my $sources = (grep { -f $_ } glob "$suitedir/$component/source/Sources.*")[0];
156         next unless defined $sources;
157         read_packages($sources, $viscomponent,'source',$suite);
158
159         for my $arch (readdir $COMPONENT) {
160             next unless $arch =~ s/^binary-//;
161             my $archdir = "$componentdir/binary-$arch";
162
163             my $packages = (grep { -f $_ } glob("$archdir/Packages.*"))[0];
164             next unless defined $packages;
165             read_packages($packages, $viscomponent,$arch,$suite);
166         }
167
168         closedir $COMPONENT;
169     }
170 }
171
172