From c1ce23e71b0223a63ad80c436048b521965a6089 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Wed, 20 Jul 2022 13:58:57 +0100 Subject: [PATCH] Commit pyupgrade code changes Signed-off-by: Pedro Algarvio --- .pre-commit-config.yaml | 5 ++++- salt/modules/logadm.py | 12 +++--------- salt/modules/smartos_vmadm.py | 9 ++------- salt/modules/win_servermanager.py | 11 +++-------- salt/modules/win_useradd.py | 21 ++++++++------------- tests/pytests/unit/renderers/test_yaml.py | 4 ++-- 6 files changed, 22 insertions(+), 40 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index adacaf97189..94b810b2779 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1113,7 +1113,10 @@ repos: args: [--py3-plus, --keep-mock] exclude: > (?x)^( - salt/client/ssh/ssh_py_shim.py| + salt/client/ssh/ssh_py_shim.py + | + salt/client/ssh/wrapper/pillar.py + | salt/ext/.*\.py )$ diff --git a/salt/modules/logadm.py b/salt/modules/logadm.py index 2587ac7d4e2..677e12f705b 100644 --- a/salt/modules/logadm.py +++ b/salt/modules/logadm.py @@ -10,12 +10,6 @@ import salt.utils.decorators as decorators import salt.utils.files import salt.utils.stringutils -try: - from shlex import quote as _quote_args # pylint: disable=E0611 -except ImportError: - from pipes import quote as _quote_args - - log = logging.getLogger(__name__) default_conf = "/etc/logadm.conf" option_toggles = { @@ -300,7 +294,7 @@ def rotate(name, pattern=None, conf_file=default_conf, **kwargs): _arg2opt(arg), ) elif arg in option_flags.values(): - command = "{} {} {}".format(command, _arg2opt(arg), _quote_args(str(val))) + command = "{} {} {}".format(command, _arg2opt(arg), shlex.quote(str(val))) elif arg != "log_file": log.warning("Unknown argument %s, don't know how to map this!", arg) if "log_file" in kwargs: @@ -313,9 +307,9 @@ def rotate(name, pattern=None, conf_file=default_conf, **kwargs): # % logadm -C2 -w mylog /my/really/long/log/file/name # % logadm -C2 -w /my/really/long/log/file/name if "entryname" not in kwargs: - command = "{} -w {}".format(command, _quote_args(kwargs["log_file"])) + command = "{} -w {}".format(command, shlex.quote(kwargs["log_file"])) else: - command = "{} {}".format(command, _quote_args(kwargs["log_file"])) + command = "{} {}".format(command, shlex.quote(kwargs["log_file"])) log.debug("logadm.rotate - command: %s", command) result = __salt__["cmd.run_all"](command, python_shell=False) diff --git a/salt/modules/smartos_vmadm.py b/salt/modules/smartos_vmadm.py index 4b3d0877acb..4e5b97baddf 100644 --- a/salt/modules/smartos_vmadm.py +++ b/salt/modules/smartos_vmadm.py @@ -4,6 +4,7 @@ Module for running vmadm command on SmartOS import logging import os +import shlex import salt.utils.args import salt.utils.files @@ -13,12 +14,6 @@ import salt.utils.platform import salt.utils.stringutils from salt.utils.odict import OrderedDict -try: - from shlex import quote as _quote_args # pylint: disable=E0611 -except ImportError: - from pipes import quote as _quote_args - - log = logging.getLogger(__name__) # Function aliases @@ -688,7 +683,7 @@ def reprovision(vm, image, key="uuid"): # vmadm reprovision [-f ] cmd = "echo {image} | vmadm reprovision {uuid}".format( uuid=salt.utils.stringutils.to_unicode(vm), - image=_quote_args(salt.utils.json.dumps({"image_uuid": image})), + image=shlex.quote(salt.utils.json.dumps({"image_uuid": image})), ) res = __salt__["cmd.run_all"](cmd, python_shell=True) retcode = res["retcode"] diff --git a/salt/modules/win_servermanager.py b/salt/modules/win_servermanager.py index 6c1a94c3db4..f26ce42183d 100644 --- a/salt/modules/win_servermanager.py +++ b/salt/modules/win_servermanager.py @@ -9,6 +9,7 @@ available and installed roles/features. Can install and remove roles/features. import logging +import shlex import salt.utils.json import salt.utils.platform @@ -16,12 +17,6 @@ import salt.utils.powershell import salt.utils.versions from salt.exceptions import CommandExecutionError -try: - from shlex import quote as _cmd_quote # pylint: disable=E0611 -except ImportError: - from pipes import quote as _cmd_quote - - log = logging.getLogger(__name__) __virtualname__ = "win_servermanager" @@ -230,7 +225,7 @@ def install(feature, recurse=False, restart=False, source=None, exclude=None): cmd = "{} -Name {} {} {} {} -WarningAction SilentlyContinue".format( command, - _cmd_quote(feature), + shlex.quote(feature), management_tools, "-IncludeAllSubFeature" if recurse else "", "" if source is None else "-Source {}".format(source), @@ -374,7 +369,7 @@ def remove(feature, remove_payload=False, restart=False): cmd = "{} -Name {} {} {} {} -WarningAction SilentlyContinue".format( command, - _cmd_quote(feature), + shlex.quote(feature), management_tools, _remove_payload, "-Restart" if restart else "", diff --git a/salt/modules/win_useradd.py b/salt/modules/win_useradd.py index 85fa3035849..e557ffd8113 100644 --- a/salt/modules/win_useradd.py +++ b/salt/modules/win_useradd.py @@ -23,6 +23,7 @@ Module for managing Windows Users. """ import logging +import shlex import time from datetime import datetime @@ -32,12 +33,6 @@ import salt.utils.platform import salt.utils.winapi from salt.exceptions import CommandExecutionError -try: - from shlex import quote as _cmd_quote # pylint: disable=E0611 -except Exception: # pylint: disable=broad-except - from pipes import quote as _cmd_quote - - log = logging.getLogger(__name__) try: @@ -463,8 +458,8 @@ def addgroup(name, group): salt '*' user.addgroup jsnuffy 'Power Users' """ - name = _cmd_quote(name) - group = _cmd_quote(group).lstrip("'").rstrip("'") + name = shlex.quote(name) + group = shlex.quote(group).lstrip("'").rstrip("'") user = info(name) if not user: @@ -496,8 +491,8 @@ def removegroup(name, group): salt '*' user.removegroup jsnuffy 'Power Users' """ - name = _cmd_quote(name) - group = _cmd_quote(group).lstrip("'").rstrip("'") + name = shlex.quote(name) + group = shlex.quote(group).lstrip("'").rstrip("'") user = info(name) @@ -632,11 +627,11 @@ def chgroups(name, groups, append=True): if ugrps == set(groups): return True - name = _cmd_quote(name) + name = shlex.quote(name) if not append: for group in ugrps: - group = _cmd_quote(group).lstrip("'").rstrip("'") + group = shlex.quote(group).lstrip("'").rstrip("'") if group not in groups: cmd = 'net localgroup "{}" {} /delete'.format(group, name) __salt__["cmd.run_all"](cmd, python_shell=True) @@ -644,7 +639,7 @@ def chgroups(name, groups, append=True): for group in groups: if group in ugrps: continue - group = _cmd_quote(group).lstrip("'").rstrip("'") + group = shlex.quote(group).lstrip("'").rstrip("'") cmd = 'net localgroup "{}" {} /add'.format(group, name) out = __salt__["cmd.run_all"](cmd, python_shell=True) if out["retcode"] != 0: diff --git a/tests/pytests/unit/renderers/test_yaml.py b/tests/pytests/unit/renderers/test_yaml.py index 93fb8414203..c80c8a82a89 100644 --- a/tests/pytests/unit/renderers/test_yaml.py +++ b/tests/pytests/unit/renderers/test_yaml.py @@ -1,4 +1,4 @@ -import collections +import collections.abc import textwrap import pytest @@ -23,7 +23,7 @@ def assert_unicode(value): for k, v in value.items(): assert_unicode(k) assert_unicode(v) - elif isinstance(value, collections.Iterable): + elif isinstance(value, collections.abc.Iterable): for item in value: assert_unicode(item)