Skip to content

Commit

Permalink
run pre-commit upgrade code for Py3.8+
Browse files Browse the repository at this point in the history
  • Loading branch information
lkubb authored and Megan Wilhite committed Jun 21, 2023
1 parent a86675d commit 9f70585
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 42 deletions.
50 changes: 22 additions & 28 deletions salt/client/ssh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def __init__(self, opts):
}
if self.opts.get("rand_thin_dir"):
self.defaults["thin_dir"] = os.path.join(
"/var/tmp", ".{}".format(uuid.uuid4().hex[:6])
"/var/tmp", f".{uuid.uuid4().hex[:6]}"
)
self.opts["ssh_wipe"] = "True"
self.returners = salt.loader.returners(self.opts, {})
Expand Down Expand Up @@ -454,9 +454,9 @@ def get_pubkey(self):
priv = self.opts.get(
"ssh_priv", os.path.join(self.opts["pki_dir"], "ssh", "salt-ssh.rsa")
)
pub = "{}.pub".format(priv)
pub = f"{priv}.pub"
with salt.utils.files.fopen(pub, "r") as fp_:
return "{} rsa root@master".format(fp_.read().split()[1])
return f"{fp_.read().split()[1]} rsa root@master"

def key_deploy(self, host, ret):
"""
Expand Down Expand Up @@ -500,7 +500,7 @@ def _key_deploy_run(self, host, target, re_run=True):
mods=self.mods,
fsclient=self.fsclient,
thin=self.thin,
**target
**target,
)
if salt.utils.path.which("ssh-copy-id"):
# we have ssh-copy-id, use it!
Expand All @@ -516,7 +516,7 @@ def _key_deploy_run(self, host, target, re_run=True):
mods=self.mods,
fsclient=self.fsclient,
thin=self.thin,
**target
**target,
)
stdout, stderr, retcode = single.cmd_block()
try:
Expand All @@ -543,7 +543,7 @@ def handle_routine(self, que, opts, host, target, mine=False):
fsclient=self.fsclient,
thin=self.thin,
mine=mine,
**target
**target,
)
ret = {"id": single.id}
stdout, stderr, retcode = single.run()
Expand Down Expand Up @@ -798,7 +798,7 @@ def run(self, jid=None):
)

if self.opts.get("verbose"):
msg = "Executing job with jid {}".format(jid)
msg = f"Executing job with jid {jid}"
print(msg)
print("-" * len(msg) + "\n")
print("")
Expand Down Expand Up @@ -883,7 +883,7 @@ def __init__(
remote_port_forwards=None,
winrm=False,
ssh_options=None,
**kwargs
**kwargs,
):
# Get mine setting and mine_functions if defined in kwargs (from roster)
self.mine = mine
Expand Down Expand Up @@ -1017,9 +1017,7 @@ def check_thin_dir(self):
"""
check if the thindir exists on the remote machine
"""
stdout, stderr, retcode = self.shell.exec_cmd(
"test -d {}".format(self.thin_dir)
)
stdout, stderr, retcode = self.shell.exec_cmd(f"test -d {self.thin_dir}")
if retcode != 0:
return False
return True
Expand Down Expand Up @@ -1131,7 +1129,7 @@ def run_wfunc(self):
self.id,
fsclient=self.fsclient,
minion_opts=self.minion_opts,
**self.target
**self.target,
)

opts_pkg = pre_wrapper["test.opts_pkg"]() # pylint: disable=E1102
Expand Down Expand Up @@ -1217,7 +1215,7 @@ def run_wfunc(self):
self.id,
fsclient=self.fsclient,
minion_opts=self.minion_opts,
**self.target
**self.target,
)
wrapper.fsclient.opts["cachedir"] = opts["cachedir"]
self.wfuncs = salt.loader.ssh_wrapper(opts, wrapper, self.context)
Expand Down Expand Up @@ -1265,7 +1263,7 @@ def run_wfunc(self):
else:
result = self.wfuncs[self.fun](*self.args, **self.kwargs)
except TypeError as exc:
result = "TypeError encountered executing {}: {}".format(self.fun, exc)
result = f"TypeError encountered executing {self.fun}: {exc}"
log.error(result, exc_info_on_loglevel=logging.DEBUG)
retcode = 1
except Exception as exc: # pylint: disable=broad-except
Expand All @@ -1292,7 +1290,7 @@ def _cmd_str(self):
"""
if self.target.get("sudo"):
sudo = (
"sudo -p '{}'".format(salt.client.ssh.shell.SUDO_PROMPT)
f"sudo -p '{salt.client.ssh.shell.SUDO_PROMPT}'"
if self.target.get("passwd")
else "sudo"
)
Expand Down Expand Up @@ -1364,20 +1362,18 @@ def execute_script(self, script, extension="py", pre_dir="", script_args=None):
script_args = shlex.split(str(script_args))
args = " {}".format(" ".join([shlex.quote(str(el)) for el in script_args]))
if extension == "ps1":
ret = self.shell.exec_cmd('"powershell {}"'.format(script))
ret = self.shell.exec_cmd(f'"powershell {script}"')
else:
if not self.winrm:
ret = self.shell.exec_cmd(
"/bin/sh '{}{}'{}".format(pre_dir, script, args)
)
ret = self.shell.exec_cmd(f"/bin/sh '{pre_dir}{script}'{args}")
else:
ret = saltwinshell.call_python(self, script)

