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 warn "pkg=$data{package} tags=$data{tag}\n" unless $facet && $tag;
119 foreach (split( /,/, $tag )) {
120 push @tags, "${facet}::$_";
123 warn "pkg=$data{package} tags=$data{tag}\n" unless $facet && $tag;
124 push @tags, "${facet}::$tag";
127 my $old = $data{tag};
128 $data{'tag'} = join ", ", @tags;
131 # we add some additional data here
132 my $descr = "$data{'description'}\000$data{'package'}\000"
134 my $sdescr = $data{'description'};
135 $sdescr =~ s/\n.*//os;
137 if (exists($descriptions{$descr})) {
138 $did = $descriptions{$descr};
140 $did = 1 + $#descriptions;
141 $descriptions[$did] = $descr;
142 $descriptions{$descr} = $did;
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";
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;
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'}}
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'}
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";
181 $packages_all_db{"$data{'package'} $data{'architecture'} $data{'version'}"}
186 open NAMES, '>', "$DBDIR/package_names_$suite.txt.new"
187 or die "Error creating package names list: $!";
188 foreach (sort keys %package_names_suite) {
193 untie %packages_all_db;
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'};
211 if (exists $virtual_packages{$pkg}) {
212 while (my ($suite, $v2) = each %{$virtual_packages{$pkg}}) {
213 $res1 .= "$suite\01".(join ' ', keys %$v2)."\01";
220 my $res = "$res1$res2$res3";
222 $packages_small_db{$pkg} = $res;
224 untie %packages_small_db;
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)) {
232 $sources_packages_db{$k} = $v;
234 untie %sources_packages_db;
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;
243 untie %packages_descriptions_db;
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)) {
251 $descriptions_packages_db{$k} = $v;
253 untie %descriptions_packages_db;
255 my $stemmer = Lingua::Stem->new();
256 $stemmer->stem_caching({ -level => 2 });
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";
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;
279 my @words = split /\s+/, $only_desc;
280 unshift @words, $pkg;
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";
288 # package with prefix
289 if ($doc->add_term("P$pkg")) {
290 warn "can't add term P$pkg: $!\n";
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";
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";
308 foreach my $t (split m/, /, $tags) {
309 if ($doc->add_term($t)) {
310 warn "can't add term $t: $!\n";
315 $xapian_db->add_document($doc)
316 or warn "failed to add document: $i\n";
320 $descriptions_db{$i} = $orig_desc;
322 untie %descriptions_db;
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";
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)) {
341 my $nr = ($v =~ tr/\000/\000/) + 1;
342 if ($nr > $MAX_PACKAGE_POSTFIXES) {
343 $v = ($v =~ /\^/) ? "^\001" . $nr
346 $package_postfixes_db{$k} = $v;
348 untie %package_postfixes_db;
350 store \%sections, "$DBDIR/sections.info";
351 store \%subsections, "$DBDIR/subsections.info";
352 store \%priorities, "$DBDIR/priorities.info";
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");
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");