Run pyupgrade against the files modified in the merge-forward

This commit is contained in:
Pedro Algarvio 2023-11-29 16:35:43 +00:00
parent 7cf72a7797
commit 5d5068cd63
No known key found for this signature in database
GPG key ID: BB36BF6584A298FF
3 changed files with 26 additions and 29 deletions

View file

@ -129,26 +129,26 @@ class Shell:
options.append("PasswordAuthentication=no")
if self.opts.get("_ssh_version", (0,)) > (4, 9):
options.append("GSSAPIAuthentication=no")
options.append("ConnectTimeout={}".format(self.timeout))
options.append(f"ConnectTimeout={self.timeout}")
if self.opts.get("ignore_host_keys"):
options.append("StrictHostKeyChecking=no")
if self.opts.get("no_host_keys"):
options.extend(["StrictHostKeyChecking=no", "UserKnownHostsFile=/dev/null"])
known_hosts = self.opts.get("known_hosts_file")
if known_hosts and os.path.isfile(known_hosts):
options.append("UserKnownHostsFile={}".format(known_hosts))
options.append(f"UserKnownHostsFile={known_hosts}")
if self.port:
options.append("Port={}".format(self.port))
options.append(f"Port={self.port}")
if self.priv and self.priv != "agent-forwarding":
options.append("IdentityFile={}".format(self.priv))
options.append(f"IdentityFile={self.priv}")
if self.user:
options.append("User={}".format(self.user))
options.append(f"User={self.user}")
if self.identities_only:
options.append("IdentitiesOnly=yes")
ret = []
for option in options:
ret.append("-o {} ".format(option))
ret.append(f"-o {option} ")
return "".join(ret)
def _passwd_opts(self):
@ -164,7 +164,7 @@ class Shell:
]
if self.opts["_ssh_version"] > (4, 9):
options.append("GSSAPIAuthentication=no")
options.append("ConnectTimeout={}".format(self.timeout))
options.append(f"ConnectTimeout={self.timeout}")
if self.opts.get("ignore_host_keys"):
options.append("StrictHostKeyChecking=no")
if self.opts.get("no_host_keys"):
@ -183,19 +183,19 @@ class Shell:
]
)
if self.port:
options.append("Port={}".format(self.port))
options.append(f"Port={self.port}")
if self.user:
options.append("User={}".format(self.user))
options.append(f"User={self.user}")
if self.identities_only:
options.append("IdentitiesOnly=yes")
ret = []
for option in options:
ret.append("-o {} ".format(option))
ret.append(f"-o {option} ")
return "".join(ret)
def _ssh_opts(self):
return " ".join(["-o {}".format(opt) for opt in self.ssh_options])
return " ".join([f"-o {opt}" for opt in self.ssh_options])
def _copy_id_str_old(self):
"""
@ -206,7 +206,7 @@ class Shell:
# passwords containing '$'
return "{} {} '{} -p {} {} {}@{}'".format(
"ssh-copy-id",
"-i {}.pub".format(self.priv),
f"-i {self.priv}.pub",
self._passwd_opts(),
self.port,
self._ssh_opts(),
@ -225,7 +225,7 @@ class Shell:
# passwords containing '$'
return "{} {} {} -p {} {} {}@{}".format(
"ssh-copy-id",
"-i {}.pub".format(self.priv),
f"-i {self.priv}.pub",
self._passwd_opts(),
self.port,
self._ssh_opts(),
@ -261,10 +261,7 @@ class Shell:
if ssh != "scp" and self.remote_port_forwards:
command.append(
" ".join(
[
"-R {}".format(item)
for item in self.remote_port_forwards.split(",")
]
[f"-R {item}" for item in self.remote_port_forwards.split(",")]
)
)
if self.ssh_options:
@ -306,7 +303,7 @@ class Shell:
rcode = None
cmd = self._cmd_str(cmd)
logmsg = "Executing non-blocking command: {}".format(cmd)
logmsg = f"Executing non-blocking command: {cmd}"
if self.passwd:
logmsg = logmsg.replace(self.passwd, ("*" * 6))
log.debug(logmsg)
@ -325,7 +322,7 @@ class Shell:
"""
cmd = self._cmd_str(cmd)
logmsg = "Executing command: {}".format(cmd)
logmsg = f"Executing command: {cmd}"
if self.passwd:
logmsg = logmsg.replace(self.passwd, ("*" * 6))
if 'decode("base64")' in logmsg or "base64.b64decode(" in logmsg:
@ -342,17 +339,17 @@ class Shell:
scp a file or files to a remote system
"""
if makedirs:
self.exec_cmd("mkdir -p {}".format(os.path.dirname(remote)))
self.exec_cmd(f"mkdir -p {os.path.dirname(remote)}")
# scp needs [<ipv6}
host = self.host
if ":" in host:
host = "[{}]".format(host)
host = f"[{host}]"
cmd = "{} {}:{}".format(local, host, remote)
cmd = f"{local} {host}:{remote}"
cmd = self._cmd_str(cmd, ssh="scp")
logmsg = "Executing command: {}".format(cmd)
logmsg = f"Executing command: {cmd}"
if self.passwd:
logmsg = logmsg.replace(self.passwd, ("*" * 6))
log.debug(logmsg)
@ -371,7 +368,7 @@ class Shell:
cmd_lst = shlex.split(cmd)
else:
cmd_lst = shlex.split(ssh_part)
cmd_lst.append("/bin/sh {}".format(cmd_part))
cmd_lst.append(f"/bin/sh {cmd_part}")
return cmd_lst
def _run_cmd(self, cmd, key_accept=False, passwd_retries=3):

View file

@ -124,7 +124,7 @@ def gpg_homedir(salt_master, test_key):
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=True,
universal_newlines=True,
text=True,
)
ret = ProcessResult(
returncode=proc.returncode,
@ -140,7 +140,7 @@ def gpg_homedir(salt_master, test_key):
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=True,
universal_newlines=True,
text=True,
input=test_key,
)
ret = ProcessResult(
@ -163,7 +163,7 @@ def gpg_homedir(salt_master, test_key):
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=True,
universal_newlines=True,
text=True,
input="KILLAGENT",
)
ret = ProcessResult(

View file

@ -44,12 +44,12 @@ def test_ssh_shell_exec_cmd(caplog):
_shell = shell.Shell(opts=opts, host=host)
_shell.passwd = passwd
with patch.object(_shell, "_split_cmd", return_value=["echo", passwd]):
ret = _shell.exec_cmd("echo {}".format(passwd))
ret = _shell.exec_cmd(f"echo {passwd}")
assert not any([x for x in ret if passwd in str(x)])
assert passwd not in caplog.text
with patch.object(_shell, "_split_cmd", return_value=["ls", passwd]):
ret = _shell.exec_cmd("ls {}".format(passwd))
ret = _shell.exec_cmd(f"ls {passwd}")
assert not any([x for x in ret if passwd in str(x)])
assert passwd not in caplog.text