2 # Convert Packages.gz files into Sleepycat db files for efficient usage of
5 # Copyright (C) 2006 Jeroen van Wolffelaar <jeroen@wolffelaar.nl>
6 # Copyright (C) 2006-2007 Frank Lichtenheld <frank@lichtenheld.de>
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.
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.
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.
28 my $what = $ARGV[0] ? "non-free" : "*";
29 # max. distinct results for a given package postfix
30 my $MAX_PACKAGE_POSTFIXES = 100;
37 use Lingua::Stem v0.82;
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 = ();
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" );
62 for my $suite (@SUITES) {
63 my %package_names_suite = ();
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: $!";
69 for my $archive (@ARCHIVES) {
70 print "Reading $archive/$suite...\n";
71 my $suite_dir = "$TOPDIR/archive/$archive/$suite";
73 print "\tseems not to exist, skipping...\n";
76 open PKG, "zcat $suite_dir/$what/binary-*/Packages.gz $suite_dir/$what/debian-installer/binary-*/Packages.gz|";
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];
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';
96 if ($data{'provides'}) {
97 foreach (split /\s*,\s*/, $data{'provides'}) {
98 $virtual_packages{$_}{$suite}{$data{'package'}}++;
99 $packages_small{$_} ||= {};
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
110 $data{'source'} = $src;
112 # expand tags like devel::{lang:c,lang:c++}
113 if ($data{'tag'} && $data{'tag'} =~ /\{/) {
114 my @complete_tags = split(/, /, $data{'tag'});
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}::$_";
124 warn "pkg=$data{package} tags=$data{tag}\n" unless $facet && $tag;
125 push @tags, "${facet}::$tag";
128 my $old = $data{tag};
129 $data{'tag'} = join ", ", @tags;
132 # we add some additional data here
133 my $descr = "$data{'description'}\000$data{'package'}\000"
135 my $sdescr = $data{'description'};
136 $sdescr =~ s/\n.*//os;
138 if (exists($descriptions{$descr})) {
139 $did = $descriptions{$descr};
141 $did = 1 + $#descriptions;
142 $descriptions[$did] = $descr;
143 $descriptions{$descr} = $did;
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";
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;
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'}}
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'}
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";
182 $packages_all_db{"$data{'package'} $data{'architecture'} $data{'version'}"}
187 open NAMES, '>', "$DBDIR/package_names_$suite.txt.new"
188 or die "Error creating package names list: $!";
189 foreach (sort keys %package_names_suite) {
194 untie %packages_all_db;
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'};
212 if (exists $virtual_packages{$pkg}) {
213 while (my ($suite, $v2) = each %{$virtual_packages{$pkg}}) {
214 $res1 .= "$suite\01".(join ' ', keys %$v2)."\01";
221 my $res = "$res1$res2$res3";
223 $packages_small_db{$pkg} = $res;
225 untie %packages_small_db;
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)) {
233 $sources_packages_db{$k} = $v;
235 untie %sources_packages_db;
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;
244 untie %packages_descriptions_db;
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)) {
252 $descriptions_packages_db{$k} = $v;
254 untie %descriptions_packages_db;
256 my $stemmer = Lingua::Stem->new();
257 $stemmer->stem_caching({ -level => 2 });
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";
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;
280 my @words = split /\s+/, $only_desc;
281 unshift @words, $pkg;
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";
289 # package with prefix
290 if ($doc->add_term("P$pkg")) {
291 warn "can't add term P$pkg: $!\n";
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";
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";
309 foreach my $t (split m/, /, $tags) {
310 if ($doc->add_term($t)) {
311 warn "can't add term $t: $!\n";
316 $xapian_db->add_document($doc)
317 or warn "failed to add document: $i\n";
321 $descriptions_db{$i} = $orig_desc;
323 untie %descriptions_db;
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";
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)) {
342 my $nr = ($v =~ tr/\000/\000/) + 1;
343 if ($nr > $MAX_PACKAGE_POSTFIXES) {
344 $v = ($v =~ /\^/) ? "^\001" . $nr
347 $package_postfixes_db{$k} = $v;
349 untie %package_postfixes_db;
351 store \%sections, "$DBDIR/sections.info";
352 store \%subsections, "$DBDIR/subsections.info";
353 store \%priorities, "$DBDIR/priorities.info";
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");
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");