Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't require six #1307

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/jnpr/junos/device.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# stdlib
import os
import six
import types
import platform
import warnings
Expand Down Expand Up @@ -659,7 +658,7 @@ def cli_to_rpc_string(self, command):
command = command.strip()
# Get the equivalent RPC
rpc = self.display_xml_rpc(command)
if isinstance(rpc, six.string_types):
if isinstance(rpc, str):
# No RPC is available.
return None
rpc_string = "rpc.%s(" % (rpc.tag.replace("-", "_"))
Expand Down
5 changes: 1 addition & 4 deletions lib/jnpr/junos/jxml.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from ncclient import manager
from ncclient.xml_ import NCElement
from lxml import etree
import six

"""
These are Junos XML 'helper' definitions use for generic XML processing
Expand Down Expand Up @@ -226,8 +225,7 @@ def cscript_conf(reply):


# xslt to remove prefix like junos:ns
strip_namespaces_prefix = six.b(
"""<?xml version="1.0" encoding="UTF-8" ?>
strip_namespaces_prefix = b"""<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no" omit-xml-declaration="no" />

Expand All @@ -249,4 +247,3 @@ def cscript_conf(reply):
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>"""
)
17 changes: 8 additions & 9 deletions lib/jnpr/junos/transport/tty_netconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,22 @@
from datetime import datetime, timedelta
from ncclient.operations.rpc import RPCReply, RPCError
from ncclient.xml_ import to_ele
import six
from ncclient.transport.session import HelloHandler


class PY6:
NEW_LINE = six.b("\n")
EMPTY_STR = six.b("")
NETCONF_EOM = six.b("]]>]]>")
STARTS_WITH = six.b("<!--")
NEW_LINE = b"\n"
EMPTY_STR = b""
NETCONF_EOM = b"]]>]]>"
STARTS_WITH = b"<!--"


__all__ = ["xmlmode_netconf"]

_NETCONF_EOM = six.b("]]>]]>")
_xmlns = re.compile(six.b("xmlns=[^>]+"))
_NETCONF_EOM = b"]]>]]>"
_xmlns = re.compile(b"xmlns=[^>]+")
_xmlns_strip = lambda text: _xmlns.sub(PY6.EMPTY_STR, text)
_junosns = re.compile(six.b("junos:"))
_junosns = re.compile(b"junos:")
_junosns_strip = lambda text: _junosns.sub(PY6.EMPTY_STR, text)

logger = logging.getLogger("jnpr.junos.tty_netconf")
Expand Down Expand Up @@ -116,7 +115,7 @@ def rpc(self, cmd):
"""
if not cmd.startswith("<"):
cmd = "<{}/>".format(cmd)
rpc = six.b("<rpc>{}</rpc>".format(cmd))
rpc = "<rpc>{}</rpc>".format(cmd).encode('utf-8')
logger.info("Calling rpc: %s" % rpc)
self._tty.rawwrite(rpc)

Expand Down
7 changes: 3 additions & 4 deletions lib/jnpr/junos/transport/tty_serial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import serial
import re
import six
from time import sleep
from datetime import datetime, timedelta

Expand All @@ -10,7 +9,7 @@
# Terminal connection over SERIAL CONSOLE
# -------------------------------------------------------------------------

_PROMPT = re.compile(six.b("|").join([six.b(i) for i in Terminal._RE_PAT]))
_PROMPT = re.compile(b"|".join([i.encode('utf-8') for i in Terminal._RE_PAT]))


class Serial(Terminal):
Expand Down Expand Up @@ -56,7 +55,7 @@ def _tty_close(self):

def write(self, content):
"""write content + <RETURN>"""
self._ser.write(six.b(content + "\n"))
self._ser.write(bytes(content + "\n", 'utf-8'))
self._ser.flush()

def rawwrite(self, content):
Expand All @@ -75,7 +74,7 @@ def read_prompt(self):
regular-expression group. If a timeout occurs, then return
the tuple(None,None).
"""
rxb = six.b("")
rxb = b""
mark_start = datetime.now()
mark_end = mark_start + timedelta(seconds=self.EXPECT_TIMEOUT)

Expand Down
21 changes: 10 additions & 11 deletions lib/jnpr/junos/transport/tty_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from time import sleep, time

import paramiko
import six

from jnpr.junos.transport.tty import Terminal

