Cleanup salt/_compat.py now that Salt is Py3.5+ only

This commit is contained in:
Pedro Algarvio 2021-02-16 10:32:46 +00:00 committed by Megan Wilhite
parent 59e44b1c64
commit 12f0657306
42 changed files with 42 additions and 378 deletions

View file

@ -8,7 +8,6 @@ cherrypy>=17.4.1
cryptography>=2.6.1
gitpython>=2.1.15
idna>=2.8
ipaddress>=1.0.22
linode-python>=1.1.1
mako>=1.0.7
psutil>=5.6.6

View file

@ -136,7 +136,7 @@ immutables==0.14
importlib-metadata==0.23 # via importlib-resources, jsonschema, pluggy, pytest, virtualenv
importlib-resources==1.5.0 # via cfn-lint, virtualenv
iniconfig==1.0.1 # via pytest
ipaddress==1.0.22
ipaddress==1.0.22 # via kubernetes
isodate==0.6.0 # via msrest
jaraco.functools==2.0
jinja2==2.10.1

View file

@ -50,7 +50,7 @@ importlib-metadata==0.23 # via importlib-resources, jsonschema, pluggy, pytest,
importlib-resources==1.5.0 # via cfn-lint, virtualenv
iniconfig==1.0.1 # via pytest
ioloop==0.1a0
ipaddress==1.0.22
ipaddress==1.0.22 # via kubernetes
jaraco.functools==2.0
jinja2==2.10.1
jmespath==0.9.4

View file

@ -136,7 +136,7 @@ immutables==0.14
importlib-metadata==0.23 # via importlib-resources, jsonschema, pluggy, pytest, virtualenv
importlib-resources==1.5.0 # via cfn-lint, virtualenv
iniconfig==1.0.1 # via pytest
ipaddress==1.0.22
ipaddress==1.0.22 # via kubernetes
isodate==0.6.0 # via msrest
jaraco.functools==2.0
jinja2==2.10.1

View file

@ -50,7 +50,7 @@ importlib-metadata==0.23 # via importlib-resources, jsonschema, pluggy, pytest,
importlib-resources==1.5.0 # via cfn-lint, virtualenv
iniconfig==1.0.1 # via pytest
ioloop==0.1a0
ipaddress==1.0.22
ipaddress==1.0.22 # via kubernetes
jaraco.functools==2.0
jinja2==2.10.1
jmespath==0.9.4

View file

@ -134,7 +134,7 @@ google-auth==1.6.3 # via kubernetes
idna==2.8
importlib-metadata==0.23 # via jsonschema, pluggy, pytest, virtualenv
iniconfig==1.0.1 # via pytest
ipaddress==1.0.22
ipaddress==1.0.22 # via kubernetes
isodate==0.6.0 # via msrest
jaraco.functools==2.0
jinja2==2.10.1

View file

@ -46,7 +46,7 @@ idna==2.8
importlib-metadata==0.23 # via jsonschema, pluggy, pytest, virtualenv
iniconfig==1.0.1 # via pytest
ioloop==0.1a0
ipaddress==1.0.22
ipaddress==1.0.22 # via kubernetes
jaraco.functools==2.0
jinja2==2.10.1
jmespath==0.9.4

View file

@ -133,7 +133,7 @@ gitpython==3.1.12
google-auth==1.6.3 # via kubernetes
idna==2.8
iniconfig==1.0.1 # via pytest
ipaddress==1.0.22
ipaddress==1.0.22 # via kubernetes
isodate==0.6.0 # via msrest
jaraco.functools==2.0
jinja2==2.10.1

View file

@ -133,7 +133,7 @@ gitpython==3.1.12
google-auth==1.6.3 # via kubernetes
idna==2.8
iniconfig==1.0.1 # via pytest
ipaddress==1.0.22
ipaddress==1.0.22 # via kubernetes
isodate==0.6.0 # via msrest
jaraco.functools==2.0
jinja2==2.10.1

View file

