From b843ec7398332312b6fdf66641111f3cc2308a04 Mon Sep 17 00:00:00 2001 From: Ole Tange Date: Thu, 21 Dec 2017 18:53:07 +0100 Subject: [PATCH] parallel: Auto add hostgroup if given on arg. --- src/parallel | 54 ++++++++++++------- testsuite/tests-to-run/parallel-local-ssh3.sh | 5 ++ testsuite/wanted-results/parallel-local-ssh3 | 3 ++ 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/src/parallel b/src/parallel index a8f1ba20..78c53318 100755 --- a/src/parallel +++ b/src/parallel @@ -1021,7 +1021,7 @@ sub options_hash { "replace|i:s" => \$opt::i, "E=s" => \$opt::eof, "eof|e:s" => \$opt::eof, - "max-args|n=i" => \$opt::max_args, + "max-args|maxargs|n=i" => \$opt::max_args, "max-replace-args|N=i" => \$opt::max_replace_args, "colsep|col-sep|C=s" => \$opt::colsep, "help|h" => \$opt::help, @@ -4918,6 +4918,8 @@ sub new { # Look for SSHLogin hostgroups %hostgroups = map { $_ => 1 } split(/\+/, $1); } + # An SSHLogin is always in the hostgroup of its "numcpu/host" + $hostgroups{$sshlogin_string} = 1; if ($sshlogin_string =~ s:^(\d+)/::) { # Override default autodetected ncpus unless missing $ncpus = $1; @@ -10302,25 +10304,23 @@ sub get { return $ret; } my $ret = $self->{'arg_sub_queue'}->get(); - if($ret and - grep { index($_->orig(),"\0") > 0 } @$ret) { - # Allow for \0 in position 0 because GNU Parallel uses "\0" - # to mean no-string - ::warning("a NUL character occurred in the input.", - "It cannot be passed through in the argument list.", - "Did you mean to use the --null option?"); - } - if(defined $Global::max_number_of_args - and $Global::max_number_of_args == 0) { - ::debug("run", "Read 1 but return 0 args\n"); - # \0 => nothing (not the empty string) - $ret = [Arg->new("\0")]; - } else { + if($ret) { + if(grep { index($_->orig(),"\0") > 0 } @$ret) { + # Allow for \0 in position 0 because GNU Parallel uses "\0" + # to mean no-string + ::warning("a NUL character occurred in the input.", + "It cannot be passed through in the argument list.", + "Did you mean to use the --null option?"); + } + if(defined $Global::max_number_of_args + and $Global::max_number_of_args == 0) { + ::debug("run", "Read 1 but return 0 args\n"); + # \0 => nothing (not the empty string) + map { $_->set_orig("\0"); } @$ret; + } # Flush cached computed replacements in Arg-objects # To fix: parallel --bar echo {%} ::: a b c ::: d e f - if($ret) { - map { $_->flush_cache() } @$ret; - } + map { $_->flush_cache() } @$ret; } return $ret; } @@ -10703,10 +10703,19 @@ sub new { # We found hostgroups on the arg @hostgroups = split(/\+/, $1); if(not grep { defined $Global::hostgroups{$_} } @hostgroups) { - ::warning("No such hostgroup (@hostgroups)."); - @hostgroups = (keys %Global::hostgroups); + # This hostgroup is not defined using -S + # Add it + ::warning("Adding hostgroups: @hostgroups"); + # Add sshlogin + for(grep { not defined $Global::hostgroups{$_} } @hostgroups) { + my $sshlogin = SSHLogin->new($_); + my $sshlogin_string = $sshlogin->string(); + $Global::host{$sshlogin_string} = $sshlogin; + $Global::hostgroups{$sshlogin_string} = 1; + } } } else { + # No hostgroup on the arg => any hostgroup @hostgroups = (keys %Global::hostgroups); } } @@ -10806,6 +10815,11 @@ sub orig { return $self->{'orig'}; } +sub set_orig { + my $self = shift; + $self->{'orig'} = shift; +} + sub trim_of { # Removes white space as specifed by --trim: # n = nothing diff --git a/testsuite/tests-to-run/parallel-local-ssh3.sh b/testsuite/tests-to-run/parallel-local-ssh3.sh index 687d5eca..d3c0cfd1 100644 --- a/testsuite/tests-to-run/parallel-local-ssh3.sh +++ b/testsuite/tests-to-run/parallel-local-ssh3.sh @@ -91,6 +91,11 @@ par_quoting_for_onall() { echo foo: /bin/ls | parallel --colsep ' ' -S lo --onall ls {2} } +par_hostgroup_only_on_args() { + echo '### Auto add hostgroup if given on on argument' + parallel --hostgroup ::: whoami@sh@lo +} + export -f $(compgen -A function | grep par_) # Tested with -j1..8 # -j6 was fastest diff --git a/testsuite/wanted-results/parallel-local-ssh3 b/testsuite/wanted-results/parallel-local-ssh3 index 253b37d0..6de7dda2 100644 --- a/testsuite/wanted-results/parallel-local-ssh3 +++ b/testsuite/wanted-results/parallel-local-ssh3 @@ -39,6 +39,9 @@ echo '### bug #49404: "Max jobs to run" does not equal the number of jobs specif should give 10 running jobs stdout parallel -S 16/lo --progress true ::: {1..10} | grep /.10 1:lo / 16 / 10 +par_hostgroup_only_on_args ### Auto add hostgroup if given on on argument +par_hostgroup_only_on_args parallel: Warning: Adding hostgroups: sh@lo +par_hostgroup_only_on_args sh par_quoting_for_onall ### bug #35427: quoting of {2} broken for --onall par_quoting_for_onall /bin/ls par_return_with_fixedstring ### Test --return with fixed string (Gave undef warnings)