]> git.deb.at Git - deb/packages.git/blob - bin/parse-packages
Create a directory suitable for mirroring the site
[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                             foreach (split( /,/, $tag )) {
119                                 push @tags, "${facet}::$_";
120                             }
121                         } else {
122                             push @tags, "${facet}::$tag";
123                         }
124                     }
125                     my $old = $data{tag};
126                     $data{'tag'} = join ", ", @tags;
127                 }
128
129                 # we add some additional data here
130                 my $descr = "$data{'description'}\000$data{'package'}\000"
131                     .($data{'tag'}||'');
132                 my $sdescr = $data{'description'};
133                 $sdescr =~ s/\n.*//os;
134                 my $did = undef;
135                 if (exists($descriptions{$descr})) {
136                         $did  = $descriptions{$descr};
137                 } else {
138                         $did = 1 + $#descriptions;
139                         $descriptions[$did] = $descr;
140                         $descriptions{$descr} = $did;
141                 }
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";
147
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;
152                 }
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'}}
162                     ||= $pkgitem);
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'}
166                     ||= $pkgitem);
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";
177                 }
178                 $data =~ s/.$//s;
179                 $packages_all_db{"$data{'package'} $data{'architecture'} $data{'version'}"}
180                         = $data;
181         }
182     }
183
184     open NAMES, '>', "$DBDIR/package_names_$suite.txt.new"
185         or die "Error creating package names list: $!";
186     foreach (sort keys %package_names_suite) {
187         print NAMES "$_\n";
188     }
189     close NAMES;
190
191     untie %packages_all_db;
192 }
193
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'};
205                 $res3 .= $v3;
206             }
207         }
208
209         if (exists $virtual_packages{$pkg}) {
210                 while (my ($suite, $v2) = each %{$virtual_packages{$pkg}}) {
211                         $res1 .= "$suite\01".(join ' ', keys %$v2)."\01";
212                 }
213         } else {
214                 $res1 .= "-\01-\01";
215         }
216         $res1 =~ s/.$/\0/s;
217
218         my $res = "$res1$res2$res3";
219         $res =~ s/.$//s;
220         $packages_small_db{$pkg} = $res;
221 }
222 untie %packages_small_db;
223
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)) {
229         $v =~ s/.$//s;
230         $sources_packages_db{$k} = $v;
231 }
232 untie %sources_packages_db;
233
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;
240 }
241 untie %packages_descriptions_db;
242
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)) {
248         $v =~ s/.$//s;
249         $descriptions_packages_db{$k} = $v;
250 }
251 untie %descriptions_packages_db;
252
253 my $stemmer = Lingua::Stem->new();
254 $stemmer->stem_caching({ -level => 2 });
255 my $xapian_db;
256 eval {
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";
260 };
261 die $@ if $@;
262 my %descriptions_db;
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;
274
275         #XAPIAN
276         eval {
277             my @words = split /\s+/, $only_desc;
278             unshift @words, $pkg;
279
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";
284             }
285
286             # package with prefix
287             if ($doc->add_term("P$pkg")) {
288                 warn "can't add term P$pkg: $!\n";
289             }
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";
295                 }
296             }
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";
303                 }
304             }
305             if ($tags) {
306                 foreach my $t (split m/, /, $tags) {
307                     if ($doc->add_term($t)) {
308                         warn "can't add term $t: $!\n";
309                     }
310                 }
311             }
312
313             $xapian_db->add_document($doc)
314                 or warn "failed to add document: $i\n";
315         };
316         die $@ if $@;
317
318         $descriptions_db{$i} = $orig_desc;
319 }
320 untie %descriptions_db;
321 $xapian_db->flush;
322 undef $xapian_db;
323
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";
331         }
332 }
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)) {
338         $v =~ s/.$//s;
339         my $nr = ($v =~ tr/\000/\000/) + 1;
340         if ($nr > $MAX_PACKAGE_POSTFIXES) {
341             $v = ($v =~ /\^/) ? "^\001" . $nr
342                 : "\001" . $nr;
343         }
344         $package_postfixes_db{$k} = $v;
345 }
346 untie %package_postfixes_db;
347
348 store \%sections, "$DBDIR/sections.info";
349 store \%subsections, "$DBDIR/subsections.info";
350 store \%priorities, "$DBDIR/priorities.info";
351
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");
359 }
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");