From 248fa1c940cbbfc67845c7f252881a948ab1265c Mon Sep 17 00:00:00 2001 From: Ole Tange Date: Wed, 14 Jul 2010 21:54:33 +0200 Subject: [PATCH] Unittest for multiple -a. Code refactor. --- doc/FUTURE_IDEAS | 3 -- src/parallel | 88 +++++++++++---------------------- unittest/tests-to-run/test26.sh | 10 ++++ unittest/wanted-results/test26 | 15 +++++- 4 files changed, 53 insertions(+), 63 deletions(-) diff --git a/doc/FUTURE_IDEAS b/doc/FUTURE_IDEAS index ad8a5d93..eca9abe1 100644 --- a/doc/FUTURE_IDEAS +++ b/doc/FUTURE_IDEAS @@ -1,6 +1,3 @@ - -multiple -a == :::: - # Hvordan udregnes system limits på remote systems hvis jeg ikke ved, hvormange # argumenter, der er? Lav system limits lokalt og lad det være max diff --git a/src/parallel b/src/parallel index 9f18d949..6d3741fb 100755 --- a/src/parallel +++ b/src/parallel @@ -82,7 +82,7 @@ B<{.}> can be changed with B<-U>. =item B<{>IB<}> -Argument from argument file I or the I'th argument. See B<::::> +Argument from argument file I or the I'th argument. See B<-a> and B<-N>. B<{>IB<}> can be used the same places as B<{}>. @@ -113,11 +113,9 @@ If B<--arg-file> is set arguments from that file will be appended. =item B<::::> I (beta test) -Use arguments from the command line as arguments files for input. One -line will be read from each of the files. The arguments can be -accessed in the command as B<{1}> .. B<{>IB<}>, so B<{1}> will be a -line from the first file, and B<{6}> will refer to the line with the -same line number from the 6th file. +Alternative version of B<-a> I B<-a> I ... + +See B<-a>. =item B<--null> @@ -137,6 +135,12 @@ Read items from the file I instead of stdin (standard input). If you use this option, stdin is given to the first process run. Otherwise, stdin is redirected from /dev/null. +If multiple B<-a> are given, one line will be read from each of the +files. The arguments can be accessed in the command as B<{1}> +.. B<{>IB<}>, so B<{1}> will be a line from the first file, and +B<{6}> will refer to the line with the same line number from the 6th +file. + =item B<--arg-sep> I (beta testing) @@ -1947,6 +1951,7 @@ sub parse_options { } $Global::total_jobs += @ARGV; @ARGV=(); + last; } else { push @new_argv, $arg; } @@ -1956,10 +1961,7 @@ sub parse_options { } if(grep /^::::$/o, @ARGV) { - # max-replace-args - if(not @::opt_a) { push @::opt_a, "/dev/null"; } - - # read all the files and merge them + # convert :::: to multiple -a my @new_argv = (); my @argument_files; while(@ARGV) { @@ -1973,36 +1975,7 @@ sub parse_options { } # Output: @ARGV = command option @ARGV=@new_argv; - $Global::input_is_filename ||= (@ARGV); - - $::opt_N = $#argument_files+1; - $Global::max_number_of_args = $#argument_files+1; - - # read the files - my @content; - my $max_lineno = 0; - my $in_fh = gensym; - for (my $fileno = 0; $fileno <= $#argument_files; $fileno++) { - if(not open ($in_fh, $argument_files[$fileno])) { - print STDERR ("$Global::progname: Cannot open $argument_files[$fileno]\n"); - exit (255); - } - for (my $lineno=0; $content[$fileno][$lineno] = get_next_arg_from_fh($in_fh); $lineno++) { - $max_lineno = max($max_lineno,$lineno); - } - close $in_fh; - } - for (my $lineno=0; $lineno <= $max_lineno; $lineno++) { - for (my $fileno = 0; $fileno <= $#argument_files; $fileno++) { - my $arg = $content[$fileno][$lineno]; - if(defined $arg) { - unget_arg($arg); - } else { - unget_arg(""); - } - } - } - $Global::total_jobs += $max_lineno; + push @::opt_a, @argument_files; } # must be done after ::: and :::: because they mess with @ARGV @@ -2012,21 +1985,10 @@ sub parse_options { # must be done after opt_arg_sep if($#::opt_a == 0) { # One -a => xargs compatibility - if(not open(ARGFILE,"<",$::opt_a[0])) { - print STDERR "$Global::progname: ". - "Cannot open input file `$::opt_a[0]': ". - "No such file or directory\n"; - exit(255); - } - $Global::argfile = *ARGFILE; + $Global::argfile = open_or_exit($::opt_a[0]); } else { - if(not open(ARGFILE,"<","/dev/null")) { - print STDERR "$Global::progname: ". - "Cannot open input file `/dev/null': ". - "No such file or directory\n"; - exit(255); - } - $Global::argfile = *ARGFILE; + # Multiple -a => xapply style + $Global::argfile = open_or_exit("/dev/null"); $::opt_N = $#::opt_a+1; $Global::max_number_of_args = $#::opt_a+1; # read the files @@ -2034,10 +1996,7 @@ sub parse_options { my $max_lineno = 0; my $in_fh = gensym; for (my $fileno = 0; $fileno <= $#::opt_a; $fileno++) { - if(not open ($in_fh, $::opt_a[$fileno])) { - print STDERR ("$Global::progname: Cannot open $::opt_a[$fileno]\n"); - exit (255); - } + $in_fh = open_or_exit($::opt_a[$fileno]); for (my $lineno=0; $content[$fileno][$lineno] = get_next_arg_from_fh($in_fh); $lineno++) { @@ -2068,7 +2027,6 @@ sub parse_options { $Global::Xargs = 1; } - if(defined $::opt_eta) { # must be done after opt_a $::opt_progress = $::opt_eta; @@ -2109,6 +2067,18 @@ sub parse_options { } } +sub open_or_exit { + my $file = shift; + my $fh = gensym; + if(not open($fh,"<",$file)) { + print STDERR "$Global::progname: ". + "Cannot open input file `$file': ". + "No such file or directory\n"; + exit(255); + } + return $fh; +} + sub cleanup { # Returns: N/A if(@::opt_basefile) { diff --git a/unittest/tests-to-run/test26.sh b/unittest/tests-to-run/test26.sh index 9d0b616c..857fedc0 100644 --- a/unittest/tests-to-run/test26.sh +++ b/unittest/tests-to-run/test26.sh @@ -32,3 +32,13 @@ echo '### Quoting if there is no command and 2 arg files' parallel -kv :::: <(echo 'echo a') <(echo 'echo b') echo '### Quoting if there is no command and 2 arg files of uneven length' parallel -kv :::: <(echo 'echo a';echo echo a1) <(echo 'echo b') + +echo '### Test multiple -a' +parallel -kv -a <(echo a) -a <(echo b) echo {2} {1} +parallel -kv echo {2} {1} :::: <(echo a) <(echo b) +echo '### Multiple -a: An unused file' +parallel -kv -a <(echo a) -a <(echo b) -a <(echo c) echo {2} {1} +parallel -kv echo {2} {1} :::: <(echo a) <(echo b) <(echo c) +echo '### Multiple -a: nonexistent' +stdout parallel -kv echo {2} {1} :::: nonexist nonexist2 +stdout parallel -kv -a nonexist -a nonexist2 echo {2} {1} diff --git a/unittest/wanted-results/test26 b/unittest/wanted-results/test26 index e2024945..00b2778b 100644 --- a/unittest/wanted-results/test26 +++ b/unittest/wanted-results/test26 @@ -10,7 +10,7 @@ 3 4 5 6 7 ### Test :::: on nonexistent -parallel: Cannot open nonexistent +parallel: Cannot open input file `nonexistent': No such file or directory ### Test :::: two files 1 5 2 6 @@ -81,3 +81,16 @@ echo a echo b a echo b echo a1 a1 +### Test multiple -a +echo b a +b a +echo b a +b a +### Multiple -a: An unused file +echo b a +b a +echo b a +b a +### Multiple -a: nonexistent +parallel: Cannot open input file `nonexist': No such file or directory +parallel: Cannot open input file `nonexist': No such file or directory