2 # Convert Packages.gz files into Sleepycat db files for efficient usage of
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 my $what = $ARGV[0] ? "non-free" : "*";
28 # max. distinct results for a given package postfix
29 my $MAX_PACKAGE_POSTFIXES = 100;
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 = ();
50 for my $archive (@ARCHIVES) {
51 for my $suite (@SUITES) {
53 my %virtual_packages = ();
54 print "Reading $archive/$suite...\n";
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|";
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];
73 next if exists($packages_all_db{"$data{'package'} $data{'architecture'} $data{'version'}"});
75 if ($data{'provides'}) {
76 foreach (split /\s*,\s*/, $data{'provides'}) {
77 $virtual_packages{$_}{$data{'package'}}++;
80 $package_names{$data{'package'}} = 1;
81 my $src = $data{'package'};
83 if ($data{'source'}) {
84 $src = $data{'source'};
85 $src =~ s/\s+.*//; # strip version info
87 $data{'source'} = $src;
88 my $descr = $data{'description'};
90 if (exists($descriptions{$descr})) {
91 $did = $descriptions{$descr};
93 $did = 1 + $#descriptions;
94 $descriptions[$did] = $descr;
95 $descriptions{$descr} = $did;
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";
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';
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";
129 $packages_all_db{"$data{'package'} $data{'architecture'} $data{'version'}"}
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";
138 untie %packages_all_db;
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)) {
149 $packages_small_db{$k} = $v;
151 untie %packages_small_db;
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)) {
159 $sources_packages_db{$k} = $v;
161 untie %sources_packages_db;
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;
170 untie %packages_descriptions_db;
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)) {
178 $descriptions_packages_db{$k} = $v;
180 untie %descriptions_packages_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];
200 untie %descriptions_db;
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";
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)) {
219 $nr = length($nr) + 1; # < number of hits
220 if ($nr > $MAX_PACKAGE_POSTFIXES) {
223 $package_postfixes_db{$k} = $v;
225 untie %package_postfixes_db;
227 store \%sections, "$DBDIR/sections.info";
228 store \%subsections, "$DBDIR/subsections.info";
229 store \%priorities, "$DBDIR/priorities.info";
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");
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");