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