@ -19,7 +19,6 @@ gitdb==4.0.5 # via gitpython
gitpython==3.1.12
idna==2.8
immutables==0.14 # via contextvars
ipaddress==1.0.22
jaraco.functools==2.0 # via cheroot, tempora
jinja2==2.10.1
linode-python==1.1.1

View file

@ -19,7 +19,6 @@ gitpython==3.1.12
idna==2.8
immutables==0.14 # via contextvars
ioloop==0.1a0
ipaddress==1.0.22
jaraco.functools==2.0 # via cheroot, tempora
jinja2==2.10.1
libnacl==1.7.1

View file

@ -19,7 +19,6 @@ gitdb==4.0.5 # via gitpython
gitpython==3.1.12
idna==2.8
immutables==0.14 # via contextvars
ipaddress==1.0.22
jaraco.functools==2.0 # via cheroot, tempora
jinja2==2.10.1
linode-python==1.1.1

View file

@ -19,7 +19,6 @@ gitpython==3.1.12
idna==2.8
immutables==0.14 # via contextvars
ioloop==0.1a0
ipaddress==1.0.22
jaraco.functools==2.0 # via cheroot, tempora
jinja2==2.10.1
libnacl==1.7.1

View file

@ -16,7 +16,6 @@ distro==1.5.0
gitdb==4.0.5 # via gitpython
gitpython==3.1.12
idna==2.8
ipaddress==1.0.22
jaraco.functools==2.0 # via cheroot, tempora
jinja2==2.10.1
linode-python==1.1.1

View file

@ -16,7 +16,6 @@ gitdb==4.0.5 # via gitpython
gitpython==3.1.12
idna==2.8
ioloop==0.1a0
ipaddress==1.0.22
jaraco.functools==2.0 # via cheroot, tempora
jinja2==2.10.1
libnacl==1.7.1

View file

@ -16,7 +16,6 @@ distro==1.5.0
gitdb==4.0.5 # via gitpython
gitpython==3.1.12
idna==2.8
ipaddress==1.0.22
jaraco.functools==2.0 # via cheroot, tempora
jinja2==2.10.1
linode-python==1.1.1

View file

@ -16,7 +16,6 @@ distro==1.5.0
gitdb==4.0.5 # via gitpython
gitpython==3.1.12
idna==2.8
ipaddress==1.0.22
jaraco.functools==2.0 # via cheroot, tempora
jinja2==2.10.1
linode-python==1.1.1

View file

@ -15,7 +15,6 @@ distro>=1.5.0
gitpython>=2.1.10
idna>=2.8
ioloop>=0.1a0
ipaddress>=1.0.22
libnacl>=1.7.1
lxml>=4.6.2
mako>=1.0.7

View file

