--- Begin Message ---
- To: Ben Gertzfield <che@debian.org>
- Subject: apt package
- From: Paul Jimenez <pj@place.org>
- Date: Wed, 16 Feb 2000 05:15:18 -0600
- Message-id: <200002161115.FAA17986@zachs.place.org>
If you're still maintaining apt, this is for you; if not, please forward
it appropriately. Thanks!
Perhaps there's a better way to do this, but perhaps not; apt-get has
freed me from the drudgery of dselect, now if only I had a way to search
the package lists from the commandline...
so here's a first pass I call apt-list; hopefully someone will make it
better, but it's my first swing at it...
let me know what you think, if it's good or bad or indifferent or
especially if I'm reinventing the wheel...
Thanks!
--pj
#!/usr/bin/perl
#
# apt-list [-o] <field>:<match> [...]
# where <field> is one of
# Package, Priority, Section, Version, Maintainer,
# Architecture, Depends, Filename, Size, MD4sum,
# Desc, Description
#
# drawing data from /var/state/apt/lists/*_Packages
# that looks like:
#
# Package: 3c5x9utils
# Version: 1.1-2
# Priority: extra
# Section: net
# Maintainer: Piotr Roszatycki <dexter@debian.org>
# Depends: libc6 (>= 2.1)
# Architecture: i386
# Filename: dists/frozen/main/binary-i386/net/3c5x9utils_1.1-2.deb
# Size: 8396
# MD5sum: 795fe3f782c231954d11d1706e3103d5
# Description: Configuration and diagnostic utils for 3Com 5x9 cards.
# 3c5x9setup : EEPROM setup and diagnostic program for the 3Com 3c5x9 series ethercards
# el3diag : Diagnostic program for 3c509 and 3c579 ethercards
# installed-size: 26
#
# Notes:
# 2000/2/16 pj@place.org
# Creation. Working, but !Not! Fast; not sure how to speed it up
# (other than rewriting it in C)
#
my $pkgdir = "/var/state/apt/lists/";
my %validfield = (
"Package" => 1,
"Priority" => 1,
"Section" => 1,
"Version" => 1,
"Maintainer" => 1,
"Architecture" => 1,
"Depends" => 1,
"Filename" => 1,
"Size" => 1,
"MD4sum" => 1,
"Desc" => 1,
"Description" => 1
);
# parse args
&showhelp(0) if (!@ARGV);
my $curarg = 0;
if ($ARGV[0] eq "-o") {
$orflag = 1;
$startarg++;
} elsif ($ARGV[0] eq "-a") {
$andflag = 1;
$startarg++;
} else {
$andflag= 1;
}
while ($curarg <= $#ARGV) {
if (($ARGV[$curarg] =~ /^(.*):(.*)$/) && $validfield{$1}) {
my $field = $1;
my $value = $2;
$conditions{$field} = $value;
} else {
print "'".$ARGV[$curarg]."' is not a valid option.\n";
&showhelp(1);
}
$curarg++;
}
# get the list of package files
opendir PKGDIR, $pkgdir;
my @filelist = grep /Packages$/, grep !/^\.\.?$/, readdir PKGDIR;
closedir PKGDIR;
# scan each file
my $filename;
my @toshow = ();
foreach $filename (@filelist) {
my @thisrecord = ();
my $shouldshow = 0;
my $failedand = 0;
open(FILEIN, "<${pkgdir}/${filename}") || die "Can't open $filename : $! \n";
while (<FILEIN>) {
if (/^$/) { # EORecord; clear it out
if ($shouldshow && ($orflag || !$failedand)) {
# @toshow = (@toshow, "\n", @thisrecord);
print @thisrecord;
print "\n";
}
@thisrecord = ();
$shouldshow = 0;
$failedand = 0;
} elsif ($conditions{"Desc"} && (/^Description:(.*)$/)) {
if ($1 =~ /$conditions{"Desc"}/) {
$shouldshow = 1;
} else {
$failedand = 1;
}
} elsif ($conditions{"Description"} && (/^Description:(.*)$/)) {
my $found = 0;
my $line = $1;
while ($line =~ /^ /) {
if ($line =~ /$conditions{"Description"}/) {
$found = 1;
}
$line = <FILEIN>;
}
if ($found) {
$shouldshow = 1;
} else {
$failedand = 1;
}
} elsif ((/^([^:]*):(.*)$/) && $conditions{$1}) {
if ($2 =~ /$conditions{$1}/) {
$shouldshow = 1;
} else {
$failedand = 1;
}
}
@thisrecord = (@thisrecord, $_);
}
close(FILEIN);
}
print @toshow;
print "\n";
sub showhelp {
my ($exitlvl) = @_;
print <<EOUsage;
apt-list [-o] field:val [field:val] ...
looks for <field> matching <val> where
<field> is one of
Package, Priority, Section, Version, Maintainer, Architecture,
Depends, Filename, Size, MD4sum, Desc, Description
and <val> is a perl regular expression (escaped as necessary)
-o means 'OR' all the specified conditions together instead of
(the default of) 'AND'ing them.
EOUsage
exit($exitlvl);
}
--- End Message ---