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 if (!-d "$TOPDIR/archive/$archive/$suite/") {
72 print "\tseems not to exist, skipping...\n";
75 open PKG, "zcat $TOPDIR/archive/$archive/$suite/$what/{,debian-installer/}binary-*/Packages.gz|";
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];
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';
95 if ($data{'provides'}) {
96 foreach (split /\s*,\s*/, $data{'provides'}) {
97 $virtual_packages{$_}{$suite}{$data{'package'}}++;
98 $packages_small{$_} ||= {};
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
109 $data{'source'} = $src;
111 # expand tags like devel::{lang:c,lang:c++}
112 if ($data{'tag'} && $data{'tag'} =~ /\{/) {
113 my @complete_tags = split(/, /, $data{'tag'});
115 foreach (@complete_tags) {
116 my ($facet, $tag) = split( /::/, $_, 2);
117 if ($tag =~ s/^\{(.+)\}$/$1/) {
118 foreach (split( /,/, $tag )) {
119 push @tags, "${facet}::$_";
122 push @tags, "${facet}::$tag";
125 my $old = $data{tag};
126 $data{'tag'} = join ", ", @tags;
129 # we add some additional data here
130 my $descr = "$data{'description'}\000$data{'package'}\000"
132 my $sdescr = $data{'description'};
133 $sdescr =~ s/\n.*//os;
135 if (exists($descriptions{$descr})) {
136 $did = $descriptions{$descr};
138 $did = 1 + $#descriptions;
139 $descriptions[$did] = $descr;
140 $descriptions{$descr} = $did;
142 $data{'description-md5'} = Digest::MD5::md5_hex($data{'description'}, "\n");
143 $data{'description'} = $did;
144 $packages_descriptions{"$data{'package'} $data{'version'} $data{'architecture'}"} = $did;
145 $descriptions_packages{$did} .=
146 "$data{'package'} $data{'version'} $data{'architecture'}\000";
148 my $section = 'main';
149 my $subsection = $data{section} || '-';
150 if ($data{section} && ($data{section} =~ m=/=o)) {
151 ($section, $subsection) = split m=/=o, $data{section}, 2;
153 $data{'section'} = $section;
154 $data{'subsection'} = $subsection;
155 $data{'priority'} ||= '-';
156 $sections{$suite}{$section}++;
157 $subsections{$suite}{$subsection}++;
158 $priorities{$suite}{$data{priority}}++;
159 my $pkgitem = "$archive $suite $data{'architecture'} ".
160 "$section $subsection $data{'priority'} $data{'version'} $data{'description-md5'} $sdescr\0";
161 my $previtem = ($packages_small{$data{'package'}}{$suite}{$data{'architecture'}}
163 $packages_small{$data{'package'}}{$suite}{$data{'architecture'}} = $pkgitem
164 if version_cmp($data{'version'}, (split /\s/o, $previtem)[6]) > 0;
165 $previtem = ($packages_small{$data{'package'}}{$suite}{'any'}
167 $packages_small{$data{'package'}}{$suite}{'any'} = $pkgitem
168 if version_cmp($data{'version'}, (split /\s/o, $previtem)[6]) > 0;
169 $sources_packages{$src} .=
170 "$archive $suite $data{'package'} $data{'version'} $data{'architecture'}\000";
171 $data{archive} = $archive;
172 while (my ($key, $value) = each (%data)) {
173 next if $key eq 'package' or $key eq 'version' or $key eq 'architecture'
174 or $key eq 'maintainer';
175 print STDERR "WARN: $key ($suite/$archive/$data{package}/$data{architecture}\n" unless defined $value;
176 $data .= "$key\00$value\00";
179 $packages_all_db{"$data{'package'} $data{'architecture'} $data{'version'}"}
184 open NAMES, '>', "$DBDIR/package_names_$suite.txt.new"
185 or die "Error creating package names list: $!";
186 foreach (sort keys %package_names_suite) {
191 untie %packages_all_db;
194 print "Writing databases...\n";
195 my %packages_small_db;
196 tie %packages_small_db, "DB_File", "$DBDIR/packages_small.db.new",
197 O_RDWR|O_CREAT, 0666, $DB_BTREE
198 or die "Error creating DB: $!";
199 while (my ($pkg, $v) = each(%packages_small)) {
200 my ($res1, $res2, $res3) = ("", "", "");
201 while (my ($suite, $v2) = each %$v) {
202 $res2 .= $v2->{'any'};
203 while (my ($arch, $v3) = each %$v2) {
204 next if $arch eq 'any' or $v3 eq $v2->{'any'};
209 if (exists $virtual_packages{$pkg}) {
210 while (my ($suite, $v2) = each %{$virtual_packages{$pkg}}) {
211 $res1 .= "$suite\01".(join ' ', keys %$v2)."\01";
218 my $res = "$res1$res2$res3";
220 $packages_small_db{$pkg} = $res;
222 untie %packages_small_db;
224 my %sources_packages_db;
225 tie %sources_packages_db, "DB_File", "$DBDIR/sources_packages.db.new",
226 O_RDWR|O_CREAT, 0666, $DB_BTREE
227 or die "Error creating DB: $!";
228 while (my ($k, $v) = each(%sources_packages)) {
230 $sources_packages_db{$k} = $v;
232 untie %sources_packages_db;
234 my %packages_descriptions_db;
235 tie %packages_descriptions_db, "DB_File", "$DBDIR/packages_descriptions.db.new",
236 O_RDWR|O_CREAT, 0666, $DB_BTREE
237 or die "Error creating DB: $!";
238 while (my ($k, $v) = each(%packages_descriptions)) {
239 $packages_descriptions_db{$k} = $v;
241 untie %packages_descriptions_db;
243 my %descriptions_packages_db;
244 tie %descriptions_packages_db, "DB_File", "$DBDIR/descriptions_packages.db.new",
245 O_RDWR|O_CREAT, 0666, $DB_BTREE
246 or die "Error creating DB: $!";
247 while (my ($k, $v) = each(%descriptions_packages)) {
249 $descriptions_packages_db{$k} = $v;
251 untie %descriptions_packages_db;
253 my $stemmer = Lingua::Stem->new();
254 $stemmer->stem_caching({ -level => 2 });
257 $xapian_db = Search::Xapian::WritableDatabase->new("$DBDIR/xapian.new",
258 Search::Xapian::DB_CREATE_OR_OPEN)
259 or die "can't create write-able db object: $!\n";
263 tie %descriptions_db, "DB_File", "$DBDIR/descriptions.db.new",
264 O_RDWR|O_CREAT, 0666, $DB_BTREE
265 or die "Error creating DB: $!";
266 print "Index $#descriptions descriptions\n";
267 for (my $i=1; $i<= $#descriptions; $i++) {
268 # strip away additional data
269 my ($only_desc, $pkg, $tags) = split /\000/o, $descriptions[$i], 3;
270 my $orig_desc = $only_desc;
271 # WARNING: This needs to correspond with what happens in
272 # Packages/Search.pm:do_xapian_search
273 $only_desc =~ s#[^\w/+]+# #og;
277 my @words = split /\s+/, $only_desc;
278 unshift @words, $pkg;
280 my $doc = Search::Xapian::Document->new()
281 or die "can't create doc object for $i: $!\n";
282 if ($doc->set_data($i)){
283 warn "can't set_data in doc object for $i: $!\n";
286 # package with prefix
287 if ($doc->add_term("P$pkg")) {
288 warn "can't add term P$pkg: $!\n";
290 # description, unstemmed with positional info
291 for my $j (0 .. (@words-1)) {
292 next if $words[$j] =~ /^\s*$/o;
293 if ($doc->add_posting($words[$j], $j)) {
294 warn "can't add posting $words[$j] at $j: $!\n";
297 # description, stemmed
298 my $stem_words = $stemmer->stem( \@words );
299 foreach my $w (@$stem_words) {
300 next if $w =~ /^\s*$/o;
301 if ($doc->add_term($w)) {
302 warn "can't add term $w: $!\n";
306 foreach my $t (split m/, /, $tags) {
307 if ($doc->add_term($t)) {
308 warn "can't add term $t: $!\n";
313 $xapian_db->add_document($doc)
314 or warn "failed to add document: $i\n";
318 $descriptions_db{$i} = $orig_desc;
320 untie %descriptions_db;
324 # package names stuff:
325 for my $pkg (keys %package_names) {
326 for (my $i=0;$i<length($pkg)-1;$i++) {
327 my $before = substr($pkg, 0, $i);
328 my $after = substr($pkg, $i);
329 $before = "^" if $before eq ""; # otherwise split doesn't work properly
330 $package_postfixes{$after} .= "$before\0";
333 my %package_postfixes_db;
334 tie %package_postfixes_db, "DB_File", "$DBDIR/package_postfixes.db.new",
335 O_RDWR|O_CREAT, 0666, $DB_BTREE
336 or die "Error creating DB: $!";
337 while (my ($k, $v) = each(%package_postfixes)) {
339 my $nr = ($v =~ tr/\000/\000/) + 1;
340 if ($nr > $MAX_PACKAGE_POSTFIXES) {
341 $v = ($v =~ /\^/) ? "^\001" . $nr
344 $package_postfixes_db{$k} = $v;
346 untie %package_postfixes_db;
348 store \%sections, "$DBDIR/sections.info";
349 store \%subsections, "$DBDIR/subsections.info";
350 store \%priorities, "$DBDIR/priorities.info";
352 rename("$DBDIR/packages_small.db.new", "$DBDIR/packages_small.db");
353 rename("$DBDIR/sources_packages.db.new", "$DBDIR/sources_packages.db");
354 for my $suite (@SUITES) {
355 rename("$DBDIR/packages_all_$suite.db.new",
356 "$DBDIR/packages_all_$suite.db");
357 rename("$DBDIR/package_names_$suite.txt.new",
358 "$DBDIR/package_names_$suite.txt");
360 rename("$DBDIR/xapian", "$DBDIR/xapian.old");
361 rename("$DBDIR/xapian.new","$DBDIR/xapian");
362 rmtree("$DBDIR/xapian.old");
363 rename("$DBDIR/packages_descriptions.db.new",
364 "$DBDIR/packages_descriptions.db");
365 rename("$DBDIR/descriptions_packages.db.new",
366 "$DBDIR/descriptions_packages.db");
367 rename("$DBDIR/descriptions.db.new", "$DBDIR/descriptions.db");
368 rename("$DBDIR/package_postfixes.db.new", "$DBDIR/package_postfixes.db");