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
28 my $what = $ARGV[0] ? "non-free" : "*";
29 # max. distinct results for a given package postfix
30 my $MAX_PACKAGE_POSTFIXES = 100;
36 use Packages::Config qw( $TOPDIR $DBDIR @ARCHIVES @SUITES );
37 &Packages::Config::init( './' );
38 my %packages_small = ();
39 my %virtual_packages = ();
40 my %package_names = ();
41 my %package_postfixes = ();
42 my %sources_packages = ();
43 my %descriptions = ();
44 my @descriptions = ("we count lines one-based\000");
45 my %packages_descriptions = ();
46 my %descriptions_packages = ();
54 -d $DBDIR || mkpath( $DBDIR );
56 for my $suite (@SUITES) {
57 my %package_names_suite = ();
59 tie %packages_all_db, "DB_File", "$DBDIR/packages_all_$suite.db.new",
60 O_RDWR|O_CREAT, 0666, $DB_BTREE
61 or die "Error creating DB: $!";
63 for my $archive (@ARCHIVES) {
64 print "Reading $archive/$suite...\n";
65 if (!-d "$TOPDIR/archive/$archive/$suite/") {
66 print "\tseems not to exist, skipping...\n";
69 open PKG, "zcat $TOPDIR/archive/$archive/$suite/$what/{,debian-installer/}binary-*/Packages.gz|";
76 while (/^(\S+):\s*(.*)\s*$/mg) {
77 my ($key, $value) = ($1, $2);
78 $value =~ s/\377/\n /g;
79 $key =~ tr [A-Z] [a-z];
83 next if exists($packages_all_db{"$data{'package'} $data{'architecture'} $data{'version'}"});
84 # Skip arch:all for amd64&kfreebsd, too often broken
85 next if ($archive eq 'amd64' or $archive eq 'kfreebsd')
86 and $data{architecture} eq 'all';
88 if ($data{'provides'}) {
89 foreach (split /\s*,\s*/, $data{'provides'}) {
90 $virtual_packages{$_}{$suite}{$data{'package'}}++;
91 $packages_small{$_} ||= {};
94 $package_names{$data{'package'}} = 1;
95 $package_names_suite{$data{'package'}} = 1;
96 my $src = $data{'package'};
98 if ($data{'source'}) {
99 $src = $data{'source'};
100 $src =~ s/\s+.*//; # strip version info
102 $data{'source'} = $src;
103 my $descr = $data{'description'};
105 if (exists($descriptions{$descr})) {
106 $did = $descriptions{$descr};
108 $did = 1 + $#descriptions;
109 $descriptions[$did] = $descr;
110 $descriptions{$descr} = $did;
112 $data{'description'} = $did;
113 $packages_descriptions{"$data{'package'} $data{'version'} $data{'architecture'}"} = $did;
114 $descriptions_packages{$did} .=
115 "$data{'package'} $data{'version'} $data{'architecture'}\000";
118 $sdescr =~ s/\n.*//s;
119 my $section = 'main';
120 my $subsection = $data{section} || '-';
121 if ($data{section} && ($data{section} =~ m=/=o)) {
122 ($section, $subsection) = split m=/=o, $data{section}, 2;
123 ($subsection, $section) = split m=/=o, $data{section}, 2
124 if $section eq 'non-US';
126 $data{'section'} = $section;
127 $data{'subsection'} = $subsection;
128 $data{'priority'} ||= '-';
129 $sections{$suite}{$section}++;
130 $subsections{$suite}{$subsection}++;
131 $priorities{$suite}{$data{priority}}++;
132 my $pkgitem = "$archive $suite $data{'architecture'} ".
133 "$section $subsection $data{'priority'} $data{'version'} $sdescr\0";
134 my $previtem = ($packages_small{$data{'package'}}{$suite}{$data{'architecture'}}
136 $packages_small{$data{'package'}}{$suite}{$data{'architecture'}} = $pkgitem
137 if version_cmp($data{'version'}, (split /\s/o, $previtem)[6]) > 0;
138 $previtem = ($packages_small{$data{'package'}}{$suite}{'any'}
140 $packages_small{$data{'package'}}{$suite}{'any'} = $pkgitem
141 if version_cmp($data{'version'}, (split /\s/o, $previtem)[6]) > 0;
142 $sources_packages{$src} .=
143 "$archive $suite $data{'package'} $data{'version'} $data{'architecture'}\000";
144 $data{archive} = $archive;
145 while (my ($key, $value) = each (%data)) {
146 next if $key eq 'package' or $key eq 'version' or $key eq 'architecture'
147 or $key eq 'maintainer';
148 print STDERR "WARN: $key ($suite/$archive/$data{package}/$data{architecture}\n" unless defined $value;
149 $data .= "$key\00$value\00";
152 $packages_all_db{"$data{'package'} $data{'architecture'} $data{'version'}"}
157 open NAMES, '>', "$DBDIR/package_names_$suite.txt.new"
158 or die "Error creating package names list: $!";
159 foreach (sort keys %package_names_suite) {
164 untie %packages_all_db;
167 print "Writing databases...\n";
168 my %packages_small_db;
169 tie %packages_small_db, "DB_File", "$DBDIR/packages_small.db.new",
170 O_RDWR|O_CREAT, 0666, $DB_BTREE
171 or die "Error creating DB: $!";
172 while (my ($pkg, $v) = each(%packages_small)) {
173 my ($res1, $res2, $res3) = ("", "", "");
174 while (my ($suite, $v2) = each %$v) {
175 $res2 .= $v2->{'any'};
176 while (my ($arch, $v3) = each %$v2) {
177 next if $arch eq 'any' or $v3 eq $v2->{'any'};
182 if (exists $virtual_packages{$pkg}) {
183 while (my ($suite, $v2) = each %{$virtual_packages{$pkg}}) {
184 $res1 .= "$suite\01".(join ' ', keys %$v2)."\01";
191 my $res = "$res1$res2$res3";
193 $packages_small_db{$pkg} = $res;
195 untie %packages_small_db;
197 my %sources_packages_db;
198 tie %sources_packages_db, "DB_File", "$DBDIR/sources_packages.db.new",
199 O_RDWR|O_CREAT, 0666, $DB_BTREE
200 or die "Error creating DB: $!";
201 while (my ($k, $v) = each(%sources_packages)) {
203 $sources_packages_db{$k} = $v;
205 untie %sources_packages_db;
207 my %packages_descriptions_db;
208 tie %packages_descriptions_db, "DB_File", "$DBDIR/packages_descriptions.db.new",
209 O_RDWR|O_CREAT, 0666, $DB_BTREE
210 or die "Error creating DB: $!";
211 while (my ($k, $v) = each(%packages_descriptions)) {
212 $packages_descriptions_db{$k} = $v;
214 untie %packages_descriptions_db;
216 my %descriptions_packages_db;
217 tie %descriptions_packages_db, "DB_File", "$DBDIR/descriptions_packages.db.new",
218 O_RDWR|O_CREAT, 0666, $DB_BTREE
219 or die "Error creating DB: $!";
220 while (my ($k, $v) = each(%descriptions_packages)) {
222 $descriptions_packages_db{$k} = $v;
224 untie %descriptions_packages_db;
227 tie %descriptions_db, "DB_File", "$DBDIR/descriptions.db.new",
228 O_RDWR|O_CREAT, 0666, $DB_BTREE
229 or die "Error creating DB: $!";
230 open DESCR, ">", "$DBDIR/descriptions.txt" or die "Error creating descriptions textfile";
231 for (my $i=1; $i<= $#descriptions; $i++) {
232 my $plain_description = $descriptions[$i];
233 # WARNING: This needs to correspond with what happens in
234 # Packages/Search.pm:do_fulltext_search
235 $plain_description =~ tr [A-Z] [a-z];
236 # ensure one space on both ends
237 $plain_description = " $plain_description ";
238 $plain_description =~ s/[(),.-]+//og;
239 $plain_description =~ s#[^a-z0-9_/+]+# #og;
240 print DESCR "$plain_description\n";
241 $descriptions_db{$i} = $descriptions[$i];
244 untie %descriptions_db;
246 # package names stuff:
247 for my $pkg (keys %package_names) {
248 for (my $i=0;$i<length($pkg)-1;$i++) {
249 my $before = substr($pkg, 0, $i);
250 my $after = substr($pkg, $i);
251 $before = "^" if $before eq ""; # otherwise split doesn't work properly
252 $package_postfixes{$after} .= "$before\0";
255 my %package_postfixes_db;
256 tie %package_postfixes_db, "DB_File", "$DBDIR/package_postfixes.db.new",
257 O_RDWR|O_CREAT, 0666, $DB_BTREE
258 or die "Error creating DB: $!";
259 while (my ($k, $v) = each(%package_postfixes)) {
263 $nr = length($nr) + 1; # < number of hits
264 if ($nr > $MAX_PACKAGE_POSTFIXES) {
267 $package_postfixes_db{$k} = $v;
269 untie %package_postfixes_db;
271 store \%sections, "$DBDIR/sections.info";
272 store \%subsections, "$DBDIR/subsections.info";
273 store \%priorities, "$DBDIR/priorities.info";
275 rename("$DBDIR/packages_small.db.new", "$DBDIR/packages_small.db");
276 rename("$DBDIR/sources_packages.db.new", "$DBDIR/sources_packages.db");
277 for my $suite (@SUITES) {
278 rename("$DBDIR/packages_all_$suite.db.new",
279 "$DBDIR/packages_all_$suite.db");
280 rename("$DBDIR/package_names_$suite.txt.new",
281 "$DBDIR/package_names_$suite.txt");
283 rename("$DBDIR/packages_descriptions.db.new",
284 "$DBDIR/packages_descriptions.db");
285 rename("$DBDIR/descriptions_packages.db.new",
286 "$DBDIR/descriptions_packages.db");
287 rename("$DBDIR/descriptions.txt.new", "$DBDIR/descriptions.txt");
288 rename("$DBDIR/descriptions.db.new", "$DBDIR/descriptions.db");
289 rename("$DBDIR/package_postfixes.db.new", "$DBDIR/package_postfixes.db");