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