Split section (main, etc.) from subsection (admin, etc.) on db generation
[deb/packages.git] / bin / parse-packages
1 #!/usr/bin/perl -w
2 # Convert Packages.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
24 my $what = $1 ? "non-free" : "*";
25 # max. distinct results for a given package postfix
26 my $MAX_PACKAGE_POSTFIXES = 100;
27
28 use DB_File;
29 my %packages_small = ();
30 my %package_names = ();
31 my %package_postfixes = ();
32 my %sources_packages = ();
33 my %descriptions = ();
34 my @descriptions = ("we count lines one-based\000");
35 my %packages_descriptions = ();
36 my %descriptions_packages = ();
37
38 my @suites = ('oldstable', 'stable', 'testing', 'unstable', 'experimental');
39
40 $/ = "";
41
42 for my $suite (@suites) {
43
44         print "Reading $suite...\n";
45         my %packages_all_db;
46         tie %packages_all_db, "DB_File", "packages_all_$suite.db.new",
47                 O_RDWR|O_CREAT, 0666, $DB_BTREE
48                 or die "Error creating DB: $!";
49         open PKG, "zcat /org/ftp.debian.org/ftp/dists/$suite/$what/{,debian-installer/}binary-*/Packages.gz|";
50         while (<PKG>) {
51                 next if /^\s*$/;
52                 my $data = "";
53                 my %data = ();
54                 chomp;
55                 s/\n /\377/g;
56                 #s/\376\377\s*\376\377/\376\377/og;
57                 while (/^(\S+):\s*(.*)\s*$/mg) {
58                         my ($key, $value) = ($1, $2);
59                         $value =~ s/\377/\n /g;
60                         $data .= "$key: $value\n";
61                         $key =~ tr [A-Z] [a-z];
62                         $data{$key} = $value;
63                 }
64                 # Skip double package
65                 next if exists($packages_all_db{"$data{'package'} $data{'architecture'} $data{'version'}"});
66                 $packages_all_db{"$data{'package'} $data{'architecture'} $data{'version'}"}
67                         = $data;
68
69                 $package_names{$data{'package'}} = 1;
70                 my $src = $data{'package'};
71                 my $srcversion = $data{'version'};
72                 if ($data{'source'}) {
73                         $src = $data{'source'};
74                         if ($src =~ /(\S+) \((\S+)\)/) {
75                                 $src = $1;
76                                 $srcversion = $2;
77                         }
78                 }
79                 my $descr = $data{'description'};
80                 my $did = undef;
81                 if (exists($descriptions{$descr})) {
82                         $did  = $descriptions{$descr};
83                 } else {
84                         $did = 1 + $#descriptions;
85                         $descriptions[$did] = $descr;
86                         $descriptions{$descr} = $did;
87                 }
88                 $packages_descriptions{"$data{'package'} $data{'version'} $data{'architecture'}"} = $did;
89                 $descriptions_packages{$did} .=
90                         "$data{'package'} $data{'version'} $data{'architecture'}\000";
91
92                 my $sdescr = $descr;
93                 $sdescr =~ s/\n.*//s;
94                 my $section = 'main';
95                 my $subsection = $data{section};
96                 if ($data{section} && ($data{section} =~ m=/=o)) {
97                     ($section, $subsection) = split m=/=o, $data{section}, 2;
98                 }
99                 $packages_small{$data{'package'}} .= "$suite $data{'architecture'} ".
100                         "$section $subsection $data{'priority'} $data{'version'} $sdescr\000";
101                 $sources_packages{$src} .=
102                         "$data{'package'} $data{'architecture'} $data{'version'}\000";
103         }
104
105         untie %packages_all_db;
106 }
107
108 print "Writing databases...\n";
109 my %packages_small_db;
110 tie %packages_small_db, "DB_File", "packages_small.db.new",
111         O_RDWR|O_CREAT, 0666, $DB_BTREE
112         or die "Error creating DB: $!";
113 while (my ($k, $v) = each(%packages_small)) {
114         $v =~ s/.$//s;
115         $packages_small_db{$k} = $v;
116 }
117 untie %packages_small_db;
118
119 my %sources_packages_db;
120 tie %sources_packages_db, "DB_File", "sources_packages.db.new",
121         O_RDWR|O_CREAT, 0666, $DB_BTREE
122         or die "Error creating DB: $!";
123 while (my ($k, $v) = each(%sources_packages)) {
124         $v =~ s/.$//s;
125         $sources_packages_db{$k} = $v;
126 }
127 untie %sources_packages_db;
128
129 my %packages_descriptions_db;
130 tie %packages_descriptions_db, "DB_File", "packages_descriptions.db.new",
131         O_RDWR|O_CREAT, 0666, $DB_BTREE
132         or die "Error creating DB: $!";
133 while (my ($k, $v) = each(%packages_descriptions)) {
134         $packages_descriptions_db{$k} = $v;
135 }
136 untie %packages_descriptions_db;
137
138 my %descriptions_packages_db;
139 tie %descriptions_packages_db, "DB_File", "descriptions_packages.db.new",
140         O_RDWR|O_CREAT, 0666, $DB_BTREE
141         or die "Error creating DB: $!";
142 while (my ($k, $v) = each(%descriptions_packages)) {
143         $v =~ s/.$//s;
144         $descriptions_packages_db{$k} = $v;
145 }
146 untie %descriptions_packages_db;
147
148 my %descriptions_db;
149 tie %descriptions_db, "DB_File", "descriptions.db.new",
150         O_RDWR|O_CREAT, 0666, $DB_BTREE
151         or die "Error creating DB: $!";
152 open DESCR, "> descriptions.txt" or die "Error creating descriptions textfile";
153 for (my $i=1; $i<= $#descriptions; $i++) {
154         my $null_d = $descriptions[$i];
155         $null_d =~ s/\n/\000/g;
156         print DESCR "$null_d\n";
157         $descriptions_db{$i} = $descriptions[$i];
158 }
159 close DESCR;
160 untie %descriptions_db;
161
162 # package names stuff:
163 for my $pkg (keys %package_names) {
164         for (my $i=0;$i<length($pkg)-1;$i++) {
165                 my $before = substr($pkg, 0, $i);
166                 my $after = substr($pkg, $i);
167                 $package_postfixes{$after} .= "$before\0";
168         }
169 }
170 my %package_postfixes_db;
171 tie %package_postfixes_db, "DB_File", "package_postfixes.db.new",
172         O_RDWR|O_CREAT, 0666, $DB_BTREE
173         or die "Error creating DB: $!";
174 while (my ($k, $v) = each(%package_postfixes)) {
175         $v =~ s/.$//s;
176         my $nr = $v;
177         $nr =~ s/[^\000]//g;
178         $nr = length($nr) + 1; # < number of hits
179         if ($nr > $MAX_PACKAGE_POSTFIXES) {
180                 $v = "\001" . $nr;
181         }
182         $package_postfixes_db{$k} = $v;
183 }
184 untie %package_postfixes_db;
185
186 rename("packages_small.db.new", "packages_small.db");
187 rename("sources_packages.db.new", "sources_packages.db");
188 for my $suite (@suites) {
189         rename("packages_all_$suite.db.new", "packages_all_$suite.db");
190 }
191 rename("packages_descriptions.db.new", "packages_descriptions.db");
192 rename("descriptions_packages.db.new", "descriptions_packages.db");
193 rename("descriptions.txt.new", "descriptions.txt");
194 rename("descriptions.db.new", "descriptions.db");
195 rename("package_postfixes.db.new", "package_postfixes.db");