improve --components parsing with comma and whitespace as separator

This commit is contained in:
Johannes 'josch' Schauer 2019-10-19 08:10:36 +02:00
parent cf9d80f059
commit 78358eaf9a
Signed by untrusted user: josch
GPG key ID: F2CBA5C78FBD83E1
2 changed files with 57 additions and 13 deletions

View file

@ -52,7 +52,7 @@ if [ ! -e shared/mmdebstrap ] || [ mmdebstrap -nt shared/mmdebstrap ]; then
fi fi
starttime= starttime=
total=102 total=103
i=1 i=1
print_header() { print_header() {
@ -510,6 +510,24 @@ else
./run_null.sh ./run_null.sh
fi fi
print_header "mode=$defaultmode,variant=apt: supply components manually"
cat << END > shared/test.sh
#!/bin/sh
set -eu
export LC_ALL=C.UTF-8
$CMD --mode=$defaultmode --variant=apt --components="main main" --comp="main,main" $DEFAULT_DIST /tmp/debian-chroot $mirror
echo "deb $mirror $DEFAULT_DIST main" | cmp /tmp/debian-chroot/etc/apt/sources.list
tar -C /tmp/debian-chroot --one-file-system -c . | tar -t | sort | diff -u tar1.txt -
rm -r /tmp/debian-chroot
END
if [ "$HAVE_QEMU" = "yes" ]; then
./run_qemu.sh
elif [ "$defaultmode" = "root" ]; then
./run_null.sh SUDO
else
./run_null.sh
fi
print_header "mode=root,variant=apt: stable default mirror" print_header "mode=root,variant=apt: stable default mirror"
cat << END > shared/test.sh cat << END > shared/test.sh
#!/bin/sh #!/bin/sh

View file

@ -1776,7 +1776,7 @@ sub main() {
my @ARGVORIG = @ARGV; my @ARGVORIG = @ARGV;
my $options = { my $options = {
components => "main", components => ["main"],
variant => "important", variant => "important",
include => undef, include => undef,
mode => 'auto', mode => 'auto',
@ -1793,7 +1793,7 @@ sub main() {
GetOptions( GetOptions(
'h|help' => sub { pod2usage(-exitval => 0, -verbose => 2) }, 'h|help' => sub { pod2usage(-exitval => 0, -verbose => 2) },
'version' => sub { print STDOUT "mmdebstrap $VERSION\n"; exit 0; }, 'version' => sub { print STDOUT "mmdebstrap $VERSION\n"; exit 0; },
'components=s' => \$options->{components}, 'components=s@' => \$options->{components},
'variant=s' => \$options->{variant}, 'variant=s' => \$options->{variant},
'include=s' => \$options->{include}, 'include=s' => \$options->{include},
'architectures=s' => \$options->{architectures}, 'architectures=s' => \$options->{architectures},
@ -2083,6 +2083,24 @@ sub main() {
info "Reading sources.list from standard input..."; info "Reading sources.list from standard input...";
$sourceslist = do { local $/; <STDIN> }; $sourceslist = do { local $/; <STDIN> };
} else { } else {
my @components = ();
foreach my $comp (@{$options->{components}}) {
my @comps = split /[,\s]+/, $comp;
foreach my $c (@comps) {
# strip leading and trailing whitespace
$c =~ s/^\s+|\s+$//g;
# skip if the remainder is an empty string
if ($c eq "") {
next;
}
# do not append component if it's already in the list
if (any {$_ eq $c} @components) {
next;
}
push @components, $c;
}
}
my $compstr = join " ", @components;
if (scalar @ARGV > 0) { if (scalar @ARGV > 0) {
for my $arg (@ARGV) { for my $arg (@ARGV) {
if ($arg eq '-') { if ($arg eq '-') {
@ -2091,7 +2109,7 @@ sub main() {
} elsif ($arg =~ /^deb(-src)? /) { } elsif ($arg =~ /^deb(-src)? /) {
$sourceslist .= "$arg\n"; $sourceslist .= "$arg\n";
} elsif ($arg =~ /:\/\//) { } elsif ($arg =~ /:\/\//) {
$sourceslist .= "deb $arg $suite $options->{components}\n"; $sourceslist .= "deb $arg $suite $compstr\n";
} elsif (-f $arg) { } elsif (-f $arg) {
open my $fh, '<', $arg or error "cannot open $arg: $!"; open my $fh, '<', $arg or error "cannot open $arg: $!";
while (my $line = <$fh>) { while (my $line = <$fh>) {
@ -2123,20 +2141,20 @@ sub main() {
} elsif (any {$_ eq $suite} @kali) { } elsif (any {$_ eq $suite} @kali) {
$mirror = 'https://http.kali.org/kali' $mirror = 'https://http.kali.org/kali'
} }
$sourceslist .= "deb $mirror $suite $options->{components}\n"; $sourceslist .= "deb $mirror $suite $compstr\n";
if (any {$_ eq $suite} @ubuntustable) { if (any {$_ eq $suite} @ubuntustable) {
$sourceslist .= "deb $mirror $suite-updates $options->{components}\n"; $sourceslist .= "deb $mirror $suite-updates $compstr\n";
$sourceslist .= "deb $secmirror $suite-security $options->{components}\n"; $sourceslist .= "deb $secmirror $suite-security $compstr\n";
} elsif (any {$_ eq $suite} @tanglustable) { } elsif (any {$_ eq $suite} @tanglustable) {
$sourceslist .= "deb $secmirror $suite-updates $options->{components}\n"; $sourceslist .= "deb $secmirror $suite-updates $compstr\n";
} elsif (any {$_ eq $suite} @debstable) { } elsif (any {$_ eq $suite} @debstable) {
$sourceslist .= "deb $mirror $suite-updates $options->{components}\n"; $sourceslist .= "deb $mirror $suite-updates $compstr\n";
if (any {$_ eq $suite} ('stable', 'oldstable', 'stretch', 'buster')) { if (any {$_ eq $suite} ('stable', 'oldstable', 'stretch', 'buster')) {
$sourceslist .= "deb $secmirror $suite/updates $options->{components}\n"; $sourceslist .= "deb $secmirror $suite/updates $compstr\n";
} else { } else {
# starting from bullseye use # starting from bullseye use
# https://lists.debian.org/87r26wqr2a.fsf@43-1.org # https://lists.debian.org/87r26wqr2a.fsf@43-1.org
$sourceslist .= "deb $secmirror $suite-security $options->{components}\n"; $sourceslist .= "deb $secmirror $suite-security $compstr\n";
} }
} }
} }
@ -2716,8 +2734,16 @@ C<pkg>. See apt(8) for the supported syntax.
=item B<--components>=I<comp1>[,I<comp2>,...] =item B<--components>=I<comp1>[,I<comp2>,...]
Comma separated list of components like main, contrib and non-free which will Comma or whitespace separated list of components like main, contrib and
be used for all URI-only I<MIRROR> arguments. non-free which will be used for all URI-only I<MIRROR> arguments. The option
can be specified multiple times and the components are concatenated in the
order in which they are given on the command line. If later list items are
repeated, then they get dropped so that the resulting component list is free
of duplicates. So the following are equivalent:
--components="main contrib non-free"
--components=main,contrib,non-free
--comp=main --comp="contrib non-free" --comp="main,non-free"
=item B<--architectures>=I<native>[,I<foreign1>,...] =item B<--architectures>=I<native>[,I<foreign1>,...]