@ -1,248 +1,12 @@
"""
Salt compatibility code
"""
# pylint: disable=import-error,unused-import,invalid-name,W0231,W0233
import binascii
import logging
# pylint: disable=unused-import
import sys
from salt.exceptions import SaltException
from salt.ext.six import binary_type, integer_types, string_types, text_type
from salt.ext.six.moves import StringIO, cStringIO
log = logging.getLogger(__name__)
try:
# Python >2.5
import xml.etree.cElementTree as ElementTree
except Exception: # pylint: disable=broad-except
try:
# Python >2.5
import xml.etree.ElementTree as ElementTree
except Exception: # pylint: disable=broad-except
try:
# normal cElementTree install
import elementtree.cElementTree as ElementTree
except Exception: # pylint: disable=broad-except
try:
# normal ElementTree install
import elementtree.ElementTree as ElementTree
except Exception: # pylint: disable=broad-except
ElementTree = None
# True if we are running on Python 3.
PY3 = sys.version_info.major == 3
if PY3:
import builtins
exceptions = builtins
# The ipaddress module included in Salt is from Python 3.9.1.
# When running from Py3.9.1+ use the standard library module, use ours otherwise
if sys.version_info >= (3, 9, 1):
import ipaddress
else:
import exceptions
if ElementTree is not None:
if not hasattr(ElementTree, "ParseError"):
class ParseError(Exception):
"""
older versions of ElementTree do not have ParseError
"""
ElementTree.ParseError = ParseError
def text_(s, encoding="latin-1", errors="strict"):
"""
If ``s`` is an instance of ``binary_type``, return
``s.decode(encoding, errors)``, otherwise return ``s``
"""
return s.decode(encoding, errors) if isinstance(s, binary_type) else s
def bytes_(s, encoding="latin-1", errors="strict"):
"""
If ``s`` is an instance of ``text_type``, return
``s.encode(encoding, errors)``, otherwise return ``s``
"""
return s.encode(encoding, errors) if isinstance(s, text_type) else s
def ascii_native_(s):
"""
Python 3: If ``s`` is an instance of ``text_type``, return
``s.encode('ascii')``, otherwise return ``str(s, 'ascii', 'strict')``
Python 2: If ``s`` is an instance of ``text_type``, return
``s.encode('ascii')``, otherwise return ``str(s)``
"""
if isinstance(s, text_type):
s = s.encode("ascii")
return str(s, "ascii", "strict") if PY3 else s
def native_(s, encoding="latin-1", errors="strict"):
"""
Python 3: If ``s`` is an instance of ``text_type``, return ``s``, otherwise
return ``str(s, encoding, errors)``
Python 2: If ``s`` is an instance of ``text_type``, return
``s.encode(encoding, errors)``, otherwise return ``str(s)``
"""
if PY3:
out = s if isinstance(s, text_type) else str(s, encoding, errors)
else:
out = s.encode(encoding, errors) if isinstance(s, text_type) else str(s)
return out
def string_io(data=None): # cStringIO can't handle unicode
"""
Pass data through to stringIO module and return result
"""
try:
return cStringIO(bytes(data))
except (UnicodeEncodeError, TypeError):
return StringIO(data)
try:
if PY3:
import ipaddress
else:
import salt.ext.ipaddress as ipaddress
except ImportError:
ipaddress = None
class IPv6AddressScoped(ipaddress.IPv6Address):
"""
Represent and manipulate single IPv6 Addresses.
Scope-aware version
"""
def __init__(self, address):
"""
Instantiate a new IPv6 address object. Scope is moved to an attribute 'scope'.
Args:
address: A string or integer representing the IP
Additionally, an integer can be passed, so
IPv6Address('2001:db8::') == IPv6Address(42540766411282592856903984951653826560)
or, more generally
IPv6Address(int(IPv6Address('2001:db8::'))) == IPv6Address('2001:db8::')
Raises:
AddressValueError: If address isn't a valid IPv6 address.
:param address:
"""
# pylint: disable-all
if not hasattr(self, "_is_packed_binary"):
# This method (below) won't be around for some Python 3 versions
# and we need check this differently anyway
self._is_packed_binary = lambda p: isinstance(p, bytes)
# pylint: enable-all
if isinstance(address, string_types) and "%" in address:
buff = address.split("%")
if len(buff) != 2:
raise SaltException('Invalid IPv6 address: "{}"'.format(address))
address, self.__scope = buff
else:
self.__scope = None
# For compatibility with python3.9 ipaddress
self._scope_id = self.__scope
if sys.version_info.major == 2:
ipaddress._BaseAddress.__init__(self, address)
ipaddress._BaseV6.__init__(self, address)
else:
# Python 3.4 fix. Versions higher are simply not affected
# https://github.com/python/cpython/blob/3.4/Lib/ipaddress.py#L543-L544
self._version = 6
self._max_prefixlen = ipaddress.IPV6LENGTH
# Efficient constructor from integer.
if isinstance(address, integer_types):
self._check_int_address(address)
self._ip = address
elif self._is_packed_binary(address):
self._check_packed_address(address, 16)
self._ip = int(binascii.hexlify(address), 16)
else:
address = str(address)
if "/" in address:
raise ipaddress.AddressValueError(
"Unexpected '/' in {}".format(address)
)
self._ip = self._ip_int_from_string(address)
def _is_packed_binary(self, data):
"""
Check if data is hexadecimal packed
:param data:
:return:
"""
packed = False
if isinstance(data, bytes) and len(data) == 16 and b":" not in data:
try:
packed = bool(int(binascii.hexlify(data), 16))
except ValueError:
pass
return packed
@property
def scope(self):
"""
Return scope of IPv6 address.
:return:
"""
return self.__scope
def __str__(self):
return text_type(
self._string_from_ip_int(self._ip)
+ ("%" + self.scope if self.scope is not None else "")
)
class IPv6InterfaceScoped(ipaddress.IPv6Interface, IPv6AddressScoped):
"""
Update
"""
def __init__(self, address):
if (
PY3
and isinstance(address, (bytes, int))
or not PY3
and isinstance(address, int)
):
IPv6AddressScoped.__init__(self, address)
self.network = ipaddress.IPv6Network(self._ip)
self._prefixlen = self._max_prefixlen
return
addr = ipaddress._split_optional_netmask(address)
IPv6AddressScoped.__init__(self, addr[0])
self.network = ipaddress.IPv6Network(address, strict=False)
self.netmask = self.network.netmask
self._prefixlen = self.network._prefixlen
self.hostmask = self.network.hostmask
if ipaddress:
ipaddress.IPv6Address = IPv6AddressScoped
if sys.version_info.major == 2:
ipaddress.IPv6Interface = IPv6InterfaceScoped
import salt.ext.ipaddress as ipaddress

