Skip to content

Commit 5516a1a

Browse files
committed
apt-file: Add --stream-results parameter
Signed-off-by: Niels Thykier <[email protected]>
1 parent cd7579f commit 5516a1a

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

apt-file

+29-7
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ sub do_grep($$) {
296296
my $matches = 0;
297297
my $quick_regexp = escape_parens($regexp);
298298
my $fd = open_data_pipeline($data);
299+
my $stream_results = $Conf->{streaming};
300+
tty_human_status('') if $stream_results;
299301
debug(1, "Pipeline open, waiting for input");
300302
while (<$fd>) {
301303

@@ -305,13 +307,18 @@ sub do_grep($$) {
305307
next if !( ( $fname, $pkgs ) = /$regexp/o );
306308

307309
debug_line ".";
308-
# Put leading slash on file name
309-
push(@{ $ret->{"/$fname"} },
310-
# ... and extract the package name
311-
map { basename($_) } split(m/,/, $pkgs)
312-
);
313-
if (++$matches % 10 == 0) {
314-
tty_human_status("Searching, found $matches results so far ...");
310+
311+
if ($stream_results) {
312+
stream_results("/$fname", map { basename($_) } split(m/,/, $pkgs));
313+
} else {
314+
# Put leading slash on file name
315+
push(@{ $ret->{"/$fname"} },
316+
# ... and extract the package name
317+
map { basename($_) } split(m/,/, $pkgs)
318+
);
319+
if (++$matches % 10 == 0) {
320+
tty_human_status("Searching, found $matches results so far ...");
321+
}
315322
}
316323
}
317324
close($fd);
@@ -321,9 +328,22 @@ sub do_grep($$) {
321328
error("A subprocess exited uncleanly (raw: $?)");
322329
}
323330
debug(1, 'Read all input');
331+
return if $stream_results;
324332
return reverse_hash($ret);
325333
}
326334

335+
sub stream_results {
336+
my ($filename, @pkgs) = @_;
337+
if ($Conf->{package_only}) {
338+
print map { "$_\n" } @pkgs;
339+
} else {
340+
for my $pkg (@pkgs) {
341+
print "$pkg: $filename\n";
342+
}
343+
}
344+
return;
345+
}
346+
327347
sub escape_parens {
328348
my $pattern = shift;
329349

@@ -577,6 +597,7 @@ Other options:
577597
--config -c <file> Parse the given APT config file [R]
578598
--option -o <A::B>=<V> Set the APT config option A::B to "V" [R]
579599
--package-only -l Only display packages name
600+
--stream-results Emit results immediately (without deduplication)
580601
--verbose -v run in verbose mode [R]
581602
--help -h Show this help.
582603
-- End of options (necessary if pattern
@@ -606,6 +627,7 @@ sub get_options() {
606627
"fixed-string|F" => \$Conf->{fixed_strings},
607628
"from-file|f" => \$Conf->{from_file},
608629
"from-deb|D" => \$Conf->{from_deb},
630+
'stream-results' => \$Conf->{streaming},
609631
'filter-suites=s' => sub { _set_option('in-suites', CONFIG_SEARCH_FILTER_SUITES, $_[1]); },
610632
'filter-origins=s' => sub { _set_option('from-origin', CONFIG_SEARCH_FILTER_ORIGINS, $_[1]); },
611633
'config-file|c=s@' => \&_parse_apt_config_file,

apt-file.pod

+15
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,21 @@ Ignore case when searching for pattern.
179179

180180
Only display package name; do not display file names.
181181

182+
=item --stream-results
183+
184+
I<This is a special-usage option useful for dealing with searches that
185+
produce a high number of matches (10 000+) or/and automated processing
186+
of results.>
187+
188+
Disable deduplication logic and immediately emit a result when a match
189+
is found.
190+
191+
This can greatly reduce the memory requirements for apt-file when
192+
processing searches with many matches. It will also reduce the time
193+
until the first match is emitted, which can be useful if matches can
194+
be processed as they are discovered and the consumer can handle
195+
duplicated matches.
196+
182197
=item -o, --option APT::Option=Value
183198

184199
Set a Configuration Option; This will set an arbitrary configuration

debian/changelog

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
apt-file (3.2.3) UNRELEASED; urgency=medium
1+
apt-file (3.3) UNRELEASED; urgency=medium
22

3+
[ Debian Janitor ]
34
* Drop no longer supported add-log-mailing-address setting from
45
debian/changelog.
56

7+
[ Niels Thykier ]
8+
* Add new --stream-results argument to apt-file. This option
9+
will make apt-file emit results as soon as they are found
10+
but also emit duplicate results in many cases. It can greatly
11+
reduce memory usage and runtime spent up to first result.
12+
613
-- Debian Janitor <[email protected]> Fri, 27 Dec 2019 01:58:57 +0000
714

815
apt-file (3.2.2) unstable; urgency=medium

0 commit comments

Comments
 (0)