Expand All @@ -15,14 +14,14 @@
# -------------------------------------------------------------------------
# Terminal connection over SSH CONSOLE
# -------------------------------------------------------------------------
_PROMPT = re.compile(six.b("|").join([six.b(i) for i in Terminal._RE_PAT]))
_PROMPT = re.compile(b"|".join([i.encode('utf-8') for i in Terminal._RE_PAT]))


class PY6:
NEW_LINE = six.b("\n")
EMPTY_STR = six.b("")
NETCONF_EOM = six.b("]]>]]>")
IN_USE = six.b("in use")
NEW_LINE = b"\n"
EMPTY_STR = b""
NETCONF_EOM = b"]]>]]>"
IN_USE = b"in use"


class SSH(Terminal):
Expand Down Expand Up @@ -136,7 +135,7 @@ def _tty_close(self):
def write(self, content):
"""write content + <ENTER>"""
logger.debug("Write: %s" % content)
self._ssh.sendall(six.b((content + "\n")))
self._ssh.sendall(bytes(content + "\n", 'utf-8'))

def rawwrite(self, content):
"""write content as-is"""
Expand All @@ -152,13 +151,13 @@ def rawwrite(self, content):
if sys.version >= "3":
content = content.decode("utf-8")
for char in content:
self._ssh.sendall(six.b(char))
self._ssh.sendall(bchar)
wtime = 10 / float(self.baud)
sleep(wtime) # do not remove

