]> git.deb.at Git - deb/packages.git/blob - bin/parse-packages
Move more messages from DoSearch.pm to search.tmpl, similar to what
[deb/packages.git] / bin / parse-packages
1 #!/usr/bin/perl -w
2 # Convert Packages.gz files into Sleepycat db files for efficient usage of
3 # data
4 #
5 # $Id$
6 #
7 # Copyright (C) 2006  Jeroen van Wolffelaar <jeroen@wolffelaar.nl>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
22 use strict;
23 use warnings;
24 use lib './lib';
25
26 $| = 1;
27
28 my $what = $ARGV[0] ? "non-free" : "*";
29 # max. distinct results for a given package postfix
30 my $MAX_PACKAGE_POSTFIXES = 100;
31
32 use DB_File;
33 use Storable;
34 use File::Path;
35 use Deb::Versions;
36 use Packages::Config qw( $TOPDIR $DBDIR @ARCHIVES @SUITES );
37 &Packages::Config::init( './' );
38 my %packages_small = ();
39 my %virtual_packages = ();
40 my %package_names = ();
41 my %package_postfixes = ();
42 my %sources_packages = ();
43 my %descriptions = ();
44 my @descriptions = ("we count lines one-based\000");
45 my %packages_descriptions = ();
46 my %descriptions_packages = ();
47
48 my %sections = ();
49 my %subsections = ();
50 my %priorities = ();
51
52 $/ = "";
53
54 -d $DBDIR || mkpath( $DBDIR );
55
56 for my $suite (@SUITES) {
57     my %package_names_suite = ();
58     my %packages_all_db;
59     tie %packages_all_db, "DB_File", "$DBDIR/packages_all_$suite.db.new",
60             O_RDWR|O_CREAT, 0666, $DB_BTREE
61             or die "Error creating DB: $!";
62
63     for my $archive (@ARCHIVES) {
64         print "Reading $archive/$suite...\n";
65         if (!-d "$TOPDIR/archive/$archive/$suite/") {
66                 print "\tseems not to exist, skipping...\n";
67                 next;
68         }
69         open PKG, "zcat $TOPDIR/archive/$archive/$suite/$what/{,debian-installer/}binary-*/Packages.gz|";
70         while (<PKG>) {
71                 next if /^\s*$/;
72                 my $data = "";
73                 my %data = ();
74                 chomp;
75                 s/\n /\377/g;
76                 while (/^(\S+):\s*(.*)\s*$/mg) {
77                         my ($key, $value) = ($1, $2);
78                         $value =~ s/\377/\n /g;
79                         $key =~ tr [A-Z] [a-z];
80                         $data{$key} = $value;
81                 }
82                 # Skip double package
83                 next if exists($packages_all_db{"$data{'package'} $data{'architecture'} $data{'version'}"});
84                 # Skip arch:all for amd64&kfreebsd, too often broken
85                 next if ($archive eq 'amd64' or $archive eq 'kfreebsd')
86                     and $data{architecture} eq 'all';
87
88                 if ($data{'provides'}) {
89                     foreach (split /\s*,\s*/, $data{'provides'}) {
90                         $virtual_packages{$_}{$suite}{$data{'package'}}++;
91                         $packages_small{$_} ||= {};
92                     }
93                 }
94                 $package_names{$data{'package'}} = 1;
95                 $package_names_suite{$data{'package'}} = 1;
96                 my $src = $data{'package'};
97                 my $src_version = '';
98                 if ($data{'source'}) {
99                         $src = $data{'source'};
100                         $src =~ s/\s+.*//; # strip version info
101                 }
102                 $data{'source'} = $src;
103                 my $descr = $data{'description'};
104                 my $did = undef;
105                 if (exists($descriptions{$descr})) {
106                         $did  = $descriptions{$descr};
107                 } else {
108                         $did = 1 + $#descriptions;
109                         $descriptions[$did] = $descr;
110                         $descriptions{$descr} = $did;
111                 }
112                 $data{'description'} = $did;
113                 $packages_descriptions{"$data{'package'} $data{'version'} $data{'architecture'}"} = $did;
114                 $descriptions_packages{$did} .=
115                         "$data{'package'} $data{'version'} $data{'architecture'}\000";
116
117                 my $sdescr = $descr;
118                 $sdescr =~ s/\n.*//s;
119                 my $section = 'main';
120                 my $subsection = $data{section} || '-';
121                 if ($data{section} && ($data{section} =~ m=/=o)) {
122                     ($section, $subsection) = split m=/=o, $data{section}, 2;
123                     ($subsection, $section) = split m=/=o, $data{section}, 2
124                         if $section eq 'non-US';
125                 }
126                 $data{'section'} = $section;
127                 $data{'subsection'} = $subsection;
128                 $data{'priority'} ||= '-';
129                 $sections{$suite}{$section}++;
130                 $subsections{$suite}{$subsection}++;
131                 $priorities{$suite}{$data{priority}}++;
132                 my $pkgitem = "$archive $suite $data{'architecture'} ".
133                         "$section $subsection $data{'priority'} $data{'version'} $sdescr\0";
134                 my $previtem = ($packages_small{$data{'package'}}{$suite}{$data{'architecture'}}
135                     ||= $pkgitem);
136                 $packages_small{$data{'package'}}{$suite}{$data{'architecture'}} = $pkgitem
137                     if version_cmp($data{'version'}, (split /\s/o, $previtem)[6]) > 0;
138                 $previtem = ($packages_small{$data{'package'}}{$suite}{'any'}
139                     ||= $pkgitem);
140                 $packages_small{$data{'package'}}{$suite}{'any'} = $pkgitem
141                     if version_cmp($data{'version'}, (split /\s/o, $previtem)[6]) > 0;
142                 $sources_packages{$src} .=
143                         "$archive $suite $data{'package'} $data{'version'} $data{'architecture'}\000";
144                 $data{archive} = $archive;
145                 while (my ($key, $value) = each (%data)) {
146                         next if $key eq 'package' or $key eq 'version' or $key eq 'architecture'
147                                 or $key eq 'maintainer';
148                         print STDERR "WARN: $key ($suite/$archive/$data{package}/$data{architecture}\n" unless defined $value;
149                         $data .= "$key\00$value\00";
150                 }
151                 $data =~ s/.$//s;
152                 $packages_all_db{"$data{'package'} $data{'architecture'} $data{'version'}"}
153                         = $data;
154         }
155     }
156
157     open NAMES, '>', "$DBDIR/package_names_$suite.txt.new"
158         or die "Error creating package names list: $!";
159     foreach (sort keys %package_names_suite) {
160         print NAMES "$_\n";
161     }
162     close NAMES;
163
164     untie %packages_all_db;
165 }
166
167 print "Writing databases...\n";
168 my %packages_small_db;
169 tie %packages_small_db, "DB_File", "$DBDIR/packages_small.db.new",
170         O_RDWR|O_CREAT, 0666, $DB_BTREE
171         or die "Error creating DB: $!";
172 while (my ($pkg, $v) = each(%packages_small)) {
173         my ($res1, $res2, $res3) = ("", "", "");
174         while (my ($suite, $v2) = each %$v) {
175             $res2 .= $v2->{'any'};
176             while (my ($arch, $v3) = each %$v2) {
177                 next if $arch eq 'any' or $v3 eq $v2->{'any'};
178                 $res3 .= $v3;
179             }
180         }
181         
182         if (exists $virtual_packages{$pkg}) {
183                 while (my ($suite, $v2) = each %{$virtual_packages{$pkg}}) {
184                         $res1 .= "$suite\01".(join ' ', keys %$v2)."\01";
185                 }
186         } else {
187                 $res1 .= "-\01-\01";
188         }
189         $res1 =~ s/.$/\0/s;
190
191         my $res = "$res1$res2$res3";
192         $res =~ s/.$//s;
193         $packages_small_db{$pkg} = $res;
194 }
195 untie %packages_small_db;
196
197 my %sources_packages_db;
198 tie %sources_packages_db, "DB_File", "$DBDIR/sources_packages.db.new",
199         O_RDWR|O_CREAT, 0666, $DB_BTREE
200         or die "Error creating DB: $!";
201 while (my ($k, $v) = each(%sources_packages)) {
202         $v =~ s/.$//s;
203         $sources_packages_db{$k} = $v;
204 }
205 untie %sources_packages_db;
206
207 my %packages_descriptions_db;
208 tie %packages_descriptions_db, "DB_File", "$DBDIR/packages_descriptions.db.new",
209         O_RDWR|O_CREAT, 0666, $DB_BTREE
210         or die "Error creating DB: $!";
211 while (my ($k, $v) = each(%packages_descriptions)) {
212         $packages_descriptions_db{$k} = $v;
213 }
214 untie %packages_descriptions_db;
215
216 my %descriptions_packages_db;
217 tie %descriptions_packages_db, "DB_File", "$DBDIR/descriptions_packages.db.new",
218         O_RDWR|O_CREAT, 0666, $DB_BTREE
219         or die "Error creating DB: $!";
220 while (my ($k, $v) = each(%descriptions_packages)) {
221         $v =~ s/.$//s;
222         $descriptions_packages_db{$k} = $v;
223 }
224 untie %descriptions_packages_db;
225
226 my %descriptions_db;
227 tie %descriptions_db, "DB_File", "$DBDIR/descriptions.db.new",
228         O_RDWR|O_CREAT, 0666, $DB_BTREE
229         or die "Error creating DB: $!";
230 open DESCR, ">", "$DBDIR/descriptions.txt" or die "Error creating descriptions textfile";
231 for (my $i=1; $i<= $#descriptions; $i++) {
232         my $plain_description = $descriptions[$i];
233 # WARNING: This needs to correspond with what happens in
234 # Packages/Search.pm:do_fulltext_search
235         $plain_description =~ tr [A-Z] [a-z];
236         # ensure one space on both ends
237         $plain_description = " $plain_description ";
238         $plain_description =~ s/[(),.-]+//og;
239         $plain_description =~ s#[^a-z0-9_/+]+# #og;
240         print DESCR "$plain_description\n";
241         $descriptions_db{$i} = $descriptions[$i];
242 }
243 close DESCR;
244 untie %descriptions_db;
245
246 # package names stuff:
247 for my $pkg (keys %package_names) {
248         for (my $i=0;$i<length($pkg)-1;$i++) {
249                 my $before = substr($pkg, 0, $i);
250                 my $after = substr($pkg, $i);
251                 $before = "^" if $before eq ""; # otherwise split doesn't work properly
252                 $package_postfixes{$after} .= "$before\0";
253         }
254 }
255 my %package_postfixes_db;
256 tie %package_postfixes_db, "DB_File", "$DBDIR/package_postfixes.db.new",
257         O_RDWR|O_CREAT, 0666, $DB_BTREE
258         or die "Error creating DB: $!";
259 while (my ($k, $v) = each(%package_postfixes)) {
260         $v =~ s/.$//s;
261         my $nr = $v;
262         $nr =~ s/[^\000]//g;
263         $nr = length($nr) + 1; # < number of hits
264         if ($nr > $MAX_PACKAGE_POSTFIXES) {
265                 $v = "\001" . $nr;
266         }
267         $package_postfixes_db{$k} = $v;
268 }
269 untie %package_postfixes_db;
270
271 store \%sections, "$DBDIR/sections.info";
272 store \%subsections, "$DBDIR/subsections.info";
273 store \%priorities, "$DBDIR/priorities.info";
274
275 rename("$DBDIR/packages_small.db.new", "$DBDIR/packages_small.db");
276 rename("$DBDIR/sources_packages.db.new", "$DBDIR/sources_packages.db");
277 for my $suite (@SUITES) {
278         rename("$DBDIR/packages_all_$suite.db.new",
279                "$DBDIR/packages_all_$suite.db");
280         rename("$DBDIR/package_names_$suite.txt.new",
281                "$DBDIR/package_names_$suite.txt");
282 }
283 rename("$DBDIR/packages_descriptions.db.new",
284        "$DBDIR/packages_descriptions.db");
285 rename("$DBDIR/descriptions_packages.db.new",
286        "$DBDIR/descriptions_packages.db");
287 rename("$DBDIR/descriptions.txt.new", "$DBDIR/descriptions.txt");
288 rename("$DBDIR/descriptions.db.new", "$DBDIR/descriptions.db");
289 rename("$DBDIR/package_postfixes.db.new", "$DBDIR/package_postfixes.db");