4 # Copyright (C) 2004-2006 Frank Lichtenheld <frank@lichtenheld.de>
6 # The code is based on the old search_packages.pl script that
9 # Copyright (C) 1998 James Treacy
10 # Copyright (C) 2000, 2001 Josip Rodin
11 # Copyright (C) 2001 Adam Heath
12 # Copyright (C) 2004 Martin Schulze
14 # This program is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 1 of the License, or
17 # (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41 package Packages::Search;
54 our @ISA = qw( Exporter );
56 our @EXPORT_OK = qw( read_entry read_entry_all read_entry_simple
57 read_src_entry read_src_entry_all find_binaries
58 do_names_search do_fulltext_search
60 our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
64 our $too_many_hits = 0;
67 my ($hash, $key, $results, $non_results, $opts) = @_;
68 my ($virt, $result) = split /\000/o, $hash->{$key} || "-\01-", 2;
70 my %virt = split /\01/o, $virt;
71 while (my ($suite, $provides) = each %virt) {
72 next if $suite eq '-';
73 if ($opts->{h_suites}{$suite}) {
74 push @$results, [ $key, "-", $suite, 'virtual', 'v', 'v', 'v', 'v',
77 push @$non_results, [ $key, "-", $suite, 'virtual', 'v', 'v', 'v', 'v',
82 foreach (split /\000/o, $result) {
83 my @data = split ( /\s/o, $_, 8 );
84 debug( "Considering entry ".join( ':', @data), 2) if DEBUG;
85 if ($opts->{h_suites}{$data[1]}
86 && ($opts->{h_archs}{$data[2]} || $data[2] eq 'all')
87 && $opts->{h_sections}{$data[3]}) {
88 debug( "Using entry ".join( ':', @data), 2) if DEBUG;
89 push @$results, [ $key, @data ];
91 push @$non_results, [ $key, @data ];
96 my ($hash, $key, $results, $opts) = @_;
98 read_entry_all( $hash, $key, $results, \@non_results, $opts );
101 #FIXME: make configurable
102 my %fallback_suites = (
103 'stable-backports' => 'stable',
104 'stable-volatile' => 'stable',
105 experimental => 'unstable' );
107 sub read_entry_simple {
108 my ($hash, $key, $archives, $suite) = @_;
109 # FIXME: drop $archives
111 my ($virt, $result) = split /\000/o, $hash->{$key} || "-\01-\0", 2;
112 my %virt = split /\01/o, $virt;
113 debug( "read_entry_simple: key=$key, archives=".
114 join(" ",(keys %$archives)).", suite=$suite", 1) if DEBUG;
115 debug( "read_entry_simple: virt=".join(" ",(%virt)), 2) if DEBUG;
116 # FIXME: not all of the 2^4=16 combinations of empty(results),
117 # empty(virt{suite}), empty(fb_result), empty(virt{fb_suite}) are dealt
118 # with correctly, but it's adequate enough for now
119 return [ $virt{$suite} ] unless defined $result;
120 foreach (split /\000/o, $result) {
121 my @data = split ( /\s/o, $_, 8 );
122 debug( "use entry: @data", 2 ) if DEBUG && $data[1] eq $suite;
123 return [ $virt{$suite}, @data ] if $data[1] eq $suite;
125 if (my $fb_suite = $fallback_suites{$suite}) {
126 my $fb_result = read_entry_simple( $hash, $key, $archives, $fb_suite );
127 my $fb_virt = shift(@$fb_result);
128 $virt{$suite} .= $virt{$suite} ? " $fb_virt" : $fb_virt if $fb_virt;
129 return [ $virt{$suite}, @$fb_result ] if @$fb_result;
131 return [ $virt{$suite} ];
134 sub read_src_entry_all {
135 my ($hash, $key, $results, $non_results, $opts) = @_;
136 my $result = $hash->{$key} || '';
137 debug( "read_src_entry_all: key=$key", 1) if DEBUG;
138 foreach (split /\000/o, $result) {
139 my @data = split ( /\s/o, $_, 6 );
140 debug( "Considering entry ".join( ':', @data), 2) if DEBUG;
141 if ($opts->{h_archives}{$data[0]}
142 && $opts->{h_suites}{$data[1]}
143 && $opts->{h_sections}{$data[2]}) {
144 debug( "Using entry ".join( ':', @data), 2) if DEBUG;
145 push @$results, [ $key, @data ];
147 push @$non_results, [ $key, @data ];
152 my ($hash, $key, $results, $opts) = @_;
154 read_src_entry_all( $hash, $key, $results, \@non_results, $opts );
156 sub do_names_search {
157 my ($keywords, $packages, $postfixes, $read_entry, $opts,
158 $results, $non_results) = @_;
160 my $first_keyword = lc shift @$keywords;
161 @$keywords = map { lc $_ } @$keywords;
163 my ($key, $prefixes) = ($first_keyword, '');
165 $postfixes->seq( $key, $prefixes, R_CURSOR );
166 while (index($key, $first_keyword) >= 0) {
167 if ($prefixes =~ /^\001(\d+)/o) {
168 debug( "$key has too many hits", 2 ) if DEBUG;
169 $too_many_hits += $1;
172 foreach (split /\000/o, $prefixes) {
173 $_ = '' if $_ eq '^';
175 foreach my $k (@$keywords) {
176 next PREFIX unless $word =~ /\Q$k\E/;
178 debug( "add word $word", 2) if DEBUG;
182 last if $postfixes->seq( $key, $prefixes, R_NEXT ) != 0;
183 last if $too_many_hits or keys %pkgs >= 100;
186 my $no_results = keys %pkgs;
187 if ($too_many_hits || ($no_results >= 100)) {
188 $too_many_hits += $no_results;
189 %pkgs = ( $first_keyword => 1 ) unless @$keywords;
191 foreach my $pkg (sort keys %pkgs) {
192 &$read_entry( $packages, $pkg, $results, $non_results, $opts );
195 sub do_fulltext_search {
196 my ($keywords, $file, $did2pkg, $packages, $read_entry, $opts,
197 $results, $non_results) = @_;
199 # NOTE: this needs to correspond with parse-packages!
201 foreach my $keyword (@$keywords) {
202 $keyword =~ tr [A-Z] [a-z];
203 if ($opts->{exact}) {
204 $keyword = " $keyword ";
206 $keyword =~ s/[(),.-]+//og;
207 $keyword =~ s;[^a-z0-9_/+]+; ;og;
210 my $first_keyword = shift @tmp;
215 # fgrep is seriously faster than using perl
216 open DESC, '-|', 'fgrep', '-n', '--', $first_keyword, $file
217 or die "couldn't open $file: $!";
220 foreach my $k (@$keywords) {
221 next LINE unless /\Q$k\E/;
225 debug( "Matched line $_", 2) if DEBUG;
226 my $result = $did2pkg->{$nr};
227 foreach (split /\000/o, $result) {
228 my @data = split /\s/, $_, 3;
229 # debug ("Considering $data[0], arch = $data[2]", 3) if DEBUG;
230 # next unless $data[2] eq 'all' || $opts->{h_archs}{$data[2]};
231 # debug ("Ok", 3) if DEBUG;
232 $numres++ unless $tmp_results{$data[0]}++;
234 last if $numres > 100;
237 $too_many_hits++ if $numres > 100;
240 foreach my $pkg (keys %tmp_results) {
241 &$read_entry( $packages, $pkg, $results, $non_results, $opts );
246 my ($pkg, $archive, $suite, $src2bin) = @_;
248 my $bins = $src2bin->{$pkg} || '';
250 foreach (split /\000/o, $bins) {
251 my @data = split /\s/, $_, 5;
253 debug( "find_binaries: considering @data", 3 ) if DEBUG;
254 if (($data[0] eq $archive)
255 && ($data[1] eq $suite)) {
257 debug( "find_binaries: using @data", 3 ) if DEBUG;
261 return [ keys %bins ];