]> git.deb.at Git - deb/packages.git/blob - bin/parse-contents
First version of parse-contents, for proto-implementing postfix searches
[deb/packages.git] / bin / parse-contents
1 #!/usr/bin/perl -w
2 # Convert Contents.gz files into Sleepycat db files for efficient usage of
3 # data
4 #
5 # $Id$
6 #
7 # Copyright (C) 2006  Jeroen van Wolffelaar <jeroen@wolffelaar.nl>
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
22 use strict;
23 use lib './lib';
24
25 use DB_File;
26 use Storable;
27 use Packages::Config qw( $TOPDIR $DBDIR @ARCHIVES @SUITES );
28 &Packages::Config::init( './' );
29 my %packages_contents = ();
30 my %file_reverse = ();
31
32 my @archives =( 'us'); #@ARCHIVES
33 my @suites = ('stable');#@SUITES
34
35 for my $archive (@archives) { for my $suite (@suites) {
36
37         print "Reading $archive/$suite/i386...\n";
38         open CONT, "zcat /org/ftp.debian.org/ftp/dists/stable/Contents-i386.gz|";
39         while (1) {$_ = <CONT>;last if /^FILE/mo;}
40         while (<CONT>) {
41                 my $data = "";
42                 my %data = ();
43                 chomp;
44                 print "Doing line $.\n" if $. % 10000 == 0;
45                 /^(\S+)\s+(\S+)/;
46                 my ($file, $value) = ($1, $2);
47                 $value =~ s#[^,/]+/##g;
48                 my @packages = split /,/, $value;
49                 for (@packages) {
50                         #$packages_contents{$_} .= "$_\0";
51                 }
52                 # Searches are case-insensitive
53                 $file =~ tr [A-Z] [a-z];
54
55                 $file_reverse{reverse $file} = join "\0", @packages;
56         }
57 }}
58
59 print "Writing databases...\n";
60 my %packages_contents_db;
61 tie %packages_contents_db, "DB_File", "packages_contents.db.new",
62         O_RDWR|O_CREAT, 0666, $DB_BTREE
63         or die "Error creating DB: $!";
64 while (my ($k, $v) = each(%packages_contents)) {
65         $v =~ s/.$//s;
66         $packages_contents_db{$k} = $v;
67 }
68 untie %packages_contents_db;
69
70 my %file_reverse_db;
71 tie %file_reverse_db, "DB_File", "$DBDIR/file_reverse.db.new",
72         O_RDWR|O_CREAT, 0666, $DB_BTREE
73         or die "Error creating DB: $!";
74 while (my ($x, $y) = each(%file_reverse)) {
75 #       $v =~ s/.$//s;
76 #       my $nr = $v;
77 #       $nr =~ s/[^\000]//g;
78 #       $nr = length($nr) + 1; # < number of hits
79 #       if ($nr > $MAX_file_reverse) {
80 #               $v = "\001" . $nr;
81 #       }
82         $file_reverse_db{$x} = $y;
83 }
84 untie %file_reverse_db;
85
86 #rename("packages_contents.db.new", "packages_contents.db");
87 rename("$DBDIR/file_reverse.db.new", "$DBDIR/file_reverse.db");