# Remove file from target system
if not self.winrm:
self.shell.exec_cmd("rm '{}{}'".format(pre_dir, script))
self.shell.exec_cmd(f"rm '{pre_dir}{script}'")
else:
self.shell.exec_cmd("del {}".format(script))
self.shell.exec_cmd(f"del {script}")

return ret

Expand Down Expand Up @@ -1465,7 +1461,7 @@ def cmd_block(self, is_retry=False):
while re.search(RSTR_RE, stderr):
stderr = re.split(RSTR_RE, stderr, 1)[1].strip()
else:
return "ERROR: {}".format(error), stderr, retcode
return f"ERROR: {error}", stderr, retcode

# FIXME: this discards output from ssh_shim if the shim succeeds. It should
# always save the shim output regardless of shim success or failure.
Expand Down Expand Up @@ -1525,7 +1521,7 @@ def cmd_block(self, is_retry=False):
# If RSTR is not seen in both stdout and stderr then there
# was a thin deployment problem.
return (
"ERROR: Failure deploying ext_mods: {}".format(stdout),
f"ERROR: Failure deploying ext_mods: {stdout}",
stderr,
retcode,
)
Expand Down Expand Up @@ -1693,7 +1689,7 @@ def mod_data(fsclient):
files = fsclient.file_list(env)
for ref in sync_refs:
mods_data = {}
pref = "_{}".format(ref)
pref = f"_{ref}"
for fn_ in sorted(files):
if fn_.startswith(pref):
if fn_.endswith((".py", ".so", ".pyx")):
Expand All @@ -1715,9 +1711,7 @@ def mod_data(fsclient):
ver_base = salt.utils.stringutils.to_bytes(ver_base)