View file

@ -92,6 +92,7 @@ import sys
import time
import urllib.parse
import uuid
import xml.etree.ElementTree as ET
from functools import cmp_to_key
import salt.config as config
@ -106,7 +107,6 @@ import salt.utils.json
import salt.utils.msgpack
import salt.utils.stringutils
import salt.utils.yaml
from salt._compat import ElementTree as ET
from salt.exceptions import (
SaltCloudConfigError,
SaltCloudException,

View file

@ -106,7 +106,6 @@ There are a few changes to note:
import abc
import datetime
import ipaddress
import json
import logging
import pprint
@ -115,6 +114,7 @@ import time
from pathlib import Path
import salt.config as config
from salt._compat import ipaddress
from salt.exceptions import (
SaltCloudConfigError,
SaltCloudException,

View file

@ -24,11 +24,11 @@ import pprint
import time
import urllib.parse
import urllib.request
import xml.etree.ElementTree as ET
from urllib.error import URLError
import salt.config as config
import salt.utils.cloud
from salt._compat import ElementTree as ET
from salt.exceptions import (
SaltCloudExecutionFailure,
SaltCloudExecutionTimeout,

View file

@ -13,10 +13,10 @@ Requires a ``subdomain`` and an ``apikey`` in ``/etc/salt/minion``:
"""
import logging
import xml.etree.ElementTree as ET
import salt.utils.http
import salt.utils.yaml
from salt._compat import ElementTree as ET
log = logging.getLogger(__name__)

View file

@ -31,7 +31,7 @@ except ImportError:
try:
from lxml import etree
except ImportError:
from salt._compat import ElementTree as etree
import xml.etree.ElementTree as etree
class KiwiExporter:
@ -75,9 +75,7 @@ class KiwiExporter:
return "\n".join(
[
line
for line in minidom.parseString(
etree.tostring(root, encoding="UTF-8", pretty_print=True)
)
for line in minidom.parseString(etree.tostring(root, encoding="UTF-8"))
.toprettyxml(indent=" ")
.split("\n")
if line.strip()

View file

@ -32,7 +32,7 @@ import yaml
try:
from lxml import etree
except ImportError:
from salt._compat import ElementTree as etree
import xml.etree.ElementTree as etree
# Juniper interface libraries

View file

@ -16,7 +16,7 @@ import salt.utils.stringutils
from salt.exceptions import CommandExecutionError
try:
from salt._compat import ElementTree as ET
import xml.etree.ElementTree as ET
HAS_ELEMENT_TREE = True
except ImportError:

View file

@ -17,12 +17,12 @@ import re
import sys
import time
import urllib.parse
import xml.etree.ElementTree as ET
from datetime import datetime
import salt.config
import salt.utils.hashutils
import salt.utils.xmlutil as xml
from salt._compat import ElementTree as ET
try:
import requests

View file

@ -14,6 +14,7 @@ import pprint
import re
import socket
import ssl
import xml.etree.ElementTree as ET
import zlib
import salt.config
@ -35,7 +36,6 @@ import salt.utils.stringutils
import salt.utils.xmlutil as xml
import salt.utils.yaml
import salt.version
from salt._compat import ElementTree as ET
from salt.ext import six
from salt.ext.six.moves import StringIO
from salt.ext.six.moves.urllib.error import URLError

View file

@ -6,12 +6,12 @@ Connection library for Amazon S3
import logging
import urllib.parse
import xml.etree.ElementTree as ET
import salt.utils.aws
import salt.utils.files
import salt.utils.hashutils
import salt.utils.xmlutil as xml
from salt._compat import ElementTree as ET
from salt.exceptions import CommandExecutionError
try:

View file

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
This salt util uses WMI to gather network information on Windows 7 and .NET 4.0+
on newer systems.
@ -20,11 +19,10 @@ depending on the version of Windows this is run on. Once support for Windows
- wmi
"""
# https://docs.microsoft.com/en-us/dotnet/api/system.net.networkinformation.networkinterface.getallnetworkinterfaces?view=netframework-4.7.2
from __future__ import absolute_import, print_function, unicode_literals
import ipaddress
import platform
from salt._compat import ipaddress
from salt.ext.six.moves import range
from salt.utils.versions import StrictVersion

View file

@ -4,10 +4,11 @@ Utilities for managing YAST
.. versionadded:: Beryllium
"""
import xml.etree.ElementTree as ET
import salt.utils.files
import salt.utils.xmlutil as xml
import salt.utils.yaml
from salt._compat import ElementTree as ET
def mksls(src, dst=None):

View file

@ -1,7 +1,8 @@
import xml.etree.ElementTree as ET
import pytest
import salt.modules.config as config
import salt.modules.virt as virt
from salt._compat import ElementTree as ET
from tests.support.mock import MagicMock

View file

@ -1,10 +1,10 @@
import os.path
import xml.etree.ElementTree as ET
import pytest
import salt.modules.virt as virt
import salt.syspaths
import salt.utils.xmlutil as xmlutil
from salt._compat import ElementTree as ET
from salt.exceptions import CommandExecutionError, SaltInvocationError
from tests.support.mock import MagicMock, patch

View file

@ -1,5 +1,6 @@
import xml.etree.ElementTree as ET
import salt.utils.xmlutil as xmlutil
from salt._compat import ElementTree as ET
def append_to_XMLDesc(mocked, fragment):

View file

@ -1,7 +1,8 @@
import xml.etree.ElementTree as ET
import pytest
import salt.modules.virt as virt
import salt.utils.xmlutil as xmlutil
from salt._compat import ElementTree as ET
from .conftest import loader_modules_config
from .test_helpers import assert_called, assert_xml_equals, strip_xml

View file

@ -1,6 +1,7 @@
# import pytest
import xml.etree.ElementTree as ET
import salt.returners.nagios_nrdp_return as nagios_nrdp_return
from salt._compat import ElementTree as ET
def test_prepare_xml():

View file

@ -1,6 +1,7 @@
import xml.etree.ElementTree as ET
import pytest
import salt.utils.xmlutil as xml
from salt._compat import ElementTree as ET
@pytest.fixture

View file

@ -18,6 +18,7 @@ import pprint
import subprocess
import tempfile
import time
import xml.etree.ElementTree as etree
import salt.config
import salt.exceptions
@ -29,7 +30,6 @@ import salt.utils.process
import salt.utils.stringutils
import salt.utils.yaml
import salt.version
from salt._compat import ElementTree as etree
from salt.ext import six
from salt.ext.six.moves import zip
from salt.ext.six.moves.queue import Empty

View file

@ -12,7 +12,7 @@ from tests.support.unit import TestCase, skipIf
try:
from lxml import etree
except ImportError:
from salt._compat import ElementTree as etree
import xml.etree.ElementTree as etree
try:
from jnpr.junos.utils.config import Config

View file

@ -9,13 +9,13 @@ import datetime
import os
import shutil
import tempfile
import xml.etree.ElementTree as ET
import salt.config
import salt.modules.config as config
import salt.modules.virt as virt
import salt.syspaths
import salt.utils.yaml
from salt._compat import ElementTree as ET
from salt.exceptions import CommandExecutionError, SaltInvocationError
# pylint: disable=import-error

View file

@ -1,84 +0,0 @@
# -*- coding: utf-8 -*-
"""
Unit tests for salt._compat
"""
# Import Python libs
from __future__ import absolute_import, print_function, unicode_literals
import logging
import sys
# Import Salt libs
import salt._compat as compat
# Import 3rd Party libs
from salt.ext.six import binary_type, text_type
# Import Salt Testing libs
from tests.support.unit import TestCase
log = logging.getLogger(__name__)
PY3 = sys.version_info.major == 3
class CompatTestCase(TestCase):
def test_text(self):
ret = compat.text_("test string")
self.assertTrue(isinstance(ret, text_type))
def test_text_binary(self):
ret = compat.text_(b"test string")
self.assertTrue(isinstance(ret, text_type))
def test_bytes(self):
ret = compat.bytes_("test string")
self.assertTrue(isinstance(ret, binary_type))
def test_bytes_binary(self):
ret = compat.bytes_(b"test string")
self.assertTrue(isinstance(ret, binary_type))
def test_ascii_native(self):
ret = compat.ascii_native_("test string")
self.assertTrue(isinstance(ret, str))
def test_ascii_native_binary(self):
ret = compat.ascii_native_(b"test string")
self.assertTrue(isinstance(ret, str))
def test_native(self):
ret = compat.native_("test string")
self.assertTrue(isinstance(ret, str))
def test_native_binary(self):
ret = compat.native_(b"test string")
self.assertTrue(isinstance(ret, str))
def test_string_io(self):
ret = compat.string_io("test string")
if PY3:
expected = "io.StringIO object"
else:
expected = "cStringIO.StringI object"
self.assertTrue(expected in repr(ret))
def test_string_io_unicode(self):
ret = compat.string_io("test string \xf8")
if PY3:
expected = "io.StringIO object"
else:
expected = "StringIO.StringIO instance"
self.assertTrue(expected in repr(ret))
def test_ipv6_class__is_packed_binary(self):
ipv6 = compat.IPv6AddressScoped("2001:db8::")
self.assertEqual(str(ipv6), "2001:db8::")
def test_ipv6_class__is_packed_binary_integer(self):
ipv6 = compat.IPv6AddressScoped(42540766411282592856903984951653826560)
self.assertEqual(str(ipv6), "2001:db8::")
def test_ipv6_class__is_packed_binary__issue_51831(self):
ipv6 = compat.IPv6AddressScoped(b"sixteen.digit.bn")
self.assertEqual(str(ipv6), "7369:7874:6565:6e2e:6469:6769:742e:626e")

View file

@ -1,8 +1,3 @@
"""
"""
# Python
import logging
import socket
import textwrap
@ -10,10 +5,7 @@ import textwrap
import pytest
import salt.modules.cmdmod
import salt.utils.dns
# Salt
from salt._compat import ipaddress
from salt.ext.six.moves import zip # pylint: disable=redefined-builtin
from salt.utils.dns import (
_data2rec,
_data2rec_group,