Remove obsolete descriptions.txt previously used for fulltext search
[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 Digest::MD5;
36 use Deb::Versions;
37 use Lingua::Stem v0.82;
38 use Search::Xapian;
39 use Packages::Config qw( $TOPDIR $DBDIR @ARCHIVES @SUITES );
40 &Packages::Config::init( './' );
41 my %packages_small = ();
42 my %virtual_packages = ();
43 my %package_names = ();
44 my %package_postfixes = ();
45 my %sources_packages = ();
46 my %descriptions = ();
47 my @descriptions = ("we count lines one-based\000");
48 my %packages_descriptions = ();
49 my %descriptions_packages = ();
50
51 my %sections = ();
52 my %subsections = ();
53 my %priorities = ();
54
55 $/ = "";
56
57 -d $DBDIR || mkpath( $DBDIR );
58 -d "$DBDIR/xapian.new" && rmtree("$DBDIR/xapian.new");
59 -d "$DBDIR/xapian.old" && rmtree("$DBDIR/xapian.old");
60 mkpath( "$DBDIR/xapian.new" );
61
62 for my $suite (@SUITES) {
63     my %package_names_suite = ();
64     my %packages_all_db;
65     tie %packages_all_db, "DB_File", "$DBDIR/packages_all_$suite.db.new",
66             O_RDWR|O_CREAT, 0666, $DB_BTREE
67             or die "Error creating DB: $!";
68
69     for my $archive (@ARCHIVES) {
70         print "Reading $archive/$suite...\n";
71         if (!-d "$TOPDIR/archive/$archive/$suite/") {
72                 print "\tseems not to exist, skipping...\n";
73                 next;
74         }
75         open PKG, "zcat $TOPDIR/archive/$archive/$suite/$what/{,debian-installer/}binary-*/Packages.gz|";
76         while (<PKG>) {
77                 next if /^\s*$/;
78                 my $data = "";
79                 my %data = ();
80                 chomp;
81                 s/\n /\377/g;
82                 while (/^(\S+):\s*(.*)\s*$/mg) {
83                         my ($key, $value) = ($1, $2);
84                         $value =~ s/\377/\n /g;
85                         $key =~ tr [A-Z] [a-z];
86                         $data{$key} = $value;
87                 }
88                 # Skip double package
89                 next if exists($packages_all_db{"$data{'package'} $data{'architecture'} $data{'version'}"});
90                 # Skip arch:all for amd64 & gnuab, any non-redundancy is
91                 # usually a bug anyway
92                 next if ($archive eq 'amd64' or $archive eq 'gnuab')
93                     and $data{architecture} eq 'all';
94
95                 if ($data{'provides'}) {
96                     foreach (split /\s*,\s*/, $data{'provides'}) {
97                         $virtual_packages{$_}{$suite}{$data{'package'}}++;
98                         $packages_small{$_} ||= {};
99                     }
100                 }
101                 $package_names{$data{'package'}} = 1;
102                 $package_names_suite{$data{'package'}} = 1;
103                 my $src = $data{'package'};
104                 my $src_version = '';
105                 if ($data{'source'}) {
106                         $src = $data{'source'};
107                         $src =~ s/\s+.*//o; # strip version info
108                 }
109                 $data{'source'} = $src;
110                 # we add some additional data here
111                 my $descr = "$data{'description'}\000$data{'package'}\000"
112                     .($data{'tag'}||'');
113                 my $sdescr = $data{'description'};
114                 $sdescr =~ s/\n.*//os;
115                 my $did = undef;
116                 if (exists($descriptions{$descr})) {
117                         $did  = $descriptions{$descr};
118                 } else {
119                         $did = 1 + $#descriptions;
120                         $descriptions[$did] = $descr;
121                         $descriptions{$descr} = $did;
122                 }
123                 $data{'description-md5'} = Digest::MD5::md5_hex($data{'description'}, "\n");
124                 $data{'description'} = $did;
125                 $packages_descriptions{"$data{'package'} $data{'version'} $data{'architecture'}"} = $did;
126                 $descriptions_packages{$did} .=
127                         "$data{'package'} $data{'version'} $data{'architecture'}\000";
128
129                 my $section = 'main';
130                 my $subsection = $data{section} || '-';
131                 if ($data{section} && ($data{section} =~ m=/=o)) {
132                     ($section, $subsection) = split m=/=o, $data{section}, 2;
133                     ($subsection, $section) = split m=/=o, $data{section}, 2
134                         if $section eq 'non-US';
135                 }
136                 $data{'section'} = $section;
137                 $data{'subsection'} = $subsection;
138                 $data{'priority'} ||= '-';
139                 $sections{$suite}{$section}++;
140                 $subsections{$suite}{$subsection}++;
141                 $priorities{$suite}{$data{priority}}++;
142                 my $pkgitem = "$archive $suite $data{'architecture'} ".
143                         "$section $subsection $data{'priority'} $data{'version'} $sdescr\0";
144                 my $previtem = ($packages_small{$data{'package'}}{$suite}{$data{'architecture'}}
145                     ||= $pkgitem);
146                 $packages_small{$data{'package'}}{$suite}{$data{'architecture'}} = $pkgitem
147                     if version_cmp($data{'version'}, (split /\s/o, $previtem)[6]) > 0;
148                 $previtem = ($packages_small{$data{'package'}}{$suite}{'any'}
149                     ||= $pkgitem);
150                 $packages_small{$data{'package'}}{$suite}{'any'} = $pkgitem
151                     if version_cmp($data{'version'}, (split /\s/o, $previtem)[6]) > 0;
152                 $sources_packages{$src} .=
153                         "$archive $suite $data{'package'} $data{'version'} $data{'architecture'}\000";
154                 $data{archive} = $archive;
155                 while (my ($key, $value) = each (%data)) {
156                         next if $key eq 'package' or $key eq 'version' or $key eq 'architecture'
157                                 or $key eq 'maintainer';
158                         print STDERR "WARN: $key ($suite/$archive/$data{package}/$data{architecture}\n" unless defined $value;
159                         $data .= "$key\00$value\00";
160                 }
161                 $data =~ s/.$//s;
162                 $packages_all_db{"$data{'package'} $data{'architecture'} $data{'version'}"}
163                         = $data;
164         }
165     }
166
167     open NAMES, '>', "$DBDIR/package_names_$suite.txt.new"
168         or die "Error creating package names list: $!";
169     foreach (sort keys %package_names_suite) {
170         print NAMES "$_\n";
171     }
172     close NAMES;
173
174     untie %packages_all_db;
175 }
176
177 print "Writing databases...\n";
178 my %packages_small_db;
179 tie %packages_small_db, "DB_File", "$DBDIR/packages_small.db.new",
180         O_RDWR|O_CREAT, 0666, $DB_BTREE
181         or die "Error creating DB: $!";
182 while (my ($pkg, $v) = each(%packages_small)) {
183         my ($res1, $res2, $res3) = ("", "", "");
184         while (my ($suite, $v2) = each %$v) {
185             $res2 .= $v2->{'any'};
186             while (my ($arch, $v3) = each %$v2) {
187                 next if $arch eq 'any' or $v3 eq $v2->{'any'};
188                 $res3 .= $v3;
189             }
190         }
191
192         if (exists $virtual_packages{$pkg}) {
193                 while (my ($suite, $v2) = each %{$virtual_packages{$pkg}}) {
194                         $res1 .= "$suite\01".(join ' ', keys %$v2)."\01";
195                 }
196         } else {
197                 $res1 .= "-\01-\01";
198         }
199         $res1 =~ s/.$/\0/s;
200
201         my $res = "$res1$res2$res3";
202         $res =~ s/.$//s;
203         $packages_small_db{$pkg} = $res;
204 }
205 untie %packages_small_db;
206
207 my %sources_packages_db;
208 tie %sources_packages_db, "DB_File", "$DBDIR/sources_packages.db.new",
209         O_RDWR|O_CREAT, 0666, $DB_BTREE
210         or die "Error creating DB: $!";
211 while (my ($k, $v) = each(%sources_packages)) {
212         $v =~ s/.$//s;
213         $sources_packages_db{$k} = $v;
214 }
215 untie %sources_packages_db;
216
217 my %packages_descriptions_db;
218 tie %packages_descriptions_db, "DB_File", "$DBDIR/packages_descriptions.db.new",
219         O_RDWR|O_CREAT, 0666, $DB_BTREE
220         or die "Error creating DB: $!";
221 while (my ($k, $v) = each(%packages_descriptions)) {
222         $packages_descriptions_db{$k} = $v;
223 }
224 untie %packages_descriptions_db;
225
226 my %descriptions_packages_db;
227 tie %descriptions_packages_db, "DB_File", "$DBDIR/descriptions_packages.db.new",
228         O_RDWR|O_CREAT, 0666, $DB_BTREE
229         or die "Error creating DB: $!";
230 while (my ($k, $v) = each(%descriptions_packages)) {
231         $v =~ s/.$//s;
232         $descriptions_packages_db{$k} = $v;
233 }
234 untie %descriptions_packages_db;
235
236 my $stemmer = Lingua::Stem->new();
237 $stemmer->stem_caching({ -level => 2 });
238 my $xapian_db;
239 eval {
240     $xapian_db = Search::Xapian::WritableDatabase->new("$DBDIR/xapian.new",
241                                                        Search::Xapian::DB_CREATE_OR_OPEN)
242         or die "can't create write-able db object: $!\n";
243 };
244 die $@ if $@;
245 my %descriptions_db;
246 tie %descriptions_db, "DB_File", "$DBDIR/descriptions.db.new",
247         O_RDWR|O_CREAT, 0666, $DB_BTREE
248         or die "Error creating DB: $!";
249 print "Index $#descriptions descriptions\n";
250 for (my $i=1; $i<= $#descriptions; $i++) {
251         my $plain_description = $descriptions[$i];
252         # strip away additional data
253         my ($only_desc) = split /\000/o, $plain_description, 2;
254 # WARNING: This needs to correspond with what happens in
255 # Packages/Search.pm:do_fulltext_search
256         $plain_description =~ tr [A-Z] [a-z];
257         # ensure one space on both ends
258         $plain_description = " $plain_description ";
259         $plain_description =~ s/[(),.-]+//og;
260         $plain_description =~ s#[^a-z0-9_/+]+# #og;
261
262         #XAPIAN
263         eval {
264             my @words = split /\s+/, $plain_description;
265             my $stem_words = $stemmer->stem( \@words );
266             my $doc = Search::Xapian::Document->new()
267                 or die "can't create doc object for $i: $!\n";
268             if ($doc->set_data($i)){
269                 warn "can't set_data in doc object for $i: $!\n";
270             }
271             for my $j (0 .. (@$stem_words-1)) {
272                 next if $stem_words->[$j] =~ /^\s*$/o;
273                 if ($doc->add_posting($stem_words->[$j], $j)) {
274                     warn "can't add word $stem_words->[$j] $j: $!\n";
275                 }
276             }
277             $xapian_db->add_document($doc)
278                 or warn "failed to add document: $i\n";
279         };
280         die $@ if $@;
281
282         $descriptions_db{$i} = $only_desc;
283 }
284 untie %descriptions_db;
285 $xapian_db->flush;
286 undef $xapian_db;
287
288 # package names stuff:
289 for my $pkg (keys %package_names) {
290         for (my $i=0;$i<length($pkg)-1;$i++) {
291                 my $before = substr($pkg, 0, $i);
292                 my $after = substr($pkg, $i);
293                 $before = "^" if $before eq ""; # otherwise split doesn't work properly
294                 $package_postfixes{$after} .= "$before\0";
295         }
296 }
297 my %package_postfixes_db;
298 tie %package_postfixes_db, "DB_File", "$DBDIR/package_postfixes.db.new",
299         O_RDWR|O_CREAT, 0666, $DB_BTREE
300         or die "Error creating DB: $!";
301 while (my ($k, $v) = each(%package_postfixes)) {
302         $v =~ s/.$//s;
303         my $nr = $v;
304         $nr =~ s/[^\000]//g;
305         $nr = length($nr) + 1; # < number of hits
306         if ($nr > $MAX_PACKAGE_POSTFIXES) {
307                 $v = "\001" . $nr;
308         }
309         $package_postfixes_db{$k} = $v;
310 }
311 untie %package_postfixes_db;
312
313 store \%sections, "$DBDIR/sections.info";
314 store \%subsections, "$DBDIR/subsections.info";
315 store \%priorities, "$DBDIR/priorities.info";
316
317 rename("$DBDIR/packages_small.db.new", "$DBDIR/packages_small.db");
318 rename("$DBDIR/sources_packages.db.new", "$DBDIR/sources_packages.db");
319 for my $suite (@SUITES) {
320         rename("$DBDIR/packages_all_$suite.db.new",
321                "$DBDIR/packages_all_$suite.db");
322         rename("$DBDIR/package_names_$suite.txt.new",
323                "$DBDIR/package_names_$suite.txt");
324 }
325 rename("$DBDIR/xapian", "$DBDIR/xapian.old");
326 rename("$DBDIR/xapian.new","$DBDIR/xapian");
327 rmtree("$DBDIR/xapian.old");
328 rename("$DBDIR/packages_descriptions.db.new",
329        "$DBDIR/packages_descriptions.db");
330 rename("$DBDIR/descriptions_packages.db.new",
331        "$DBDIR/descriptions_packages.db");
332 rename("$DBDIR/descriptions.db.new", "$DBDIR/descriptions.db");
333 rename("$DBDIR/package_postfixes.db.new", "$DBDIR/package_postfixes.db");