]> git.deb.at Git - deb/packages.git/blob - bin/parse-packages
Merge branch 'master' into debian-master
[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 # Copyright (C) 2006  Jeroen van Wolffelaar <jeroen@wolffelaar.nl>
6 # Copyright (C) 2006-2007 Frank Lichtenheld <frank@lichtenheld.de>
7 #
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 & debports, any non-redundancy is
91                 # usually a bug anyway
92                 next if ($archive eq 'amd64' or $archive eq 'debports')
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
111                 # expand tags like devel::{lang:c,lang:c++}
112                 if ($data{'tag'} && $data{'tag'} =~ /\{/) {
113                     my @complete_tags = split(/, /, $data{'tag'});
114                     my @tags;
115                     foreach (@complete_tags) {
116                         my ($facet, $tag) = split( /::/, $_, 2);
117                         if ($tag =~ s/^\{(.+)\}$/$1/) {
118                             warn "pkg=$data{package} tags=$data{tag}\n" unless $facet && $tag;
119                             foreach (split( /,/, $tag )) {
120                                 push @tags, "${facet}::$_";
121                             }
122                         } else {
123                             warn "pkg=$data{package} tags=$data{tag}\n" unless $facet && $tag;
124                             push @tags, "${facet}::$tag";
125                         }
126                     }
127                     my $old = $data{tag};
128                     $data{'tag'} = join ", ", @tags;
129                 }
130
131                 # we add some additional data here
132                 my $descr = "$data{'description'}\000$data{'package'}\000"
133                     .($data{'tag'}||'');
134                 my $sdescr = $data{'description'};
135                 $sdescr =~ s/\n.*//os;
136                 my $did = undef;
137                 if (exists($descriptions{$descr})) {
138                         $did  = $descriptions{$descr};
139                 } else {
140                         $did = 1 + $#descriptions;
141                         $descriptions[$did] = $descr;
142                         $descriptions{$descr} = $did;
143                 }
144                 $data{'description-md5'} = Digest::MD5::md5_hex($data{'description'}, "\n");
145                 $data{'description'} = $did;
146                 $packages_descriptions{"$data{'package'} $data{'version'} $data{'architecture'}"} = $did;
147                 $descriptions_packages{$did} .=
148                         "$data{'package'} $data{'version'} $data{'architecture'}\000";
149
150                 my $section = 'main';
151                 my $subsection = $data{section} || '-';
152                 if ($data{section} && ($data{section} =~ m=/=o)) {
153                     ($section, $subsection) = split m=/=o, $data{section}, 2;
154                 }
155                 $data{'section'} = $section;
156                 $data{'subsection'} = $subsection;
157                 $data{'priority'} ||= '-';
158                 $sections{$suite}{$section}++;
159                 $subsections{$suite}{$subsection}++;
160                 $priorities{$suite}{$data{priority}}++;
161                 my $pkgitem = "$archive $suite $data{'architecture'} ".
162                         "$section $subsection $data{'priority'} $data{'version'} $data{'description-md5'} $sdescr\0";
163                 my $previtem = ($packages_small{$data{'package'}}{$suite}{$data{'architecture'}}
164                     ||= $pkgitem);
165                 $packages_small{$data{'package'}}{$suite}{$data{'architecture'}} = $pkgitem
166                     if version_cmp($data{'version'}, (split /\s/o, $previtem)[6]) > 0;
167                 $previtem = ($packages_small{$data{'package'}}{$suite}{'any'}
168                     ||= $pkgitem);
169                 $packages_small{$data{'package'}}{$suite}{'any'} = $pkgitem
170                     if version_cmp($data{'version'}, (split /\s/o, $previtem)[6]) > 0;
171                 $sources_packages{$src} .=
172                         "$archive $suite $data{'package'} $data{'version'} $data{'architecture'}\000";
173                 $data{archive} = $archive;
174                 while (my ($key, $value) = each (%data)) {
175                         next if $key eq 'package' or $key eq 'version' or $key eq 'architecture'
176                                 or $key eq 'maintainer';
177                         print STDERR "WARN: $key ($suite/$archive/$data{package}/$data{architecture}\n" unless defined $value;
178                         $data .= "$key\00$value\00";
179                 }
180                 $data =~ s/.$//s;
181                 $packages_all_db{"$data{'package'} $data{'architecture'} $data{'version'}"}
182                         = $data;
183         }
184     }
185
186     open NAMES, '>', "$DBDIR/package_names_$suite.txt.new"
187         or die "Error creating package names list: $!";
188     foreach (sort keys %package_names_suite) {
189         print NAMES "$_\n";
190     }
191     close NAMES;
192
193     untie %packages_all_db;
194 }
195
196 print "Writing databases...\n";
197 my %packages_small_db;
198 tie %packages_small_db, "DB_File", "$DBDIR/packages_small.db.new",
199         O_RDWR|O_CREAT, 0666, $DB_BTREE
200         or die "Error creating DB: $!";
201 while (my ($pkg, $v) = each(%packages_small)) {
202         my ($res1, $res2, $res3) = ("", "", "");
203         while (my ($suite, $v2) = each %$v) {
204             $res2 .= $v2->{'any'};
205             while (my ($arch, $v3) = each %$v2) {
206                 next if $arch eq 'any' or $v3 eq $v2->{'any'};
207                 $res3 .= $v3;
208             }
209         }
210
211         if (exists $virtual_packages{$pkg}) {
212                 while (my ($suite, $v2) = each %{$virtual_packages{$pkg}}) {
213                         $res1 .= "$suite\01".(join ' ', keys %$v2)."\01";
214                 }
215         } else {
216                 $res1 .= "-\01-\01";
217         }
218         $res1 =~ s/.$/\0/s;
219
220         my $res = "$res1$res2$res3";
221         $res =~ s/.$//s;
222         $packages_small_db{$pkg} = $res;
223 }
224 untie %packages_small_db;
225
226 my %sources_packages_db;
227 tie %sources_packages_db, "DB_File", "$DBDIR/sources_packages.db.new",
228         O_RDWR|O_CREAT, 0666, $DB_BTREE
229         or die "Error creating DB: $!";
230 while (my ($k, $v) = each(%sources_packages)) {
231         $v =~ s/.$//s;
232         $sources_packages_db{$k} = $v;
233 }
234 untie %sources_packages_db;
235
236 my %packages_descriptions_db;
237 tie %packages_descriptions_db, "DB_File", "$DBDIR/packages_descriptions.db.new",
238         O_RDWR|O_CREAT, 0666, $DB_BTREE
239         or die "Error creating DB: $!";
240 while (my ($k, $v) = each(%packages_descriptions)) {
241         $packages_descriptions_db{$k} = $v;
242 }
243 untie %packages_descriptions_db;
244
245 my %descriptions_packages_db;
246 tie %descriptions_packages_db, "DB_File", "$DBDIR/descriptions_packages.db.new",
247         O_RDWR|O_CREAT, 0666, $DB_BTREE
248         or die "Error creating DB: $!";
249 while (my ($k, $v) = each(%descriptions_packages)) {
250         $v =~ s/.$//s;
251         $descriptions_packages_db{$k} = $v;
252 }
253 untie %descriptions_packages_db;
254
255 my $stemmer = Lingua::Stem->new();
256 $stemmer->stem_caching({ -level => 2 });
257 my $xapian_db;
258 eval {
259     $xapian_db = Search::Xapian::WritableDatabase->new("$DBDIR/xapian.new",
260                                                        Search::Xapian::DB_CREATE_OR_OPEN)
261         or die "can't create write-able db object: $!\n";
262 };
263 die $@ if $@;
264 my %descriptions_db;
265 tie %descriptions_db, "DB_File", "$DBDIR/descriptions.db.new",
266         O_RDWR|O_CREAT, 0666, $DB_BTREE
267         or die "Error creating DB: $!";
268 print "Index $#descriptions descriptions\n";
269 for (my $i=1; $i<= $#descriptions; $i++) {
270         # strip away additional data
271         my ($only_desc, $pkg, $tags) = split /\000/o, $descriptions[$i], 3;
272         my $orig_desc = $only_desc;
273 # WARNING: This needs to correspond with what happens in
274 # Packages/Search.pm:do_xapian_search
275         $only_desc =~ s#[^\w/+]+# #og;
276
277         #XAPIAN
278         eval {
279             my @words = split /\s+/, $only_desc;
280             unshift @words, $pkg;
281
282             my $doc = Search::Xapian::Document->new()
283                 or die "can't create doc object for $i: $!\n";
284             if ($doc->set_data($i)){
285                 warn "can't set_data in doc object for $i: $!\n";
286             }
287
288             # package with prefix
289             if ($doc->add_term("P$pkg")) {
290                 warn "can't add term P$pkg: $!\n";
291             }
292             # description, unstemmed with positional info
293             for my $j (0 .. (@words-1)) {
294                 next if $words[$j] =~ /^\s*$/o;
295                 if ($doc->add_posting($words[$j], $j)) {
296                     warn "can't add posting $words[$j] at $j: $!\n";
297                 }
298             }
299             # description, stemmed
300             my $stem_words = $stemmer->stem( \@words );
301             foreach my $w (@$stem_words) {
302                 next if $w =~ /^\s*$/o;
303                 if ($doc->add_term($w)) {
304                     warn "can't add term $w: $!\n";
305                 }
306             }
307             if ($tags) {
308                 foreach my $t (split m/, /, $tags) {
309                     if ($doc->add_term($t)) {
310                         warn "can't add term $t: $!\n";
311                     }
312                 }
313             }
314
315             $xapian_db->add_document($doc)
316                 or warn "failed to add document: $i\n";
317         };
318         die $@ if $@;
319
320         $descriptions_db{$i} = $orig_desc;
321 }
322 untie %descriptions_db;
323 $xapian_db->flush;
324 undef $xapian_db;
325
326 # package names stuff:
327 for my $pkg (keys %package_names) {
328         for (my $i=0;$i<length($pkg)-1;$i++) {
329                 my $before = substr($pkg, 0, $i);
330                 my $after = substr($pkg, $i);
331                 $before = "^" if $before eq ""; # otherwise split doesn't work properly
332                 $package_postfixes{$after} .= "$before\0";
333         }
334 }
335 my %package_postfixes_db;
336 tie %package_postfixes_db, "DB_File", "$DBDIR/package_postfixes.db.new",
337         O_RDWR|O_CREAT, 0666, $DB_BTREE
338         or die "Error creating DB: $!";
339 while (my ($k, $v) = each(%package_postfixes)) {
340         $v =~ s/.$//s;
341         my $nr = ($v =~ tr/\000/\000/) + 1;
342         if ($nr > $MAX_PACKAGE_POSTFIXES) {
343             $v = ($v =~ /\^/) ? "^\001" . $nr
344                 : "\001" . $nr;
345         }
346         $package_postfixes_db{$k} = $v;
347 }
348 untie %package_postfixes_db;
349
350 store \%sections, "$DBDIR/sections.info";
351 store \%subsections, "$DBDIR/subsections.info";
352 store \%priorities, "$DBDIR/priorities.info";
353
354 rename("$DBDIR/packages_small.db.new", "$DBDIR/packages_small.db");
355 rename("$DBDIR/sources_packages.db.new", "$DBDIR/sources_packages.db");
356 for my $suite (@SUITES) {
357         rename("$DBDIR/packages_all_$suite.db.new",
358                "$DBDIR/packages_all_$suite.db");
359         rename("$DBDIR/package_names_$suite.txt.new",
360                "$DBDIR/package_names_$suite.txt");
361 }
362 rename("$DBDIR/xapian", "$DBDIR/xapian.old");
363 rename("$DBDIR/xapian.new","$DBDIR/xapian");
364 rmtree("$DBDIR/xapian.old");
365 rename("$DBDIR/packages_descriptions.db.new",
366        "$DBDIR/packages_descriptions.db");
367 rename("$DBDIR/descriptions_packages.db.new",
368        "$DBDIR/descriptions_packages.db");
369 rename("$DBDIR/descriptions.db.new", "$DBDIR/descriptions.db");
370 rename("$DBDIR/package_postfixes.db.new", "$DBDIR/package_postfixes.db");