]> git.deb.at Git - deb/packages.git/blob - bin/parse-contents
Add debian-ports archs: powerpcspe sh4 sparc64 (Closes: #571325)
[deb/packages.git] / bin / parse-contents
1 #!/usr/bin/perl -w
2 # Convert Contents.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 warnings;
22 use lib './lib';
23
24 $| = 1;
25
26 # Important, we want sorting and such to happen like in the C locale: binary,
27 # without any fancy collation. FIXME: is this actually adequate?
28 $ENV{"LC_ALL"} = 'C';
29
30 my $what = $ARGV[0] ? "head -10000|" : "";
31
32 # More RAM vs more disk I/O tradeoff parameters, does not change
33 # functionality. True will always use more RAM at the benefit of less
34 # temporary files, and is adviced when possible
35 my $SORT_REVERSE_CONCURRENTLY = 1;
36
37 use English;
38 use DB_File;
39 use Storable;
40 use File::Path;
41 use File::Basename;
42 use Packages::CommonCode qw(:all);
43 use Packages::Config qw( $TOPDIR $DBDIR @ARCHIVES @SUITES @ARCHITECTURES );
44 &Packages::Config::init( './' );
45
46 my @archives = @ARCHIVES;
47 my @suites = @SUITES;
48 my @archs = @ARCHITECTURES;
49 my %deborts_hash;
50 @deborts_hash{qw( avr32 m68k powerpcspe sh4 sparc64 )} = ();
51
52 $DBDIR .= "/contents";
53 mkdirp( $DBDIR );
54
55 for my $suite (@suites) {
56     for my $arch (@archs) {
57
58         my $filelist_db = "$DBDIR/filelists_${suite}_${arch}.db";
59         my $dbtime = (stat $filelist_db)[9];
60         my %packages_contents = ();
61         my %packages_contents_nr = ();
62         my %packages_contents_lastword = ();
63
64         my $extra = "";
65         $extra = "|sort" if $SORT_REVERSE_CONCURRENTLY;
66
67         open REVERSED, "$extra>$DBDIR/reverse.tmp"
68             or die "Failed to open output reverse file: $!";
69
70         my $changed = 0;
71         for my $archive (@archives) {
72
73             my $filename = "$TOPDIR/archive/$archive/$suite/Contents-$arch.gz";
74             next unless -f $filename;
75             # Note: ctime, because mtime is set back via rsync
76             my $ftime = (stat $filename)[10];
77             next if defined $dbtime and $dbtime > $ftime;
78             print "$archive/$suite/$arch needs update\n";
79             $changed++;
80         }
81         if ($changed) {
82             for my $archive (@archives) {
83
84                 my $filename = "$TOPDIR/archive/$archive/$suite/Contents-$arch.gz";
85                 next unless -f $filename;
86                 print "Reading $archive/$suite/$arch...\n";
87
88                 open CONT, "zcat $filename|$what"
89                     or die $!;
90                 while (<CONT>) { last if /^FILE/mo; }
91                 if (eof(CONT)) { # no header found
92                     close CONT; # explicit close to reset $.
93                     open CONT, "zcat $filename|$what";
94                 }
95                 while (<CONT>) {
96                     my $data = "";
97                     my %data = ();
98                     chomp;
99                     print "Doing line ".($NR/1000)."k (out of approx 2.0M)\n"
100                         if $NR % 250000 == 0;
101                     /^(.+?)\s+(\S+)$/o;
102                     my ($file, $value) = ($1, $2);
103                     $value =~ s#[^,/]+/##og;
104                     my @packages = split m/,/, $value;
105                     for (@packages) {
106                         $packages_contents_nr{$_}++;
107                         my $lw = $packages_contents_lastword{$_} || "\0";
108                         my $i=0;
109                         while (substr($file,$i,1) eq substr($lw,$i++,1)) {}
110                         $i--;
111                         $i = 255 if $i > 255;
112                         $packages_contents{$_} .= pack "CC/a*", ($i, substr($file, $i));
113                         $packages_contents_lastword{$_} = "$file\0";
114                     }
115                     # Searches are case-insensitive
116                     (my $nocase = $file) =~ tr [A-Z] [a-z];
117                     my $case = ($nocase eq $file) ? '-' : $file;
118
119                     print REVERSED (reverse $nocase)."\0".$case."\0".
120                         (join ":$arch\0", @packages).":$arch\n";
121                 }
122                 close CONT;
123
124             }
125             close REVERSED;
126
127             print "Sorting reverse list if needed\n";
128             system("cd $DBDIR && sort reverse.tmp > reverse.sorted &&".
129                    " mv reverse.{sorted,tmp}") == 0
130                    or die "Failed to sort reverse"
131                    unless $SORT_REVERSE_CONCURRENTLY;
132
133             print "Writing filelist db\n";
134             tie my %packages_contents_db, "DB_File", "$filelist_db.new",
135             O_RDWR|O_CREAT, 0666, $DB_BTREE
136                 or die "Error creating DB: $!";
137             while (my ($k, $v) = each(%packages_contents)) {
138                 $packages_contents_db{$k} = (pack "L", $packages_contents_nr{$k})
139                     . $v;
140             }
141             untie %packages_contents_db;
142
143             rename("$DBDIR/reverse.tmp", "$DBDIR/reverse_${suite}_${arch}.txt");
144
145             activate($filelist_db);
146             #FIXME: hardcoded archs. (debports has no contrib/non-free)
147             if (not exists $deborts_hash{$arch}) {
148                 system("ln", "-sf", basename($filelist_db),
149                        "$DBDIR/filelists_${suite}_all.db") == 0
150                            or die "Oops";
151             }
152         }
153     }
154
155     my $go = 0;
156     my $suite_mtime = (stat "$DBDIR/reverse_$suite.db")[9];
157     for my $file (glob "$DBDIR/reverse_${suite}_*.txt") {
158         $go = 1 if not defined $suite_mtime
159             or $suite_mtime < (stat $file)[9];
160     }
161     next unless $go;
162
163     print "Merging reverse path lists for ${suite}...\n";
164
165     open MERGED, "-|", "sort -m $DBDIR/reverse_${suite}_*.txt"
166         or die "Failed to open merged list";
167     open FILENAMES, ">", "$DBDIR/filenames_$suite.txt.new"
168         or die "Failed to open filenames list";
169     tie my %reverse_path_db, "DB_File", "$DBDIR/reverse_${suite}.db.new",
170         O_RDWR|O_CREAT, 0666, $DB_BTREE
171         or die "Error creating DB: $!";
172
173     my $lastpath = my $lastcasepath = my $lastfile = "";
174     my %matches = ();
175     while (<MERGED>) {
176         print "Doing line ".($NR/1000000)."M (out of approx. 20M)\n"
177             if $NR % 1000000 == 0;
178         chomp;
179         my @line = split m/\0/o, $_;
180         my $revpath = shift @line;
181         my $casepath = shift @line;
182         if ($revpath ne $lastpath) {
183             # Wrap: Do useful stuff with this ($lastpath, @matches)
184             if ($lastpath ne "") {
185                 my @matches;
186                 while (my ($k, $v) = each %matches) {
187                     push @matches, join("\0", $k, @$v);
188                 }
189                 $reverse_path_db{$lastpath} = join "\1", @matches;
190                 %matches = ();
191             }
192             $lastpath =~ s,/.*,,o;
193             if ($lastfile ne $lastpath) {
194                 $lastfile = $lastpath;
195                 print FILENAMES (reverse $lastfile)."\n";
196             }
197             #
198             $lastpath = $revpath;
199             $lastcasepath = $casepath;
200             $matches{$casepath} = \@line;
201             next;
202 #       } elsif ($lastcasepath ne "" and $casepath ne $lastcasepath) {
203 #           warn reverse($revpath)." has more than one casepath: $casepath $lastcasepath\n";
204         }
205         push @{$matches{$casepath}}, @line;
206     }
207     # Note: do useful stuff here too, for out last entry. Maybe prevent this by
208     # adding a fake ultimate entry?
209     {
210         my @matches;
211         while (my ($k, $v) = each %matches) {
212             push @matches, join("\0", $k, @$v);
213         }
214         $reverse_path_db{$lastpath} = join "\1", @matches;
215     }
216
217     untie %reverse_path_db;
218     close FILENAMES;
219     close MERGED;
220
221     activate("$DBDIR/filenames_$suite.txt");
222     activate("$DBDIR/reverse_$suite.db");
223 }
224
225 # vim: set ts=4