lintpkgsrc: cleanup

Mostly remove redundant punctuation around 'push' and 'defined'.
This commit is contained in:
rillig 2022-08-10 07:12:52 +00:00
parent 180813544f
commit 31ad82e05c
2 changed files with 131 additions and 141 deletions

View file

@ -1,6 +1,6 @@
#!@PERL5@ #!@PERL5@
# $NetBSD: lintpkgsrc.pl,v 1.68 2022/08/10 06:08:15 rillig Exp $ # $NetBSD: lintpkgsrc.pl,v 1.69 2022/08/10 07:12:52 rillig Exp $
# Written by David Brownlee <abs@netbsd.org>. # Written by David Brownlee <abs@netbsd.org>.
# #
@ -52,13 +52,13 @@ sub pkgname($self) {
} }
sub var($self, $key, $val = undef) { sub var($self, $key, $val = undef) {
(defined $val) defined $val
? ($self->{vars}->{$key} = $val) ? ($self->{vars}->{$key} = $val)
: $self->{vars}->{$key}; : $self->{vars}->{$key};
} }
sub vars($self) { sub vars($self) {
keys %{$self->{vars}}; keys $self->{vars}->%*;
} }
# Pkgs collects all versions of a given PKGBASE, e.g. apache-1.3.27 and # Pkgs collects all versions of a given PKGBASE, e.g. apache-1.3.27 and
@ -82,7 +82,7 @@ sub pkgbase($self) {
# Returns all available versions of the package, in decreasing # Returns all available versions of the package, in decreasing
# alphabetical(!) order. # alphabetical(!) order.
sub versions($self) { sub versions($self) {
return sort { $b cmp $a } keys %{$self->{pkgvers}}; reverse sort keys $self->{pkgvers}->%*;
} }
sub add($self, $pkgbase, $pkgversion) { sub add($self, $pkgbase, $pkgversion) {
@ -95,10 +95,9 @@ sub add($self, $pkgbase, $pkgversion) {
# pkgver($pkgversion) returns the PkgVer, or undef. # pkgver($pkgversion) returns the PkgVer, or undef.
sub pkgver($self, $pkgversion = undef) { sub pkgver($self, $pkgversion = undef) {
my $pkgvers = $self->{pkgvers}; my $pkgvers = $self->{pkgvers};
if (@_ > 1) { defined $pkgversion
return $pkgvers->{$pkgversion}; ? $pkgvers->{$pkgversion}
} : sort { $b->pkgversion cmp $a->pkgversion } values %$pkgvers;
return sort { $b->pkgversion cmp $a->pkgversion } values %{$pkgvers};
} }
# XXX: Returns the alphabetically(!) highest PkgVer. # XXX: Returns the alphabetically(!) highest PkgVer.
@ -116,14 +115,12 @@ sub new($class) {
} }
sub add($self, $pkgbase, $pkgversion) { sub add($self, $pkgbase, $pkgversion) {
if (!$self->{$pkgbase}) { my $pkgs = ($self->{$pkgbase} ||= Pkgs->new($pkgbase));
$self->{$pkgbase} = Pkgs->new($pkgbase); $pkgs->add($pkgbase, $pkgversion);
}
$self->{$pkgbase}->add($pkgbase, $pkgversion);
} }
sub numpkgver($self) { sub numpkgver($self) {
scalar($self->pkgver); scalar $self->pkgver;
} }
# pkgver() returns all PkgVers, sorted by pkgbase, then by version in # pkgver() returns all PkgVers, sorted by pkgbase, then by version in
@ -137,29 +134,26 @@ sub pkgver($self, $pkgbase = undef, $pkgversion = undef) {
if (!defined $pkgbase) { if (!defined $pkgbase) {
my (@pkgvers); my (@pkgvers);
foreach my $pkg ($self->pkgs) { foreach my $pkg ($self->pkgs) {
push(@pkgvers, $pkg->pkgver); push @pkgvers, $pkg->pkgver;
} }
return @pkgvers; return @pkgvers;
} }
my $pkgs = $self->{$pkgbase}; my $pkgs = $self->{$pkgbase};
if (defined $pkgs) { defined $pkgs && defined $pkgversion
return defined $pkgversion ? $pkgs->pkgver($pkgversion)
? $pkgs->pkgver($pkgversion) : defined $pkgs
: $pkgs->pkgver(); ? $pkgs->pkgver
} : ();
return;
} }
# pkgs() returns all Pkgs, sorted by pkgbase. # pkgs() returns all Pkgs, sorted by pkgbase.
# #
# pkgs($pkgbase) returns the Pkgs, or undef. # pkgs($pkgbase) returns the Pkgs, or undef.
sub pkgs($self, $pkgbase = undef) { sub pkgs($self, $pkgbase = undef) {
if (defined $pkgbase) { defined $pkgbase
return $self->{$pkgbase}; ? $self->{$pkgbase}
} else { : sort { $a->pkgbase cmp $b->pkgbase } values %$self;
return sort { $a->pkgbase cmp $b->pkgbase } values %$self;
}
} }
package main; package main;
@ -185,13 +179,11 @@ my (
my $magic_undefined = 'M_a_G_i_C_uNdEfInEd'; my $magic_undefined = 'M_a_G_i_C_uNdEfInEd';
sub debug(@) { sub debug(@) {
($opt{D}) && print STDERR 'DEBUG: ', @_; $opt{D} and print STDERR 'DEBUG: ', @_;
} }
sub verbose(@) { sub verbose(@) {
if (-t STDERR) { -t STDERR and print STDERR @_;
print STDERR @_;
}
} }
sub fail($msg) { sub fail($msg) {
@ -208,7 +200,7 @@ sub listdir($base, $dir = undef) {
my (@list, @thislist); my (@list, @thislist);
$thisdir = $base; $thisdir = $base;
if (defined($dir)) { if (defined $dir) {
$thisdir .= "/$dir"; $thisdir .= "/$dir";
$dir .= '/'; $dir .= '/';
} else { } else {
@ -216,13 +208,13 @@ sub listdir($base, $dir = undef) {
} }
opendir(DIR, $thisdir) || fail("Unable to opendir($thisdir): $!"); opendir(DIR, $thisdir) || fail("Unable to opendir($thisdir): $!");
@thislist = grep(substr($_, 0, 1) ne '.' && $_ ne 'CVS', readdir(DIR)); @thislist = grep { substr($_, 0, 1) ne '.' && $_ ne 'CVS' } readdir(DIR);
closedir(DIR); closedir(DIR);
foreach my $entry (@thislist) { foreach my $entry (@thislist) {
if (-d "$thisdir/$entry") { if (-d "$thisdir/$entry") {
push(@list, listdir($base, "$dir$entry")); push @list, listdir($base, "$dir$entry");
} else { } else {
push(@list, "$dir$entry"); push @list, "$dir$entry";
} }
} }
@list; @list;
@ -239,22 +231,20 @@ sub split_pkgversion($pkgversion) {
$pkgversion = lc($pkgversion); $pkgversion = lc($pkgversion);
# See pkgtools/pkg_install/files/lib/dewey.c. # See pkgtools/pkg_install/files/lib/dewey.c.
my (@temp); my @temp = ($pkgversion =~ s/nb(\d+)//) ? +$1 : 0;
push(@temp, ($pkgversion =~ s/nb(\d+)//) ? +$1 : 0);
foreach my $elem (split(/(pl|pre|rc|beta|alpha|\D)/, $pkgversion)) { foreach my $elem (split(/(pl|pre|rc|beta|alpha|\D)/, $pkgversion)) {
if ($elem =~ /\d/) { if ($elem =~ /\d/) {
push(@temp, +$elem); push @temp, +$elem;
} elsif ($elem eq 'pl' || $elem eq '.' || $elem eq '_') { } elsif ($elem eq 'pl' || $elem eq '.' || $elem eq '_') {
push(@temp, 0); push @temp, 0;
} elsif ($elem eq 'pre' || $elem eq 'rc') { } elsif ($elem eq 'pre' || $elem eq 'rc') {
push(@temp, -1); push @temp, -1;
} elsif ($elem eq 'beta') { } elsif ($elem eq 'beta') {
push(@temp, -2); push @temp, -2;
} elsif ($elem eq 'alpha') { } elsif ($elem eq 'alpha') {
push(@temp, -3); push @temp, -3;
} elsif ('a' le $elem && $elem le 'z') { } elsif ('a' le $elem && $elem le 'z') {
push(@temp, 0); push @temp, 0, ord($elem) - ord('a') + 1;
push(@temp, ord($elem) - ord('a') + 1);
} }
} }
@temp; @temp;
@ -270,26 +260,19 @@ sub pkgversioncmp($va, $op, $vb) {
} }
$cmp ||= $nb_a <=> $nb_b; $cmp ||= $nb_a <=> $nb_b;
if ($op eq '<') { $op eq '<' ? $cmp < 0
$cmp < 0; : $op eq '<=' ? $cmp <= 0
} elsif ($op eq '<=') { : $op eq '>' ? $cmp > 0
$cmp <= 0; : $cmp >= 0;
} elsif ($op eq '>') {
$cmp > 0;
} else {
$cmp >= 0;
}
} }
# Return a copy of $value in which trivial variable expressions are replaced # Return a copy of $value in which trivial variable expressions are replaced
# with their variable values. # with their variable values.
sub expand_var($value, $vars) { sub expand_var($value, $vars) {
while ($value =~ /\$\{([-\w.]+)\}/) { while ($value =~ /\$\{([-\w.]+)\}/) {
if (defined($vars->{$1})) { $value = defined $vars->{$1}
$value = $` . $vars->{$1} . $'; ? "$`$vars->{$1}$'"
} else { : "$`$magic_undefined$'";
$value = $` . $magic_undefined . $';
}
} }
$value; $value;
} }
@ -305,7 +288,6 @@ sub parse_eval_make_false($line, $vars) {
debug("conditional: $test\n"); debug("conditional: $test\n");
# XXX Could do something with target
while ($test =~ /(target|empty|make|defined|exists)\s*\(([^()]+)\)/) { while ($test =~ /(target|empty|make|defined|exists)\s*\(([^()]+)\)/) {
my ($testname, $varname) = ($1, $2); my ($testname, $varname) = ($1, $2);
my $var; my $var;
@ -336,15 +318,16 @@ sub parse_eval_make_false($line, $vars) {
} }
if ($testname eq 'exists') { if ($testname eq 'exists') {
$_ = (-e $varname) ? 1 : 0; $_ = -e $varname ? 1 : 0;
} elsif ($testname eq 'defined') { } elsif ($testname eq 'defined') {
$_ = defined($var) ? 1 : 0; $_ = defined $var ? 1 : 0;
} elsif ($testname eq 'empty') { } elsif ($testname eq 'empty') {
$_ = ((not defined($var) or (length($var) == 0)) ? 1 : 0); $_ = !defined $var || $var eq '' ? 1 : 0;
} else { } else {
# XXX Could do something with target
$_ = 0; $_ = 0;
} }
@ -354,9 +337,9 @@ sub parse_eval_make_false($line, $vars) {
while ($test =~ /([^\s()\|\&]+)\s+(!=|==)\s+([^\s()]+)/) { while ($test =~ /([^\s()\|\&]+)\s+(!=|==)\s+([^\s()]+)/) {
if ($2 eq '==') { if ($2 eq '==') {
$_ = ($1 eq $3) ? 1 : 0; $_ = $1 eq $3 ? 1 : 0;
} else { } else {
$_ = ($1 ne $3) ? 1 : 0; $_ = $1 ne $3 ? 1 : 0;
} }
$test =~ s/[^\s()\|\&]+\s+(!=|==)\s+[^\s()]+/$_/; $test =~ s/[^\s()\|\&]+\s+(!=|==)\s+[^\s()]+/$_/;
} }
@ -427,12 +410,12 @@ sub parse_makefile_line_include($file, $incfile,
my $NEWCURDIR = $incfile; my $NEWCURDIR = $incfile;
$NEWCURDIR =~ s#/[^/]*$##; $NEWCURDIR =~ s#/[^/]*$##;
push(@$incdirs, $NEWCURDIR) push @$incdirs, $NEWCURDIR
unless grep { $_ eq $NEWCURDIR } @$incdirs; unless grep { $_ eq $NEWCURDIR } @$incdirs;
unshift(@$lines, ".CURDIR=" . $vars->{'.CURDIR'}); unshift @$lines, ".CURDIR=" . $vars->{'.CURDIR'};
chomp(my @inc_lines = <FILE>); chomp(my @inc_lines = <FILE>);
unshift(@$lines, @inc_lines); unshift @$lines, @inc_lines;
unshift(@$lines, ".CURDIR=$NEWCURDIR"); unshift @$lines, ".CURDIR=$NEWCURDIR";
close(FILE); close(FILE);
} }
@ -472,8 +455,9 @@ sub expand_modifiers($file, $varname, $left, $subvar, $mods, $right, $vars) {
debug("$file: substitutelist $varname ($result) $subvar (@mods)\n"); debug("$file: substitutelist $varname ($result) $subvar (@mods)\n");
foreach (@mods) { foreach (@mods) {
if (m#(U)(.*)#) { # FIXME: Add '^' anchor.
$result ||= $2; if (m# (U) (.*) #x) {
$result ||= "fallback:$2";
} elsif (m# ([CS]) (.) ([^/\@]+) \2 ([^/\@]*) \2 ([1g]*) #x) { } elsif (m# ([CS]) (.) ([^/\@]+) \2 ([^/\@]*) \2 ([1g]*) #x) {
# TODO: Use non-greedy repetitions above. # TODO: Use non-greedy repetitions above.
# TODO: Properly handle separators other than '/' and '@'. # TODO: Properly handle separators other than '/' and '@'.
@ -525,8 +509,8 @@ sub parse_makefile_vars($file, $cwd = undef) {
chomp(@lines = <FILE>); chomp(@lines = <FILE>);
close(FILE); close(FILE);
push(@incdirs, '.'); push @incdirs, '.';
push(@incdirs, dirname($file)); push @incdirs, dirname($file);
# Some Makefiles depend on these being set # Some Makefiles depend on these being set
if ($file eq '/etc/mk.conf') { if ($file eq '/etc/mk.conf') {
@ -544,7 +528,7 @@ sub parse_makefile_vars($file, $cwd = undef) {
$vars{'.CURDIR'} = getcwd; $vars{'.CURDIR'} = getcwd;
} }
push(@incdirs, $vars{'.CURDIR'}); push @incdirs, $vars{'.CURDIR'};
if ($opt{L}) { if ($opt{L}) {
print "$file\n"; print "$file\n";
} }
@ -560,22 +544,20 @@ sub parse_makefile_vars($file, $cwd = undef) {
# Conditionals # Conditionals
# #
if (m#^ \. \s* if(|def|ndef) \s+ (.*) #x) { if (m#^ \. \s* if(|def|ndef) \s+ (.*) #x) {
my ($type, $false); my $type = $1;
$type = $1;
if ($if_false[$#if_false]) { if ($if_false[$#if_false]) {
push(@if_false, 2); push @if_false, 2;
} elsif ($type eq '') { } elsif ($type eq '') {
# Straight if # Straight if
push(@if_false, parse_eval_make_false($2, \%vars)); push @if_false, parse_eval_make_false($2, \%vars);
} else { } else {
$false = !defined($vars{expand_var($2, \%vars)}); my $false = !defined $vars{expand_var($2, \%vars)};
if ($type eq 'ndef') { if ($type eq 'ndef') {
$false = !$false; $false = !$false;
} }
push(@if_false, $false ? 1 : 0); push @if_false, $false ? 1 : 0;
} }
debug("$file: .if$type (! @if_false)\n"); debug("$file: .if$type (! @if_false)\n");
@ -651,10 +633,9 @@ sub get_default_makefile_vars() {
$default_vars->{OPSYS}, $default_vars->{OPSYS},
$default_vars->{OS_VERSION}, $default_vars->{OS_VERSION},
$default_vars->{MACHINE} $default_vars->{MACHINE}
) = (split); ) = split;
if (!$default_vars->{MACHINE}) { $default_vars->{MACHINE}
die('Unable to extract machine from uname'); or die('Unable to extract machine from uname');
}
# Handle systems without uname -p (NetBSD pre 1.4) # Handle systems without uname -p (NetBSD pre 1.4)
chomp($default_vars->{MACHINE_ARCH} = `uname -p 2>/dev/null`); chomp($default_vars->{MACHINE_ARCH} = `uname -p 2>/dev/null`);
@ -664,18 +645,14 @@ sub get_default_makefile_vars() {
chomp($default_vars->{MACHINE_ARCH} = `sysctl -n hw.machine_arch`); chomp($default_vars->{MACHINE_ARCH} = `sysctl -n hw.machine_arch`);
} }
if (!$default_vars->{MACHINE_ARCH}) { $default_vars->{MACHINE_ARCH} ||= $default_vars->{MACHINE};
$default_vars->{MACHINE_ARCH} = $default_vars->{MACHINE};
}
$default_vars->{OBJECT_FMT} = 'x'; $default_vars->{OBJECT_FMT} = 'x';
$default_vars->{LOWER_OPSYS} = lc($default_vars->{OPSYS}); $default_vars->{LOWER_OPSYS} = lc($default_vars->{OPSYS});
if ($opt{P}) { $default_vars->{PKGSRCDIR} = $opt{P}
$default_vars->{PKGSRCDIR} = realpath($opt{P}); ? realpath($opt{P})
} else { : $conf_pkgsrcdir;
$default_vars->{PKGSRCDIR} = $conf_pkgsrcdir;
}
$default_vars->{DESTDIR} = ''; $default_vars->{DESTDIR} = '';
$default_vars->{LOCALBASE} = '/usr/pkg'; $default_vars->{LOCALBASE} = '/usr/pkg';
@ -693,6 +670,7 @@ sub get_default_makefile_vars() {
} }
} }
# XXX: repeated from above?
if ($opt{P}) { if ($opt{P}) {
$default_vars->{PKGSRCDIR} = realpath($opt{P}); $default_vars->{PKGSRCDIR} = realpath($opt{P});
} }
@ -727,28 +705,26 @@ sub get_default_makefile_vars() {
# #
sub invalid_version($pkgmatch) { sub invalid_version($pkgmatch) {
my ($fail, $ok); my ($fail, $ok);
my (@pkgmatches, @todo); my (@pkgmatches);
@todo = ($pkgmatch); my @todo = ($pkgmatch);
# We handle {} here, everything else in package_globmatch # We handle {} here, everything else in package_globmatch
while ($pkgmatch = shift @todo) { while ($pkgmatch = shift @todo) {
if ($pkgmatch =~ /(.*)\{([^{}]+)}(.*)/) { if ($pkgmatch =~ /(.*)\{([^{}]+)}(.*)/) {
foreach (split(',', $2)) { foreach (split(',', $2)) {
push(@todo, "$1$_$3"); push @todo, "$1$_$3";
} }
} else { } else {
push(@pkgmatches, $pkgmatch); push @pkgmatches, $pkgmatch;
} }
} }
foreach $pkgmatch (@pkgmatches) { foreach $pkgmatch (@pkgmatches) {
my ($pkg, $badver) = package_globmatch($pkgmatch); my ($pkg, $badver) = package_globmatch($pkgmatch);
if (defined($badver)) { if (defined $badver) {
my ($pkgs); if (my $pkgs = $pkgdb->pkgs($pkg)) {
if ($pkgs = $pkgdb->pkgs($pkg)) {
$fail .= $fail .=
"Version mismatch: '$pkg' $badver vs " "Version mismatch: '$pkg' $badver vs "
. join(',', $pkgs->versions) . "\n"; . join(',', $pkgs->versions) . "\n";
@ -756,28 +732,19 @@ sub invalid_version($pkgmatch) {
$fail .= "Unknown package: '$pkg' version $badver\n"; $fail .= "Unknown package: '$pkg' version $badver\n";
} }
} else { } else {
# If we find one match, don't bitch about others # If we find one match, don't bitch about others
$ok = 1; $ok = 1;
} }
} }
$ok && ($fail = undef); $ok ? undef : $fail;
$fail;
} }
# Use pkg_info to list installed packages
#
sub list_installed_packages() { sub list_installed_packages() {
my (@pkgs); open(PKG_INFO, 'pkg_info -e "*" |')
or fail("Unable to run pkg_info: $!");
open(PKG_INFO, 'pkg_info -e "*" |') || fail("Unable to run pkg_info: $!"); chomp(my @pkgs = <PKG_INFO>);
while (defined(my $pkg = <PKG_INFO>)) {
chomp($pkg);
push(@pkgs, canonicalize_pkgname($pkg));
}
close(PKG_INFO); close(PKG_INFO);
map { $_ = canonicalize_pkgname($_) } @pkgs;
@pkgs;
} }
# List top level pkgsrc categories # List top level pkgsrc categories
@ -798,18 +765,15 @@ sub list_pkgsrc_categories($pkgsrcdir) {
# For a given category, list potentially valid pkgdirs # For a given category, list potentially valid pkgdirs
# #
sub list_pkgsrc_pkgdirs($pkgsrcdir, $cat) { sub list_pkgsrc_pkgdirs($pkgsrcdir, $cat) {
my (@pkgdirs); opendir(CAT, "$pkgsrcdir/$cat")
or die("Unable to opendir($pkgsrcdir/$cat): $!");
if (!opendir(CAT, "$pkgsrcdir/$cat")) { my @pkgdirs = sort grep {
die("Unable to opendir($pkgsrcdir/$cat): $!"); $_ ne 'Makefile'
} && $_ ne 'pkg'
@pkgdirs = && $_ ne 'CVS'
sort grep($_ ne 'Makefile' && substr($_, 0, 1) ne '.'
&& $_ ne 'pkg' } readdir(CAT);
&& $_ ne 'CVS' closedir(CAT) or die;
&& substr($_, 0, 1) ne '.',
readdir(CAT));
closedir(CAT);
@pkgdirs; @pkgdirs;
} }
@ -905,12 +869,12 @@ sub package_globmatch($pkgmatch) {
($matchpkgname, $matchver) = ($1, $2); ($matchpkgname, $matchver) = ($1, $2);
if (defined $pkgdb->pkgs($matchpkgname)) { if (defined $pkgdb->pkgs($matchpkgname)) {
push(@pkgnames, $matchpkgname); push @pkgnames, $matchpkgname;
} elsif ($regex = glob2regex($matchpkgname)) { } elsif ($regex = glob2regex($matchpkgname)) {
foreach my $pkg ($pkgdb->pkgs) { foreach my $pkg ($pkgdb->pkgs) {
if ($pkg->pkgbase =~ /$regex/) { if ($pkg->pkgbase =~ /$regex/) {
push(@pkgnames, $pkg->pkgbase); push @pkgnames, $pkg->pkgbase;
} }
} }
} }
@ -1145,7 +1109,7 @@ sub pkgsrc_check_depends() {
$depend = canonicalize_pkgname($depend); $depend = canonicalize_pkgname($depend);
if (($msg = invalid_version($depend))) { if (($msg = invalid_version($depend))) {
if (!defined($err)) { if (!defined $err) {
print $pkgver->pkgname . " DEPENDS errors:\n"; print $pkgver->pkgname . " DEPENDS errors:\n";
} }
$err = 1; $err = 1;
@ -1217,7 +1181,7 @@ sub scan_pkgsrc_distfiles_vs_distinfo($pkgsrcdir, $pkgdistdir, $check_unref,
if (!defined($dist = $distfiles{$distn})) { if (!defined($dist = $distfiles{$distn})) {
$bad_distfiles{$distn} = 1; $bad_distfiles{$distn} = 1;
} elsif ($dist->{sum} ne 'IGNORE') { } elsif ($dist->{sum} ne 'IGNORE') {
push(@{$sumfiles{ $dist->{sumtype} }}, $distn); push @{$sumfiles{ $dist->{sumtype} }}, $distn;
} }
} }
} }, } },
@ -1248,9 +1212,9 @@ sub scan_pkgsrc_distfiles_vs_distinfo($pkgsrcdir, $pkgdistdir, $check_unref,
} }
my $pid = open3(my $in, my $out, undef, 'xargs', 'digest', $sum); my $pid = open3(my $in, my $out, undef, 'xargs', 'digest', $sum);
defined($pid) || fail 'fork'; defined $pid || fail 'fork';
my $pid2 = fork(); my $pid2 = fork();
defined($pid2) || fail 'fork'; defined $pid2 || fail 'fork';
if ($pid2) { if ($pid2) {
close($in); close($in);
} else { } else {
@ -1354,7 +1318,7 @@ sub check_prebuilt_packages() {
if (!defined $pkgver) { if (!defined $pkgver) {
if ($opt{p}) { if ($opt{p}) {
print "$File::Find::dir/$_\n"; print "$File::Find::dir/$_\n";
push(@matched_prebuiltpackages, "$File::Find::dir/$_"); push @matched_prebuiltpackages, "$File::Find::dir/$_";
} }
# Pick probably the last version # Pick probably the last version
@ -1363,12 +1327,12 @@ sub check_prebuilt_packages() {
if ($opt{R} && $pkgver->var('RESTRICTED')) { if ($opt{R} && $pkgver->var('RESTRICTED')) {
print "$File::Find::dir/$_\n"; print "$File::Find::dir/$_\n";
push(@matched_prebuiltpackages, "$File::Find::dir/$_"); push @matched_prebuiltpackages, "$File::Find::dir/$_";
} }
if ($opt{O} && $pkgver->var('OSVERSION_SPECIFIC')) { if ($opt{O} && $pkgver->var('OSVERSION_SPECIFIC')) {
print "$File::Find::dir/$_\n"; print "$File::Find::dir/$_\n";
push(@matched_prebuiltpackages, "$File::Find::dir/$_"); push @matched_prebuiltpackages, "$File::Find::dir/$_";
} }
} }
@ -1386,7 +1350,7 @@ sub check_prebuilt_packages() {
$dest = "$File::Find::dir/$dest"; $dest = "$File::Find::dir/$dest";
} }
if (!$prebuilt_pkgdir_cache{$dest}) { if (!$prebuilt_pkgdir_cache{$dest}) {
push(@prebuilt_pkgdirs, $dest); push @prebuilt_pkgdirs, $dest;
} }
} }
} }
@ -1432,7 +1396,7 @@ sub remove_distfiles($pkgsrcdir, $pkgdistdir) {
if ($pkgname =~ /^([^*?[]+)-([\d*?[].*)/) { if ($pkgname =~ /^([^*?[]+)-([\d*?[].*)/) {
foreach my $pkgver ($pkgdb->pkgver($1)) { foreach my $pkgver ($pkgdb->pkgver($1)) {
next if $pkgver->var('dir') =~ /-current/; next if $pkgver->var('dir') =~ /-current/;
push(@installed, $pkgver); push @installed, $pkgver;
last; last;
} }
} }
@ -1452,7 +1416,7 @@ sub remove_distfiles($pkgsrcdir, $pkgdistdir) {
$dn =~ s/^(\.\/)*//; $dn =~ s/^(\.\/)*//;
if (!defined $distfiles{$dn}) { if (!defined $distfiles{$dn}) {
$distfiles{$dn}{name} = $dn; $distfiles{$dn}{name} = $dn;
push(@pkgdistfiles, $dn); push @pkgdistfiles, $dn;
} }
} }
close(DISTINFO); close(DISTINFO);
@ -1478,7 +1442,7 @@ sub remove_distfiles($pkgsrcdir, $pkgdistdir) {
} }
} }
if ($found != 1) { if ($found != 1) {
push(@orphan, $dldf); push @orphan, $dldf;
print "Orphaned file: $dldf\n"; print "Orphaned file: $dldf\n";
} }
$found = 0; $found = 0;
@ -1505,7 +1469,7 @@ sub remove_distfiles($pkgsrcdir, $pkgdistdir) {
} }
} }
if ($found == 1) { if ($found == 1) {
push(@parent, $pkgdf); push @parent, $pkgdf;
print "Parented file: $pkgdf\n"; print "Parented file: $pkgdf\n";
} }
$found = 0; $found = 0;
@ -1601,7 +1565,7 @@ sub check_outdated_installed_packages($pkgsrcdir) {
foreach my $pkgver ($pkgdb->pkgver($1)) { foreach my $pkgver ($pkgdb->pkgver($1)) {
next if $pkgver->var('dir') =~ /-current/; next if $pkgver->var('dir') =~ /-current/;
push(@update, $pkgver); push @update, $pkgver;
last; last;
} }
} }
@ -1632,7 +1596,7 @@ sub check_outdated_installed_packages($pkgsrcdir) {
print "\nRunning '$conf_make fetch-list | sh' for each package:\n"; print "\nRunning '$conf_make fetch-list | sh' for each package:\n";
foreach my $pkgver (@update) { foreach my $pkgver (@update) {
my $pkgpath = $pkgver->var('dir'); my $pkgpath = $pkgver->var('dir');
defined($pkgpath) defined $pkgpath
or fail('Cannot determine ' . $pkgver->pkgbase . ' directory'); or fail('Cannot determine ' . $pkgver->pkgbase . ' directory');
print "$pkgsrcdir/$pkgpath\n"; print "$pkgsrcdir/$pkgpath\n";
@ -1707,6 +1671,14 @@ sub main() {
} }
} }
sub export_for_test() {
({
'opt' => \%opt,
'default_vars' => $default_vars,
'pkgdb' => $pkgdb,
});
}
if (caller()) { if (caller()) {
# To allow easy testing of the code. # To allow easy testing of the code.
# TODO: reduce the use of global variables, or make them accessible # TODO: reduce the use of global variables, or make them accessible

View file

@ -1,4 +1,4 @@
# $NetBSD: parse_makefile.t,v 1.4 2022/08/09 20:51:46 rillig Exp $ # $NetBSD: parse_makefile.t,v 1.5 2022/08/10 07:12:52 rillig Exp $
use strict; use strict;
use warnings; use warnings;
@ -52,5 +52,23 @@ sub test_parse_makefile_vars() {
ok($vars->{VAR}, 'value'); ok($vars->{VAR}, 'value');
} }
sub test_expand_modifiers() {
my $vars = {
REF => 'VALUE',
};
export_for_test()->{opt}->{D} = 1;
expand_modifiers('file.mk', 'VAR', '<', 'REF', 'S,U,X,', '>', $vars);
# FIXME: Should be 'VALXE', but the 'U' is wrongly interpreted as a
# ':U' modifier.
ok($vars->{VAR}, '<VALUE>');
expand_modifiers('file.mk', 'VAR', '<', 'REF', 'S,VAL,H,', '>', $vars);
ok($vars->{VAR}, '<HUE>');
}
test_expand_var(); test_expand_var();
test_parse_makefile_vars(); test_parse_makefile_vars();
test_expand_modifiers();