]> git.deb.at Git - deb/packages.git/blob - bin/parse-sources
setup-site: Only search for files to work on two directories deep
[deb/packages.git] / bin / parse-sources
1 #!/usr/bin/perl -w
2 # Convert Sources.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_SOURCE_POSTFIXES = 100;
30
31 use DB_File;
32 use File::Path;
33 use Packages::Config qw( $TOPDIR $DBDIR @ARCHIVES @SUITES );
34 &Packages::Config::init( './' );
35 my %sources_small = ();
36 my %source_names = ();
37 my %source_postfixes = ();
38
39 $/ = "";
40
41 -d $DBDIR || mkpath( $DBDIR );
42
43 for my $archive (@ARCHIVES) {
44     for my $suite (@SUITES) {
45
46         print "Reading $archive/$suite...\n";
47         my %source_names_suite = ();
48         my %sources_all_db;
49         tie %sources_all_db, "DB_File", "$DBDIR/sources_all_$suite.db.new",
50                 O_RDWR|O_CREAT, 0666, $DB_BTREE
51                 or die "Error creating DB: $!";
52         if (!-d "$TOPDIR/archive/$archive/$suite/") {
53                 print "\tseems not to exist, skipping...\n";
54                 next;
55         }
56         open PKG, "zcat $TOPDIR/archive/$archive/$suite/$what/source/Sources.gz|";
57         while (<PKG>) {
58                 next if /^\s*$/;
59                 my $data = "";
60                 my %data = ();
61                 chomp;
62                 s/\n /\377/g;
63                 while (/^(\S+):\s*(.*)\s*$/mg) {
64                         my ($key, $value) = ($1, $2);
65                         $value =~ s/\377/\n /g;
66                         $key =~ tr [A-Z] [a-z];
67                         $data{$key} = $value;
68                 }
69                 $source_names{$data{'package'}} = 1;
70                 $source_names_suite{$data{'package'}} = 1;
71                 delete $data{'binary'};
72
73                 $data{files} =~ s/\s*\n\s*/\01/sog;
74                 $data{files} =~ s/^\01//sg;
75
76                 my $section = 'main';
77                 my $subsection = $data{section} || '-';
78                 if ($data{section} && ($data{section} =~ m=/=o)) {
79                     ($section, $subsection) = split m=/=o, $data{section}, 2;
80                     ($subsection, $section) = split m=/=o, $data{section}, 2
81                         if $section eq 'non-US';
82                 }
83                 $data{'section'} = $section;
84                 $data{'subsection'} = $subsection;
85                 $data{'priority'} ||= "-";
86                 $sources_small{$data{'package'}} .=
87                         "$archive $suite $section $subsection $data{'priority'} $data{'version'}\000";
88
89                 while (my ($key, $value) = each (%data)) {
90                     next if $key eq 'package' or $key eq 'archive' or $key eq 'suite';
91                     print STDERR "WARN: $key ($suite/$archive/$data{package}/$data{architecture}\n" unless defined $value;
92                     $data .= "$key\00$value\00";
93                 }
94                 $data =~ s/.$//so;
95                 $sources_all_db{"$archive $suite $data{'package'}"}
96                         = $data;
97         }
98         open NAMES, '>>', "$DBDIR/source_names_$suite.txt.new"
99             or die "Error creating source names list: $!";
100         foreach (sort keys %source_names_suite) {
101             print NAMES "$_\n";
102         }
103         close NAMES;
104
105         untie %sources_all_db;
106     }
107 }
108
109 print "Writing databases...\n";
110 my %sources_small_db;
111 tie %sources_small_db, "DB_File", "$DBDIR/sources_small.db.new",
112         O_RDWR|O_CREAT, 0666, $DB_BTREE
113         or die "Error creating DB: $!";
114 while (my ($k, $v) = each(%sources_small)) {
115         $v =~ s/.$//s;
116         $sources_small_db{$k} = $v;
117 }
118 untie %sources_small_db;
119
120 # package names stuff:
121 for my $pkg (keys %source_names) {
122         for (my $i=0;$i<length($pkg)-1;$i++) {
123                 my $before = substr($pkg, 0, $i);
124                 my $after = substr($pkg, $i);
125                 $before = "^" if $before eq ""; # otherwise split doesn't work properly
126                 $source_postfixes{$after} .= "$before\0";
127         }
128 }
129 my %source_postfixes_db;
130 tie %source_postfixes_db, "DB_File", "$DBDIR/source_postfixes.db.new",
131         O_RDWR|O_CREAT, 0666, $DB_BTREE
132         or die "Error creating DB: $!";
133 while (my ($k, $v) = each(%source_postfixes)) {
134         $v =~ s/.$//s;
135         my $nr = $v;
136         $nr =~ s/[^\000]//g;
137         $nr = length($nr) + 1; # < number of hits
138         if ($nr > $MAX_SOURCE_POSTFIXES) {
139                 $v = "\001" . $nr;
140         }
141         $source_postfixes_db{$k} = $v;
142 }
143 untie %source_postfixes_db;
144
145 for my $suite (@SUITES) {
146         rename("$DBDIR/sources_all_$suite.db.new", "$DBDIR/sources_all_$suite.db");
147         rename("$DBDIR/source_names_$suite.txt.new", "$DBDIR/source_names_$suite.txt");
148 }
149 rename("$DBDIR/sources_small.db.new", "$DBDIR/sources_small.db");
150 rename("$DBDIR/source_postfixes.db.new", "$DBDIR/source_postfixes.db");