2 # Convert Sources.gz files into Sleepycat db files for efficient usage of
5 # Copyright (C) 2006 Jeroen van Wolffelaar <jeroen@wolffelaar.nl>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 my $what = $ARGV[0] ? "non-free" : "*";
26 # max. distinct results for a given package postfix
27 my $MAX_SOURCE_POSTFIXES = 100;
31 use Packages::Config qw( $TOPDIR $DBDIR @ARCHIVES @SUITES );
32 &Packages::Config::init( './' );
33 my %sources_small = ();
34 my %source_names = ();
35 my %source_postfixes = ();
39 -d $DBDIR || mkpath( $DBDIR );
41 for my $archive (@ARCHIVES) {
42 for my $suite (@SUITES) {
44 print "Reading $archive/$suite...\n";
45 my %source_names_suite = ();
47 tie %sources_all_db, "DB_File", "$DBDIR/sources_all_$suite.db.new",
48 O_RDWR|O_CREAT, 0666, $DB_BTREE
49 or die "Error creating DB: $!";
50 if (!-d "$TOPDIR/archive/$archive/$suite/") {
51 print "\tseems not to exist, skipping...\n";
54 open PKG, "zcat $TOPDIR/archive/$archive/$suite/$what/source/Sources.gz|";
61 while (/^(\S+):\s*(.*)\s*$/mg) {
62 my ($key, $value) = ($1, $2);
63 $value =~ s/\377/\n /g;
64 $key =~ tr [A-Z] [a-z];
67 $source_names{$data{'package'}} = 1;
68 $source_names_suite{$data{'package'}} = 1;
69 delete $data{'binary'};
71 $data{files} =~ s/\s*\n\s*/\01/sog;
72 $data{files} =~ s/^\01//sg;
75 my $subsection = $data{section} || '-';
76 if ($data{section} && ($data{section} =~ m=/=o)) {
77 ($section, $subsection) = split m=/=o, $data{section}, 2;
79 $data{'section'} = $section;
80 $data{'subsection'} = $subsection;
81 $data{'priority'} ||= "-";
82 $sources_small{$data{'package'}} .=
83 "$archive $suite $section $subsection $data{'priority'} $data{'version'}\000";
85 while (my ($key, $value) = each (%data)) {
86 next if $key eq 'package' or $key eq 'archive' or $key eq 'suite';
87 print STDERR "WARN: $key ($suite/$archive/$data{package}/$data{architecture}\n" unless defined $value;
88 $data .= "$key\00$value\00";
91 $sources_all_db{"$archive $suite $data{'package'}"}
94 open NAMES, '>>', "$DBDIR/source_names_$suite.txt.new"
95 or die "Error creating source names list: $!";
96 foreach (sort keys %source_names_suite) {
101 untie %sources_all_db;
105 print "Writing databases...\n";
106 my %sources_small_db;
107 tie %sources_small_db, "DB_File", "$DBDIR/sources_small.db.new",
108 O_RDWR|O_CREAT, 0666, $DB_BTREE
109 or die "Error creating DB: $!";
110 while (my ($k, $v) = each(%sources_small)) {
112 $sources_small_db{$k} = $v;
114 untie %sources_small_db;
116 # package names stuff:
117 for my $pkg (keys %source_names) {
118 for (my $i=0;$i<length($pkg)-1;$i++) {
119 my $before = substr($pkg, 0, $i);
120 my $after = substr($pkg, $i);
121 $before = "^" if $before eq ""; # otherwise split doesn't work properly
122 $source_postfixes{$after} .= "$before\0";
125 my %source_postfixes_db;
126 tie %source_postfixes_db, "DB_File", "$DBDIR/source_postfixes.db.new",
127 O_RDWR|O_CREAT, 0666, $DB_BTREE
128 or die "Error creating DB: $!";
129 while (my ($k, $v) = each(%source_postfixes)) {
133 $nr = length($nr) + 1; # < number of hits
134 if ($nr > $MAX_SOURCE_POSTFIXES) {
137 $source_postfixes_db{$k} = $v;
139 untie %source_postfixes_db;
141 for my $suite (@SUITES) {
142 rename("$DBDIR/sources_all_$suite.db.new", "$DBDIR/sources_all_$suite.db");
143 rename("$DBDIR/source_names_$suite.txt.new", "$DBDIR/source_names_$suite.txt");
145 rename("$DBDIR/sources_small.db.new", "$DBDIR/sources_small.db");
146 rename("$DBDIR/source_postfixes.db.new", "$DBDIR/source_postfixes.db");