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