ver = hashlib.sha1(ver_base).hexdigest()
ext_tar_path = os.path.join(
fsclient.opts["cachedir"], "ext_mods.{}.tgz".format(ver)
)
ext_tar_path = os.path.join(fsclient.opts["cachedir"], f"ext_mods.{ver}.tgz")
mods = {"version": ver, "file": ext_tar_path}
if os.path.isfile(ext_tar_path):
return mods
Expand Down Expand Up @@ -1766,7 +1760,7 @@ def _convert_args(args):
for key in list(arg.keys()):
if key == "__kwarg__":
continue
converted.append("{}={}".format(key, arg[key]))
converted.append(f"{key}={arg[key]}")
else:
converted.append(arg)
return converted
18 changes: 9 additions & 9 deletions salt/client/ssh/wrapper/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def _ssh_state(chunks, st_kwargs, kwargs, test=False):
cmd,
fsclient=__context__["fileclient"],
minion_opts=__salt__.minion_opts,
**st_kwargs
**st_kwargs,
)
single.shell.send(trans_tar, "{}/salt_state.tgz".format(__opts__["thin_dir"]))
stdout, stderr, _ = single.cmd_block()
Expand Down Expand Up @@ -244,7 +244,7 @@ def sls(mods, saltenv="base", test=None, exclude=None, **kwargs):
cmd,
fsclient=__context__["fileclient"],
minion_opts=__salt__.minion_opts,
**st_kwargs
**st_kwargs,
)
single.shell.send(trans_tar, "{}/salt_state.tgz".format(opts["thin_dir"]))
stdout, stderr, _ = single.cmd_block()
Expand Down Expand Up @@ -392,7 +392,7 @@ def low(data, **kwargs):
cmd,
fsclient=__context__["fileclient"],
minion_opts=__salt__.minion_opts,
**st_kwargs
**st_kwargs,
)
single.shell.send(trans_tar, "{}/salt_state.tgz".format(__opts__["thin_dir"]))
stdout, stderr, _ = single.cmd_block()
Expand Down Expand Up @@ -482,7 +482,7 @@ def high(data, **kwargs):
cmd,
fsclient=__context__["fileclient"],
minion_opts=__salt__.minion_opts,
**st_kwargs
**st_kwargs,
)
single.shell.send(trans_tar, "{}/salt_state.tgz".format(opts["thin_dir"]))
stdout, stderr, _ = single.cmd_block()
Expand Down Expand Up @@ -558,7 +558,7 @@ def request(mods=None, **kwargs):
try:
if salt.utils.platform.is_windows():
# Make sure cache file isn't read-only
__salt__["cmd.run"]('attrib -R "{}"'.format(notify_path))
__salt__["cmd.run"](f'attrib -R "{notify_path}"')
with salt.utils.files.fopen(notify_path, "w+b") as fp_:
salt.payload.dump(req, fp_)
except OSError:
Expand Down Expand Up @@ -622,7 +622,7 @@ def clear_request(name=None):
try:
if salt.utils.platform.is_windows():
# Make sure cache file isn't read-only
__salt__["cmd.run"]('attrib -R "{}"'.format(notify_path))
__salt__["cmd.run"](f'attrib -R "{notify_path}"')
with salt.utils.files.fopen(notify_path, "w+b") as fp_:
salt.payload.dump(req, fp_)
except OSError:
Expand Down Expand Up @@ -730,7 +730,7 @@ def highstate(test=None, **kwargs):
cmd,
fsclient=__context__["fileclient"],
minion_opts=__salt__.minion_opts,
**st_kwargs
**st_kwargs,
)
single.shell.send(trans_tar, "{}/salt_state.tgz".format(opts["thin_dir"]))
stdout, stderr, _ = single.cmd_block()
Expand Down Expand Up @@ -821,7 +821,7 @@ def top(topfn, test=None, **kwargs):
cmd,
fsclient=__context__["fileclient"],
minion_opts=__salt__.minion_opts,
**st_kwargs
**st_kwargs,
)
single.shell.send(trans_tar, "{}/salt_state.tgz".format(opts["thin_dir"]))
stdout, stderr, _ = single.cmd_block()
Expand Down Expand Up @@ -1227,7 +1227,7 @@ def single(fun, name, test=None, **kwargs):
cmd,
fsclient=__context__["fileclient"],
minion_opts=__salt__.minion_opts,
**st_kwargs
**st_kwargs,
)

# Copy the tar down
Expand Down
8 changes: 3 additions & 5 deletions tests/pytests/integration/ssh/test_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,13 @@ def test_set_path(salt_ssh_cli, tmp_path, salt_ssh_roster_file):
roster_data = salt.utils.yaml.safe_load(rfh)
roster_data["localhost"].update(
{
"set_path": "$PATH:/usr/local/bin/:{}".format(path),
"set_path": f"$PATH:/usr/local/bin/:{path}",
}
)
with salt.utils.files.fopen(roster_file, "w") as wfh:
salt.utils.yaml.safe_dump(roster_data, wfh)

ret = salt_ssh_cli.run(
"--roster-file={}".format(roster_file), "environ.get", "PATH"
)
ret = salt_ssh_cli.run(f"--roster-file={roster_file}", "environ.get", "PATH")
assert ret.returncode == 0
assert path in ret.data

Expand All @@ -98,7 +96,7 @@ def test_tty(salt_ssh_cli, tmp_path, salt_ssh_roster_file):
roster_data["localhost"].update({"tty": True})
with salt.utils.files.fopen(roster_file, "w") as wfh:
salt.utils.yaml.safe_dump(roster_data, wfh)
ret = salt_ssh_cli.run("--roster-file={}".format(roster_file), "test.ping")
ret = salt_ssh_cli.run(f"--roster-file={roster_file}", "test.ping")
assert ret.returncode == 0
assert ret.data is True

Expand Down

0 comments on commit 9f70585

Please sign in to comment.