* Move coniguratio stuf to own module
[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 # $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 my $what = $ARGV[0] ? "non-free" : "*";
26 # max. distinct results for a given package postfix
27 my $MAX_PACKAGE_POSTFIXES = 100;
28
29 use DB_File;
30 use Packages::Config qw( $TOPDIR $DBDIR @ARCHIVES @SUITES );
31 &Packages::Config::init( './' );
32 my %packages_small = ();
33 my %package_names = ();
34 my %package_postfixes = ();
35 my %sources_packages = ();
36 my %descriptions = ();
37 my @descriptions = ("we count lines one-based\000");
38 my %packages_descriptions = ();
39 my %descriptions_packages = ();
40
41 $/ = "";
42
43 for my $archive (@ARCHIVES) {
44     for my $suite (@SUITES) {
45
46         print "Reading $archive/$suite...\n";
47         my %packages_all_db;
48         tie %packages_all_db, "DB_File", "$DBDIR/packages_all_$suite.db.new",
49                 O_RDWR|O_CREAT, 0666, $DB_BTREE
50                 or die "Error creating DB: $!";
51         open PKG, "zcat $TOPDIR/archive/$archive/$suite/$what/{,debian-installer/}binary-*/Packages.gz|";
52         while (<PKG>) {
53                 next if /^\s*$/;
54                 my $data = "";
55                 my %data = ();
56                 chomp;
57                 s/\n /\377/g;
58                 while (/^(\S+):\s*(.*)\s*$/mg) {
59                         my ($key, $value) = ($1, $2);
60                         $value =~ s/\377/\n /g;
61                         $key =~ tr [A-Z] [a-z];
62                         $data{$key} = $value;
63                 }
64                 # Skip double package
65                 next if exists($packages_all_db{"$data{'package'} $data{'architecture'} $data{'version'}"});
66
67                 $package_names{$data{'package'}} = 1;
68                 my $src = $data{'package'};
69                 my $src_version = '';
70                 if ($data{'source'}) {
71                         $src = $data{'source'};
72                         $src_version = $1
73                             if $src =~ s/\s+\(\s*=\s*(.*)\).*//; # strip version info
74                 }
75                 $data{'source'} = $src;
76                 $data{'source-version'} = $src_version;
77                 my $descr = $data{'description'};
78                 my $did = undef;
79                 if (exists($descriptions{$descr})) {
80                         $did  = $descriptions{$descr};
81                 } else {
82                         $did = 1 + $#descriptions;
83                         $descriptions[$did] = $descr;
84                         $descriptions{$descr} = $did;
85                 }
86                 $data{'description'} = $did;
87                 $packages_descriptions{"$data{'package'} $data{'version'} $data{'architecture'}"} = $did;
88                 $descriptions_packages{$did} .=
89                         "$data{'package'} $data{'version'} $data{'architecture'}\000";
90
91                 my $sdescr = $descr;
92                 $sdescr =~ s/\n.*//s;
93                 my $section = 'main';
94                 my $subsection = $data{section} || '-';
95                 if ($data{section} && ($data{section} =~ m=/=o)) {
96                     ($section, $subsection) = split m=/=o, $data{section}, 2;
97                 }
98                 $data{'section'} = $section;
99                 $data{'subsection'} = $subsection;
100                 $data{'priority'} ||= '-';
101                 $packages_small{$data{'package'}} .= "$archive $suite $data{'architecture'} ".
102                         "$section $subsection $data{'priority'} $data{'version'} $sdescr\000";
103                 $sources_packages{$src} .=
104                         "$archive $suite $data{'package'} $data{'version'} $data{'architecture'}\000";
105                 $data{archive} = $archive;
106                 while (my ($key, $value) = each (%data)) {
107                         next if $key eq 'package' or $key eq 'version' or $key eq 'architecture'
108                                 or $key eq 'maintainer';
109                         print STDERR "WARN: $key ($suite/$archive/$data{package}/$data{architecture}\n" unless defined $value;
110                         $data .= "$key: $value\n";
111                 }
112                 $packages_all_db{"$data{'package'} $data{'architecture'} $data{'version'}"}
113                         = $data;
114         }
115
116         untie %packages_all_db;
117     }
118 }
119
120 print "Writing databases...\n";
121 my %packages_small_db;
122 tie %packages_small_db, "DB_File", "$DBDIR/packages_small.db.new",
123         O_RDWR|O_CREAT, 0666, $DB_BTREE
124         or die "Error creating DB: $!";
125 while (my ($k, $v) = each(%packages_small)) {
126         $v =~ s/.$//s;
127         $packages_small_db{$k} = $v;
128 }
129 untie %packages_small_db;
130
131 my %sources_packages_db;
132 tie %sources_packages_db, "DB_File", "$DBDIR/sources_packages.db.new",
133         O_RDWR|O_CREAT, 0666, $DB_BTREE
134         or die "Error creating DB: $!";
135 while (my ($k, $v) = each(%sources_packages)) {
136         $v =~ s/.$//s;
137         $sources_packages_db{$k} = $v;
138 }
139 untie %sources_packages_db;
140
141 my %packages_descriptions_db;
142 tie %packages_descriptions_db, "DB_File", "$DBDIR/packages_descriptions.db.new",
143         O_RDWR|O_CREAT, 0666, $DB_BTREE
144         or die "Error creating DB: $!";
145 while (my ($k, $v) = each(%packages_descriptions)) {
146         $packages_descriptions_db{$k} = $v;
147 }
148 untie %packages_descriptions_db;
149
150 my %descriptions_packages_db;
151 tie %descriptions_packages_db, "DB_File", "$DBDIR/descriptions_packages.db.new",
152         O_RDWR|O_CREAT, 0666, $DB_BTREE
153         or die "Error creating DB: $!";
154 while (my ($k, $v) = each(%descriptions_packages)) {
155         $v =~ s/.$//s;
156         $descriptions_packages_db{$k} = $v;
157 }
158 untie %descriptions_packages_db;
159
160 my %descriptions_db;
161 tie %descriptions_db, "DB_File", "$DBDIR/descriptions.db.new",
162         O_RDWR|O_CREAT, 0666, $DB_BTREE
163         or die "Error creating DB: $!";
164 open DESCR, ">", "$DBDIR/descriptions.txt" or die "Error creating descriptions textfile";
165 for (my $i=1; $i<= $#descriptions; $i++) {
166         my $plain_description = $descriptions[$i];
167         $plain_description =~ s/\n .\n/ /og;
168         $plain_description =~ s/[\n \t]+/ /og;
169         print DESCR "$plain_description\n";
170         $descriptions_db{$i} = $descriptions[$i];
171 }
172 close DESCR;
173 untie %descriptions_db;
174
175 # package names stuff:
176 for my $pkg (keys %package_names) {
177         for (my $i=0;$i<length($pkg)-1;$i++) {
178                 my $before = substr($pkg, 0, $i);
179                 my $after = substr($pkg, $i);
180                 $before = "^" if $before eq ""; # otherwise split doesn't work properly
181                 $package_postfixes{$after} .= "$before\0";
182         }
183 }
184 my %package_postfixes_db;
185 tie %package_postfixes_db, "DB_File", "$DBDIR/package_postfixes.db.new",
186         O_RDWR|O_CREAT, 0666, $DB_BTREE
187         or die "Error creating DB: $!";
188 while (my ($k, $v) = each(%package_postfixes)) {
189         $v =~ s/.$//s;
190         my $nr = $v;
191         $nr =~ s/[^\000]//g;
192         $nr = length($nr) + 1; # < number of hits
193         if ($nr > $MAX_PACKAGE_POSTFIXES) {
194                 $v = "\001" . $nr;
195         }
196         $package_postfixes_db{$k} = $v;
197 }
198 untie %package_postfixes_db;
199
200 rename("$DBDIR/packages_small.db.new", "$DBDIR/packages_small.db");
201 rename("$DBDIR/sources_packages.db.new", "$DBDIR/sources_packages.db");
202 for my $suite (@SUITES) {
203         rename("$DBDIR/packages_all_$suite.db.new",
204                "$DBDIR/packages_all_$suite.db");
205 }
206 rename("$DBDIR/packages_descriptions.db.new",
207        "$DBDIR/packages_descriptions.db");
208 rename("$DBDIR/descriptions_packages.db.new",
209        "$DBDIR/descriptions_packages.db");
210 rename("$DBDIR/descriptions.txt.new", "$DBDIR/descriptions.txt");
211 rename("$DBDIR/descriptions.db.new", "$DBDIR/descriptions.db");
212 rename("$DBDIR/package_postfixes.db.new", "$DBDIR/package_postfixes.db");