def read(self):
"""read a single line"""
rxb = six.b("")
rxb = b""
while True:
data = self._ssh.recv(self.RECVSZ)
if data is None or len(data) <= 0:
Expand All @@ -180,7 +179,7 @@ def read_prompt(self):
regular-expression group. If a timeout occurs, then return
the tuple(None,None).
"""
rxb = six.b("")
rxb = b""
timeout = time() + self.READ_PROMPT_DELAY

while time() < timeout:
Expand All @@ -199,7 +198,7 @@ def read_prompt(self):
return rxb, found.lastgroup

def _read_until(self, match, timeout=None):
rxb = six.b("")
rxb = b""
timeout = time() + self.READ_PROMPT_DELAY

while time() < timeout:
Expand Down
15 changes: 7 additions & 8 deletions lib/jnpr/junos/transport/tty_telnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import telnetlib
import logging
import sys
import six

from jnpr.junos.transport.tty import Terminal

Expand All @@ -14,10 +13,10 @@


class PY6:
NEW_LINE = six.b("\n")
EMPTY_STR = six.b("")
NETCONF_EOM = six.b("]]>]]>")
IN_USE = six.b("in use")
NEW_LINE = b"\n"
EMPTY_STR = b""
NETCONF_EOM = b"]]>]]>"
IN_USE = b"in use"


class Telnet(Terminal):
Expand Down Expand Up @@ -81,7 +80,7 @@ def _tty_close(self):
def write(self, content):
"""write content + <ENTER>"""
logger.debug("Write: %s" % content)
self._tn.write(six.b((content + "\n")))
self._tn.write(bytes(content + "\n", 'utf-8'))

def rawwrite(self, content):
"""write content as-is"""
Expand All @@ -97,7 +96,7 @@ def rawwrite(self, content):
if sys.version >= "3":
content = content.decode("utf-8")
for char in content:
self._tn.write(six.b(char))
self._tn.write(char.encode("utf-8"))
wtime = 10 / float(self.baud)
sleep(wtime) # do not remove

Expand All @@ -106,7 +105,7 @@ def read(self):
return self._tn.read_until(PY6.NEW_LINE, self.EXPECT_TIMEOUT)

def read_prompt(self):
_RE_PAT = [six.b(i) for i in Terminal._RE_PAT]
_RE_PAT = [i.encode('utf-8') for i in Terminal._RE_PAT]
got = self._tn.expect(_RE_PAT, self.EXPECT_TIMEOUT)
if PY6.IN_USE in got[2]:
raise RuntimeError("open_fail: port already in use")
Expand Down
1 change: 0 additions & 1 deletion lib/jnpr/junos/utils/start_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import datetime
from jnpr.junos.utils.ssh_client import open_ssh_client
import subprocess
import six
from threading import Thread
import time

Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ ncclient>=0.6.15
scp>=0.7.0
jinja2>=2.7.1
PyYAML>=5.1
six
pyserial
yamlordereddictloader
pyparsing
Expand Down
5 changes: 0 additions & 5 deletions tests/unit/facts/test_swver.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
__author__ = "Stacy Smith"
__credits__ = "Jeremy Schulman, Nitin Kumar"

import six

try:
import unittest2 as unittest
except:
Expand All @@ -13,9 +11,6 @@


class TestVersionInfo(unittest.TestCase):
if six.PY2:
assertCountEqual = unittest.TestCase.assertItemsEqual

def test_version_info_after_type_len_else(self):
self.assertEqual(version_info("12.1X46-D10").build, None)

Expand Down
29 changes: 13 additions & 16 deletions tests/unit/test_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import sys
import os
from lxml import etree
import six
import socket

from jnpr.junos.console import Console
Expand Down Expand Up @@ -40,16 +39,16 @@ def tearDownClass(cls):
def setUp(self, mock_write, mock_expect, mock_open):
tty_netconf.open = MagicMock()
mock_expect.side_effect = [
(1, re.search("(?P<login>ogin:\s*$)", "login: "), six.b("\r\r\n ogin:")),
(1, re.search("(?P<login>ogin:\s*$)", "login: "), b"\r\r\n ogin:"),
(
2,
re.search("(?P<passwd>assword:\s*$)", "password: "),
six.b("\r\r\n password:"),
b"\r\r\n password:",
),
(
3,
re.search("(?P<shell>%|#\s*$)", "junos % "),
six.b("\r\r\nroot@device:~ # "),
b"\r\r\nroot@device:~ # ",
),
]
self.dev = Console(host="1.1.1.1", user="lab", password="lab123", mode="Telnet")
Expand Down Expand Up @@ -87,16 +86,16 @@ def test_telnet_old_fact_warning(self, mock_warn):
def test_login_bad_password(self, mock_write, mock_expect, mock_open):
tty_netconf.open = MagicMock()
mock_expect.side_effect = [
(1, re.search("(?P<login>ogin:\s*$)", "login: "), six.b("\r\r\n ogin:")),
(1, re.search("(?P<login>ogin:\s*$)", "login: "), b"\r\r\n ogin:"),
(
2,
re.search("(?P<passwd>assword:\s*$)", "password: "),
six.b("\r\r\n password:"),
b"\r\r\n password:",
),
(
3,
re.search("(?P<badpasswd>ogin incorrect)", "login incorrect"),
six.b("\r\r\nlogin incorrect"),
b"\r\r\nlogin incorrect",
),
]
self.dev = Console(host="1.1.1.1", user="lab", password="lab123", mode="Telnet")
Expand All @@ -110,16 +109,16 @@ def test_with_context(self, mock_write, mock_expect, mock_open, mock_logout):
tty_netconf.open = MagicMock()

mock_expect.side_effect = [
(1, re.search("(?P<login>ogin:\s*$)", "login: "), six.b("\r\r\n ogin:")),
(1, re.search("(?P<login>ogin:\s*$)", "login: "), b"\r\r\n ogin:"),
(
2,
re.search("(?P<passwd>assword:\s*$)", "password: "),
six.b("\r\r\n password:"),
b"\r\r\n password:",
),
(
3,
re.search("(?P<shell>%|#\s*$)", "junos % "),
six.b("\r\r\nroot@device:~ # "),
b"\r\r\nroot@device:~ # ",
),
]
with Console(
Expand Down Expand Up @@ -175,9 +174,9 @@ def test_console_tty_open_err(self, mock_login, mock_telnet):
def test_console_serial(self, mock_write, mock_expect, mock_open):
tty_netconf.open = MagicMock()
mock_expect.side_effect = [
six.b("\r\r\n Login:"),
six.b("\r\r\n password:"),
six.b("\r\r\nroot@device:~ # "),
b"\r\r\n Login:",
b"\r\r\n password:",
b"\r\r\nroot@device:~ # ",
]
self.dev = Console(host="1.1.1.1", user="lab", password="lab123", mode="serial")
self.dev.open()
Expand Down Expand Up @@ -258,15 +257,13 @@ def test_load_console(
</policy-statement>
</policy-options>"""

mock_read_until.return_value = six.b(
"""
mock_read_until.return_value = b"""
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/15.2I0/junos">
<load-configuration-results>
<ok/>
</load-configuration-results>
</rpc-reply>
]]>]]>"""
)
cu = Config(self.dev)
op = cu.load(xml, format="xml")
cu.commit()
Expand Down