Use python in preference to perl

I know how to make python code robust, no clue about perl.
This commit is contained in:
Kovid Goyal 2022-03-08 11:05:02 +05:30
parent b91e47c3b6
commit 65b9c69bd8
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -42,13 +42,13 @@ if command -v base64 > /dev/null 2> /dev/null; then
elif command -v b64encode > /dev/null 2> /dev/null; then elif command -v b64encode > /dev/null 2> /dev/null; then
base64_encode() { command b64encode - | command sed '1d;$d' | command tr -d \\n\\r; } base64_encode() { command b64encode - | command sed '1d;$d' | command tr -d \\n\\r; }
base64_decode() { command fold -w 76 | command b64decode -r; } base64_decode() { command fold -w 76 | command b64decode -r; }
elif detect_perl; then
base64_encode() { command "$perl" -MMIME::Base64 -0777 -ne 'print encode_base64($_)'; }
base64_decode() { command "$perl" -MMIME::Base64 -ne 'print decode_base64($_)'; }
elif detect_python; then elif detect_python; then
pybase64() { command "$python" -c "import sys, base64; getattr(sys.stdout, 'buffer', sys.stdout).write(base64.standard_b64$1(getattr(sys.stdin, 'buffer', sys.stdin).read()))"; } pybase64() { command "$python" -c "import sys, base64; getattr(sys.stdout, 'buffer', sys.stdout).write(base64.standard_b64$1(getattr(sys.stdin, 'buffer', sys.stdin).read()))"; }
base64_encode() { pybase64 "encode"; } base64_encode() { pybase64 "encode"; }
base64_decode() { pybase64 "decode"; } base64_decode() { pybase64 "decode"; }
elif detect_perl; then
base64_encode() { command "$perl" -MMIME::Base64 -0777 -ne 'print encode_base64($_)'; }
base64_decode() { command "$perl" -MMIME::Base64 -ne 'print decode_base64($_)'; }
else else
die "base64 executable not present on remote host, ssh kitten cannot function." die "base64 executable not present on remote host, ssh kitten cannot function."
fi fi
@ -108,19 +108,41 @@ else
return $? return $?
} }
if detect_perl; then if [ "$(printf "%s" "test" | command head -c 3 2> /dev/null)" = "tes" ]; then
read_n_bytes_from_tty() {
command "$perl" -MList::Util=min -e \
'open(my $fh,"</dev/tty"); binmode($fh); my ($n,$buf)=(@ARGV[0],"");'\
'while($n){my $rv=sysread($fh,$buf,min(65536,$n)); die($!) if !defined($rv); die() if !$rv; $n-=$rv; print $buf;}' "$1" 2> /dev/null
}
elif [ "$(printf "%s" "test" | command head -c 3 2> /dev/null)" = "tes" ]; then
# using dd with bs=1 is very slow, so use head. On non GNU coreutils head # using dd with bs=1 is very slow, so use head. On non GNU coreutils head
# does not limit itself to reading -c bytes only from the pipe so we can potentially lose # does not limit itself to reading -c bytes only from the pipe so we can potentially lose
# some trailing data, for instance if the user starts typing. Cant be helped. # some trailing data, for instance if the user starts typing. Cant be helped.
read_n_bytes_from_tty() { read_n_bytes_from_tty() {
command head -c "$1" < /dev/tty command head -c "$1" < /dev/tty
} }
elif detect_python; then
read_n_bytes_from_tty() {
command "$python" "-c" "
import sys, os, errno
def eintr_retry(func, *args):
while True:
try:
return func(*args)
except EnvironmentError as e:
if e.errno != errno.EINTR:
raise
n = $1
in_fd = sys.stdin.fileno()
out_fd = sys.stdout.fileno()
while n > 0:
d = memoryview(eintr_retry(os.read, in_fd, n))
n -= len(d)
while d:
nw = eintr_retry(os.write, out_fd, d)
d = d[nw:]
" < /dev/tty
}
elif detect_perl; then
read_n_bytes_from_tty() {
command "$perl" -MList::Util=min -e \
'open(my $fh,"</dev/tty"); binmode($fh); my ($n,$buf)=(@ARGV[0],"");'\
'while($n){my $rv=sysread($fh,$buf,min(65536,$n)); die($!) if !defined($rv); die() if !$rv; $n-=$rv; print $buf;}' "$1" 2> /dev/null
}
else else
read_n_bytes_from_tty() { read_n_bytes_from_tty() {
command dd bs=1 count="$1" < /dev/tty 2> /dev/null command dd bs=1 count="$1" < /dev/tty 2> /dev/null