prtwash.pl: allow user to toggle the parsing of pkgmk.conf

This commit is contained in:
John McQuah 2022-05-29 12:40:03 -04:00
parent 0485af9be2
commit 3bf8fd6025
2 changed files with 68 additions and 52 deletions

View File

@ -36,34 +36,28 @@ All items not included in the previous list WILL be deleted
You can choose to delete some of the above by passing the proper
option:
.TP
.B
-p
also remove the built package (current version)
.B \-p
remove the built package (current version)
.TP
.B
-b
also remove other built packages (older versions)
.B \-b
remove older builds (any package tarball that does NOT match
the current version)
.TP
.B
-d
also remove the add-on files provided by CRUX maintainers
.B \-d
remove add-on files provided by CRUX maintainers
(patches and initscripts downloaded with \fBports -u\fP)
.TP
.B
-s
also remove the upstream sources (those NOT obtained via \fBports -u\fP)
.B \-s
remove upstream sources (those NOT obtained via \fBports -u\fP)
.TP
.B
-t
.B \-t
(test mode) display the target files without deleting
.TP
.B
-q
.B \-q
(quiet mode) suppress messages about files that are kept;
basically the same as \fBprtwash [OPTIONS] | grep -v "keeping"\fP
.TP
.B
-a
.B \-a
(automatic mode) take the port collections from the
/etc/prt-get.conf file and do a recursive cleaning;
if omitted, the command line must contain at least one <path> specifying
@ -71,12 +65,14 @@ a valid port directory
.PP
Misc options
.TP
.B
-h
.B \-\-parse\-pkgmk\-conf
Revert to the v0.9 behaviour of parsing /etc/pkgmk.conf to determine the
compression mode of built packages
.TP
.B \-h
Display usage information
.TP
.B
-v
.B \-v
Display version
.SH ENVIRONMENT
In automatic mode, \fBprtwash\fP gets a list of repositories from
@ -93,11 +89,24 @@ Sources under CRUX version control are only deleted if you pass the
option \fB\-d\fP. Sources from upstream are only deleted if you pass the
option \fB\-s\fP.
.PP
The current version of the package is only deleted if you pass the
option \fB\-p\fP. Older versions of the package are only deleted if you
pass the option \fB\-b\fP. Note that if the compression mode defined
in /etc/pkgmk.conf has been changed since you last built the package,
\fBprtwash\fP will not recognize the tarball in its regexp search.
You can also delete the built package (current version) by passing
the option \fB\-p\fP. To select only older versions for deletion,
pass the option \fB\-b\fP instead. These two options operate independently,
allowing you to target four distinct subsets (including the empty set)
for deletion.
.PP
By passing the option \fB\-\-parse\-pkgmk\-conf\fP, you can modify the behaviour
of the pattern matching performed to detect built packages. By default, the
regexp searches are done with the patterns
.B /${name}#${version}.*.pkg.tar.(gz|lz|xz|bz2)/
and
.B /${name}#.*pkg.tar.(gz|lz|xz|bz2)/
using the variables defined in the \fBPkgfile\fP(5). Passing the option
.B \-\-parse\-pkgmk\-conf
will result in a more narrow regexp search, allowing the deletion of any package
tarball that uses a different compression algorithm than the one currently defined
in /etc/pkgmk.conf (which is how the v0.9 bash script worked).
.SH COMPARISON WITH OTHER UTILITIES
Because \fBprtwash\fP reads the location of port collections from \fB/etc/prt-get.conf\fP(5),
you can easily control which collections are cleaned in automatic mode by commenting

View File

