6caf5cfa42d5f7a5a60f722c3fdda917c2b18407
[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                 $packages_small{$data{'package'}} .= "$suite $data{'architecture'} ".
95                         "$data{'section'} $data{'priority'} $data{'version'} $sdescr\000";
96                 $sources_packages{$src} .=
97                         "$data{'package'} $data{'architecture'} $data{'version'}\000";
98         }
99
100         untie %packages_all_db;
101 }
102
103 print "Writing databases...\n";
104 my %packages_small_db;
105 tie %packages_small_db, "DB_File", "packages_small.db.new",
106         O_RDWR|O_CREAT, 0666, $DB_BTREE
107         or die "Error creating DB: $!";
108 while (my ($k, $v) = each(%packages_small)) {
109         $v =~ s/.$//s;
110         $packages_small_db{$k} = $v;
111 }
112 untie %packages_small_db;
113
114 my %sources_packages_db;
115 tie %sources_packages_db, "DB_File", "sources_packages.db.new",
116         O_RDWR|O_CREAT, 0666, $DB_BTREE
117         or die "Error creating DB: $!";
118 while (my ($k, $v) = each(%sources_packages)) {
119         $v =~ s/.$//s;
120         $sources_packages_db{$k} = $v;
121 }
122 untie %sources_packages_db;
123
124 my %packages_descriptions_db;
125 tie %packages_descriptions_db, "DB_File", "packages_descriptions.db.new",
126         O_RDWR|O_CREAT, 0666, $DB_BTREE
127         or die "Error creating DB: $!";
128 while (my ($k, $v) = each(%packages_descriptions)) {
129         $packages_descriptions_db{$k} = $v;
130 }
131 untie %packages_descriptions_db;
132
133 my %descriptions_packages_db;
134 tie %descriptions_packages_db, "DB_File", "descriptions_packages.db.new",
135         O_RDWR|O_CREAT, 0666, $DB_BTREE
136         or die "Error creating DB: $!";
137 while (my ($k, $v) = each(%descriptions_packages)) {
138         $v =~ s/.$//s;
139         $descriptions_packages_db{$k} = $v;
140 }
141 untie %descriptions_packages_db;
142
143 my %descriptions_db;
144 tie %descriptions_db, "DB_File", "descriptions.db.new",
145         O_RDWR|O_CREAT, 0666, $DB_BTREE
146         or die "Error creating DB: $!";
147 open DESCR, "> descriptions.txt" or die "Error creating descriptions textfile";
148 for (my $i=1; $i<= $#descriptions; $i++) {
149         my $null_d = $descriptions[$i];
150         $null_d =~ s/\n/\000/g;
151         print DESCR "$null_d\n";
152         $descriptions_db{$i} = $descriptions[$i];
153 }
154 close DESCR;
155 untie %descriptions_db;
156
157 # package names stuff:
158 for my $pkg (keys %package_names) {
159         for (my $i=0;$i<length($pkg)-1;$i++) {
160                 my $before = substr($pkg, 0, $i);
161                 my $after = substr($pkg, $i);
162                 $package_postfixes{$after} .= "$before\0";
163         }
164 }
165 my %package_postfixes_db;
166 tie %package_postfixes_db, "DB_File", "package_postfixes.db.new",
167         O_RDWR|O_CREAT, 0666, $DB_BTREE
168         or die "Error creating DB: $!";
169 while (my ($k, $v) = each(%package_postfixes)) {
170         $v =~ s/.$//s;
171         my $nr = $v;
172         $nr =~ s/[^\000]//g;
173         $nr = length($nr) + 1; # < number of hits
174         if ($nr > $MAX_PACKAGE_POSTFIXES) {
175                 $v = "\001" . $nr;
176         }
177         $package_postfixes_db{$k} = $v;
178 }
179 untie %package_postfixes_db;
180
181 rename("packages_small.db.new", "packages_small.db");
182 rename("sources_packages.db.new", "sources_packages.db");
183 for my $suite (@suites) {
184         rename("packages_all_$suite.db.new", "packages_all_$suite.db");
185 }
186 rename("packages_descriptions.db.new", "packages_descriptions.db");
187 rename("descriptions_packages.db.new", "descriptions_packages.db");
188 rename("descriptions.txt.new", "descriptions.txt");
189 rename("descriptions.db.new", "descriptions.db");
190 rename("package_postfixes.db.new", "package_postfixes.db");