]> git.deb.at Git - deb/packages.git/blob - bin/parse-sources
Enable zh-tw
[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 # 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.
10
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.
15
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.
19
20 use strict;
21 use lib './lib';
22
23 $| = 1;
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 File::Path;
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 = ();
36
37 $/ = "";
38
39 -d $DBDIR || mkpath( $DBDIR );
40
41 for my $archive (@ARCHIVES) {
42     for my $suite (@SUITES) {
43
44         print "Reading $archive/$suite...\n";
45         my %source_names_suite = ();
46         my %sources_all_db;
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";
52                 next;
53         }
54         open PKG, "zcat $TOPDIR/archive/$archive/$suite/$what/source/Sources.gz|";
55         while (<PKG>) {
56                 next if /^\s*$/;
57                 my $data = "";
58                 my %data = ();
59                 chomp;
60                 s/\n /\377/g;
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];
65                         $data{$key} = $value;
66                 }
67                 $source_names{$data{'package'}} = 1;
68                 $source_names_suite{$data{'package'}} = 1;
69                 delete $data{'binary'};
70
71                 $data{files} =~ s/\s*\n\s*/\01/sog;
72                 $data{files} =~ s/^\01//sg;
73
74                 my $section = 'main';
75                 my $subsection = $data{section} || '-';
76                 if ($data{section} && ($data{section} =~ m=/=o)) {
77                     ($section, $subsection) = split m=/=o, $data{section}, 2;
78                 }
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";
84
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";
89                 }
90                 $data =~ s/.$//so;
91                 $sources_all_db{"$archive $suite $data{'package'}"}
92                         = $data;
93         }
94         open NAMES, '>>', "$DBDIR/source_names_$suite.txt.new"
95             or die "Error creating source names list: $!";
96         foreach (sort keys %source_names_suite) {
97             print NAMES "$_\n";
98         }
99         close NAMES;
100
101         untie %sources_all_db;
102     }
103 }
104
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)) {
111         $v =~ s/.$//s;
112         $sources_small_db{$k} = $v;
113 }
114 untie %sources_small_db;
115
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";
123         }
124 }
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)) {
130         $v =~ s/.$//s;
131         my $nr = ($v =~ tr/\000/\000/) + 1;
132         if ($nr > $MAX_SOURCE_POSTFIXES) {
133             $v = ($v =~ /\^/) ? "^\001" . $nr
134                 : "\001" . $nr;
135         }
136         $source_postfixes_db{$k} = $v;
137 }
138 untie %source_postfixes_db;
139
140 for my $suite (@SUITES) {
141         rename("$DBDIR/sources_all_$suite.db.new", "$DBDIR/sources_all_$suite.db");
142         rename("$DBDIR/source_names_$suite.txt.new", "$DBDIR/source_names_$suite.txt");
143 }
144 rename("$DBDIR/sources_small.db.new", "$DBDIR/sources_small.db");
145 rename("$DBDIR/source_postfixes.db.new", "$DBDIR/source_postfixes.db");