@ -18,7 +18,7 @@ our @portdirs;
our $argports;
my $version = 1.2.1;
our %options = ( oldver=>0, pkgtoo=>0, srctoo=>0, addons=>0,
dryrun=>0, auto=>0, quiet=>0 );
dryrun=>0, auto=>0, quiet=>0, parse_pkgmk=>0 );
#################### main routine ################################
parse_args();
@ -46,9 +46,8 @@ if ($options{auto} == 1) {
#################### subroutines #################################
sub parse_args {
while ( my $arg = pop(@ARGV) ) {
next if (-f $arg); # no filenames, only directories and options
if ($arg eq "-b") {
foreach my $arg (@ARGV) {
if ($arg eq "-b") {
$options{oldver} = 1;
} elsif ($arg eq "-p") {
$options{pkgtoo} = 1;
@ -62,11 +61,16 @@ sub parse_args {
$options{auto} = 1;
} elsif ($arg eq "-q") {
$options{quiet} = 1;
} elsif ($arg eq "--parse-pkgmk-conf") {
$options{parse_pkgmk} = 1;
} elsif ($arg eq "-v") {
print "$version\n";
exit(0);
} elsif (-d $arg) {
} elsif ((-d $arg) || # false for symlink to a portdir, so a
(-f "$arg/Pkgfile")) { # followup test is performed to catch those
push (@portdirs, $arg);
} elsif (-f $arg) {
print "WARN: ignoring invalid port directory $arg\n";
} else {
print_usage();
}
@ -124,7 +128,7 @@ sub parse_pkgfile {
return \$name, \$version, \$release, \@source, \@renames;
}
sub keeplist { # remember to pop off the last element for regex purposes
sub keeplist { # remember to pop off the last two elements for regex purposes
my $port = shift;
my @keepers = ("Pkgfile",".footprint",".signature");
push (@keepers,"pre-install","post-install","README","README.md",
@ -140,7 +144,7 @@ sub keeplist { # remember to pop off the last element for regex purposes
my $i; my $si; my $ki;
ENTRY: for ($i=0; $i<=$#source; $i++) {
$si = $source[$i];
$ki = ($renames[$i] eq "SKIP" or $renames[$i] eq "") ? basename($si) :
$ki = ("$renames[$i]" eq "SKIP" or "$renames[$i]" eq "") ? basename($si) :
$renames[$i];
if ($si =~ /^remote:/) {
next ENTRY if ($options{srctoo}==1);
@ -150,21 +154,23 @@ sub keeplist { # remember to pop off the last element for regex purposes
push(@keepers, $ki);
}
}
push (@keepers, $name.'#'.$version.'-'.$release.".pkg.tar.$compression_mode") if $options{pkgtoo}==0;
push (@keepers, $name."#.*pkg.tar.$compression_mode");
push (@keepers, "$name#$version.*\.pkg\.tar\.$compression_mode");
push (@keepers, "$name#.*\.pkg\.tar\.$compression_mode");
return @keepers;
}
sub get_compress {
my $conf = "/etc/pkgmk.conf";
$compression_mode = "gz";
open(CONFIG,$conf) or return $compression_mode;
while(<CONFIG>) {
$compression_mode = $1 if m/^[\t ]*PKGMK_COMPRESSION_MODE=(.*)\n/;
my $suffix = "(gz|lz|xz|bz2)";
if ($options{parse_pkgmk} == 1) {
my $conf = "/etc/pkgmk.conf";
open(CONFIG,$conf) or return $suffix;
while(<CONFIG>) {
$suffix = $1 if m/^[\t ]*PKGMK_COMPRESSION_MODE=(.*)\n/;
}
close(CONFIG);
$suffix =~ s/"//g; # ensure no double-quotes remain (thanks jaeger)
}
close(CONFIG);
$compression_mode =~ s/"//g; # ensure no double-quotes remain (thanks jaeger)
return $compression_mode;
return $suffix;
}
sub do_wash {
@ -174,16 +180,18 @@ sub do_wash {
print "WARN: no Pkgfile found in $port. Skipping.\n";
return;
} else {
my @keepers = keeplist($port);
my $nevermatch = "ReallyLongStringThatWouldNeverMatchAnyBuiltPackage";
my $pkgrex = pop(@keepers);
$pkgrex = ($options{oldver} == 1 or $options{pkgtoo} == 1) ? $nevermatch : $pkgrex;
my @keepers = keeplist($port); my $iswanted;
my $allbuilds = pop(@keepers);
my $currbuild = pop(@keepers);
opendir (DIR,$port) or return;
print "=====> washing $port\n" unless $options{quiet} == 1;
foreach my $f (sort(readdir(DIR))) {
next if ($f eq '.' or $f eq '..');
$f =~ s/\+/\\\+/g; # plus sign in filenames interferes with regex search
if ((grep /$f/, @keepers) or ($f =~ /$pkgrex/)) {
$iswanted = ( grep (/$f/, @keepers ) >= 1 );
if ($iswanted or ($options{pkgtoo}==0)*($f =~ /$currbuild/)
or ($options{oldver}==0)*($f =~ /$allbuilds/)*($f !~ /$currbuild/)) {
print "... keeping file $port/$f.\n" unless $options{quiet} == 1;
} else {
remove ("$port/$f");
@ -194,8 +202,7 @@ sub do_wash {
}
sub getportdirs { # returns scalar references to two arrays
my @basedirs;
my @localports;
my @basedirs; my @localports;
my $conf = "/etc/prt-get.conf";
open(PORTS, $conf) or die "could not open $conf";
@ -211,7 +218,7 @@ sub getportdirs { # returns scalar references to two arrays
push @basedirs, $line if (-d $line);
} else {
my @a = split(/:/, $line);
my @b = split(/,/, @a[1]);
my @b = split(/,/, $a[1]);
while ( my $c = pop @b ) {
my $port = $a[0] . "/" . $c;
push @localports, $port if (-d $port);