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