Released as 20210922 ('Vindelev')
[parallel.git] / src / env_parallel.zsh
blobfb18607f5b05c68d22212e218ee6ff54b8da6aa0
1 #!/usr/bin/env zsh
3 # This file must be sourced in zsh:
5 # source =env_parallel.zsh
7 # after which 'env_parallel' works
10 # Copyright (C) 2016-2021 Ole Tange, http://ole.tange.dk and Free
11 # Software Foundation, Inc.
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 3 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, see <http://www.gnu.org/licenses/>
25 # or write to the Free Software Foundation, Inc., 51 Franklin St,
26 # Fifth Floor, Boston, MA 02110-1301 USA
28 # SPDX-FileCopyrightText: 2021 Ole Tange, http://ole.tange.dk and Free Software and Foundation, Inc.
29 # SPDX-License-Identifier: GPL-3.0-or-later
31 env_parallel() {
32 # env_parallel.zsh
34 _names_of_ALIASES() {
35 print -l ${(k)aliases}
37 _bodies_of_ALIASES() {
38 local _i
39 for _i ($@); do
40 echo 'alias '"$(alias $_i)"
41 done
43 _names_of_FUNCTIONS() {
44 print -l ${(k)functions}
46 _bodies_of_FUNCTIONS() {
47 typeset -f "$@"
49 _names_of_VARIABLES() {
50 print -l ${(k)parameters}
52 _bodies_of_VARIABLES() {
53 typeset -p "$@"
55 _ignore_HARDCODED() {
56 # These names cannot be detected
57 echo '([-\?\#\!\$\*\@\_0]|zsh_eval_context|ZSH_EVAL_CONTEXT|LINENO|IFS|commands|functions|options|aliases|EUID|EGID|UID|GID|dis_patchars|patchars|terminfo|galiases|keymaps|parameters|jobdirs|dirstack|functrace|funcsourcetrace|zsh_scheduled_events|dis_aliases|dis_reswords|dis_saliases|modules|reswords|saliases|widgets|userdirs|historywords|nameddirs|termcap|dis_builtins|dis_functions|jobtexts|funcfiletrace|dis_galiases|builtins|history|jobstates|funcstack|run-help)'
59 _ignore_READONLY() {
60 typeset -pr | perl -e '@r = map {
61 chomp;
62 # sh on UnixWare: readonly TIMEOUT
63 # ash: readonly var='val'
64 # ksh: var='val'
65 s/^(readonly )?([^= ]*)(=.*|)$/$2/ or
66 # bash: declare -ar BASH_VERSINFO=([0]="4" [1]="4")
67 # zsh: typeset -r var='val'
68 s/^\S+\s+\S+\s+(\S[^=]*)(=.*|$)/$1/;
69 $_ } <>;
70 $vars = join "|",map { quotemeta $_ } @r;
71 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
74 _remove_bad_NAMES() {
75 # Do not transfer vars and funcs from env_parallel
76 _ignore_RO="`_ignore_READONLY`"
77 _ignore_HARD="`_ignore_HARDCODED`"
78 # Macos-grep does not like long patterns
79 # Old Solaris grep does not support -E
80 # Perl Version of:
81 # grep -Ev '^(...)$' |
82 perl -ne '/^(
83 PARALLEL_ENV|
84 PARALLEL_TMP|
85 _alias_NAMES|
86 _bodies_of_ALIASES|
87 _bodies_of_FUNCTIONS|
88 _bodies_of_VARIABLES|
89 _error_PAR|
90 _function_NAMES|
91 _get_ignored_VARS|
92 _grep_REGEXP|
93 _ignore_HARD|
94 _ignore_HARDCODED|
95 _ignore_READONLY|
96 _ignore_RO|
97 _ignore_UNDERSCORE|
98 _list_alias_BODIES|
99 _list_function_BODIES|
100 _list_variable_VALUES|
101 _make_grep_REGEXP|
102 _names_of_ALIASES|
103 _names_of_FUNCTIONS|
104 _names_of_VARIABLES|
105 _names_of_maybe_FUNCTIONS|
106 _parallel_exit_CODE|
107 _prefix_PARALLEL_ENV|
108 _prefix_PARALLEL_ENV|
109 _remove_bad_NAMES|
110 _remove_readonly|
111 _variable_NAMES|
112 _warning_PAR|
113 _which_PAR)$/x and next;
114 # Filter names matching --env
115 /^'"$_grep_REGEXP"'$/ or next;
116 /^'"$_ignore_UNDERSCORE"'$/ and next;
117 # Remove readonly variables
118 /^'"$_ignore_RO"'$/ and next;
119 /^'"$_ignore_HARD"'$/ and next;
120 print;'
122 _get_ignored_VARS() {
123 perl -e '
124 for(@ARGV){
125 $next_is_env and push @envvar, split/,/, $_;
126 $next_is_env=/^--env$/;
128 if(grep { /^_$/ } @envvar) {
129 if(not open(IN, "<", "$ENV{HOME}/.parallel/ignored_vars")) {
130 print STDERR "parallel: Error: ",
131 "Run \"parallel --record-env\" in a clean environment first.\n";
132 } else {
133 chomp(@ignored_vars = <IN>);
136 if($ENV{PARALLEL_IGNORED_NAMES}) {
137 push @ignored_vars, split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
138 chomp @ignored_vars;
140 $vars = join "|",map { quotemeta $_ } @ignored_vars;
141 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
142 ' -- "$@"
145 # Get the --env variables if set
146 # --env _ should be ignored
147 # and convert a b c to (a|b|c)
148 # If --env not set: Match everything (.*)
149 _make_grep_REGEXP() {
150 perl -e '
151 for(@ARGV){
152 /^_$/ and $next_is_env = 0;
153 $next_is_env and push @envvar, split/,/, $_;
154 $next_is_env = /^--env$/;
156 $vars = join "|",map { quotemeta $_ } @envvar;
157 print $vars ? "($vars)" : "(.*)";
158 ' -- "$@"
160 _which_PAR() {
161 # type returns:
162 # ll is an alias for ls -l (in ash)
163 # bash is a tracked alias for /bin/bash
164 # true is a shell builtin (in bash)
165 # myfunc is a function (in bash)
166 # myfunc is a shell function (in zsh)
167 # which is /usr/bin/which (in sh, bash)
168 # which is hashed (/usr/bin/which)
169 # gi is aliased to `grep -i' (in bash)
170 # aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
171 # Return 0 if found, 1 otherwise
172 LANG=C type "$@" |
173 perl -pe '$exit += (s/ is an alias for .*// ||
174 s/ is aliased to .*// ||
175 s/ is a function// ||
176 s/ is a shell function// ||
177 s/ is a shell builtin// ||
178 s/.* is hashed .(\S+).$/$1/ ||
179 s/.* is (a tracked alias for )?//);
180 END { exit not $exit }'
182 _warning_PAR() {
183 echo "env_parallel: Warning: $@" >&2
185 _error_PAR() {
186 echo "env_parallel: Error: $@" >&2
189 if _which_PAR parallel >/dev/null; then
190 true parallel found in path
191 else
192 _error_PAR 'parallel must be in $PATH.'
193 return 255
196 # Grep regexp for vars given by --env
197 _grep_REGEXP="`_make_grep_REGEXP \"$@\"`"
198 unset _make_grep_REGEXP
200 # Deal with --env _
201 _ignore_UNDERSCORE="`_get_ignored_VARS \"$@\"`"
202 unset _get_ignored_VARS
204 # --record-env
205 if perl -e 'exit grep { /^--record-env$/ } @ARGV' -- "$@"; then
206 true skip
207 else
208 (_names_of_ALIASES;
209 _names_of_FUNCTIONS;
210 _names_of_VARIABLES) |
211 cat > "$HOME"/.parallel/ignored_vars
212 return 0
215 # --session
216 if perl -e 'exit grep { /^--session$/ } @ARGV' -- "$@"; then
217 true skip
218 else
219 # Insert ::: between each level of session
220 # so you can pop off the last ::: at --end-session
221 PARALLEL_IGNORED_NAMES="`echo \"$PARALLEL_IGNORED_NAMES\";
222 echo :::;
223 (_names_of_ALIASES;
224 _names_of_FUNCTIONS;
225 _names_of_VARIABLES) | perl -ne '
226 BEGIN{
227 map { $ignored_vars{$_}++ }
228 split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
230 chomp;
231 for(split/\s+/) {
232 if(not $ignored_vars{$_}) {
233 print $_,\"\\n\";
237 export PARALLEL_IGNORED_NAMES
238 return 0
240 if perl -e 'exit grep { /^--end.?session$/ } @ARGV' -- "$@"; then
241 true skip
242 else
243 # Pop off last ::: from PARALLEL_IGNORED_NAMES
244 PARALLEL_IGNORED_NAMES="`perl -e '
245 $ENV{PARALLEL_IGNORED_NAMES} =~ s/(.*):::.*?$/$1/s;
246 print $ENV{PARALLEL_IGNORED_NAMES}
248 return 0
250 # Grep alias names
251 _alias_NAMES="`_names_of_ALIASES | _remove_bad_NAMES | xargs echo`"
252 _list_alias_BODIES="_bodies_of_ALIASES $_alias_NAMES"
253 if [ "$_alias_NAMES" = "" ] ; then
254 # no aliases selected
255 _list_alias_BODIES="true"
257 unset _alias_NAMES
259 # Grep function names
260 _function_NAMES="`_names_of_FUNCTIONS | _remove_bad_NAMES | xargs echo`"
261 _list_function_BODIES="_bodies_of_FUNCTIONS $_function_NAMES"
262 if [ "$_function_NAMES" = "" ] ; then
263 # no functions selected
264 _list_function_BODIES="true"
266 unset _function_NAMES
268 # Grep variable names
269 _variable_NAMES="`_names_of_VARIABLES | _remove_bad_NAMES | xargs echo`"
270 _list_variable_VALUES="_bodies_of_VARIABLES $_variable_NAMES"
271 if [ "$_variable_NAMES" = "" ] ; then
272 # no variables selected
273 _list_variable_VALUES="true"
275 unset _variable_NAMES
277 PARALLEL_ENV="`
278 eval $_list_alias_BODIES;
279 eval $_list_function_BODIES;
280 eval $_list_variable_VALUES;
282 export PARALLEL_ENV
283 unset _list_alias_BODIES _list_variable_VALUES _list_function_BODIES
284 unset _bodies_of_ALIASES _bodies_of_VARIABLES _bodies_of_FUNCTIONS
285 unset _names_of_ALIASES _names_of_VARIABLES _names_of_FUNCTIONS
286 unset _ignore_HARDCODED _ignore_READONLY _ignore_UNDERSCORE
287 unset _remove_bad_NAMES _grep_REGEXP
288 unset _prefix_PARALLEL_ENV
289 # Test if environment is too big
290 if `_which_PAR true` >/dev/null 2>/dev/null ; then
291 parallel "$@"
292 _parallel_exit_CODE=$?
293 # Clean up variables/functions
294 unset PARALLEL_ENV
295 unset _which_PAR _which_TRUE
296 unset _warning_PAR _error_PAR
297 # Unset _parallel_exit_CODE before return
298 eval "unset _parallel_exit_CODE; return $_parallel_exit_CODE"
299 else
300 unset PARALLEL_ENV;
301 _error_PAR "Your environment is too big."
302 _error_PAR "You can try 3 different approaches:"
303 _error_PAR "1. Run 'env_parallel --session' before you set"
304 _error_PAR " variables or define functions."
305 _error_PAR "2. Use --env and only mention the names to copy."
306 _error_PAR "3. Try running this in a clean environment once:"
307 _error_PAR " env_parallel --record-env"
308 _error_PAR " And then use '--env _'"
309 _error_PAR "For details see: man env_parallel"
310 return 255
314 parset() {
315 _parset_PARALLEL_PRG=parallel
316 _parset_main "$@"
319 env_parset() {
320 _parset_PARALLEL_PRG=env_parallel
321 _parset_main "$@"
324 _parset_main() {
325 # If $1 contains ',' or space:
326 # Split on , to get the destination variable names
327 # If $1 is a single destination variable name:
328 # Treat it as the name of an array
330 # # Create array named myvar
331 # parset myvar echo ::: {1..10}
332 # echo ${myvar[5]}
334 # # Put output into $var_a $var_b $var_c
335 # varnames=(var_a var_b var_c)
336 # parset "${varnames[*]}" echo ::: {1..3}
337 # echo $var_c
339 # # Put output into $var_a4 $var_b4 $var_c4
340 # parset "var_a4 var_b4 var_c4" echo ::: {1..3}
341 # echo $var_c4
343 _parset_NAME="$1"
344 if [ "$_parset_NAME" = "" ] ; then
345 echo parset: Error: No destination variable given. >&2
346 echo parset: Error: Try: >&2
347 echo parset: Error: ' ' parset myarray echo ::: foo bar >&2
348 return 255
350 if [ "$_parset_NAME" = "--help" ] ; then
351 echo parset: Error: Usage: >&2
352 echo parset: Error: ' ' parset varname GNU Parallel options and command >&2
353 echo
354 parallel --help
355 return 255
357 if [ "$_parset_NAME" = "--version" ] ; then
358 echo "parset 20210922 (GNU parallel `parallel --minversion 1`)"
359 echo "Copyright (C) 2007-2021 Ole Tange, http://ole.tange.dk and Free Software"
360 echo "Foundation, Inc."
361 echo "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>"
362 echo "This is free software: you are free to change and redistribute it."
363 echo "GNU parallel comes with no warranty."
364 echo
365 echo "Web site: https://www.gnu.org/software/parallel"
366 echo
367 echo "When using programs that use GNU Parallel to process data for publication"
368 echo "please cite as described in 'parallel --citation'."
369 echo
370 return 255
372 shift
374 # Bash: declare -A myassoc=( )
375 # Zsh: typeset -A myassoc=( )
376 # Ksh: typeset -A myassoc=( )
377 if (typeset -p "$_parset_NAME" 2>/dev/null; echo) |
378 perl -ne 'exit not (/^declare[^=]+-A|^typeset[^=]+-A/)' ; then
379 # This is an associative array
380 eval "`$_parset_PARALLEL_PRG -k --parset assoc,"$_parset_NAME" "$@"`"
381 # The eval returns the function!
382 else
383 # This is a normal array or a list of variable names
384 eval "`$_parset_PARALLEL_PRG -k --parset var,"$_parset_NAME" "$@"`"
385 # The eval returns the function!