From 1c743f47ce599353436e328daf17ad648616bde4 Mon Sep 17 00:00:00 2001 From: dineshbaburam91 Date: Wed, 27 Apr 2022 17:23:33 +0530 Subject: [PATCH 01/77] Fixed UT and provided support for lxml version --- lib/jnpr/junos/factory/table.py | 4 +++- tests/unit/factory/test_table.py | 6 +++--- tests/unit/utils/test_sw.py | 4 +++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/jnpr/junos/factory/table.py b/lib/jnpr/junos/factory/table.py index 724c3d988..72eb6d79b 100644 --- a/lib/jnpr/junos/factory/table.py +++ b/lib/jnpr/junos/factory/table.py @@ -245,7 +245,9 @@ def savexml(self, path, hostname=False, timestamp=False, append=None): fname += "_%s" % append path = fname + fext - return etree.ElementTree(self.xml).write(open(path, "wb")) + with open(path, "wb+") as f: + pass + return etree.ElementTree(self.xml).write(path) def to_json(self): """ diff --git a/tests/unit/factory/test_table.py b/tests/unit/factory/test_table.py index 25e8b1b18..870a723ed 100644 --- a/tests/unit/factory/test_table.py +++ b/tests/unit/factory/test_table.py @@ -139,9 +139,9 @@ def test_table_get_RuntimeError(self): def test_table_savexml(self, mock_file, mock_execute): mock_execute.side_effect = self._mock_manager self.ppt.xml = etree.XML("test") - self.ppt.savexml("/vasr/tmssp/foo.xml", hostname=True, append="test") - mock_file.assert_called_once_with("/vasr/tmssp/foo_1.1.1.1_test.xml", "wb") - self.ppt.savexml("/vasr/tmssp/foo.xml", hostname=True, timestamp=True) + self.ppt.savexml("foo.xml", hostname=True, append="test") + mock_file.assert_called_once_with("foo_1.1.1.1_test.xml", "wb+") + self.ppt.savexml("foo.xml", hostname=True, timestamp=True) self.assertEqual(mock_file.call_count, 2) def _read_file(self, fname): diff --git a/tests/unit/utils/test_sw.py b/tests/unit/utils/test_sw.py index f7c566249..c48d6f1a9 100644 --- a/tests/unit/utils/test_sw.py +++ b/tests/unit/utils/test_sw.py @@ -161,7 +161,9 @@ def test_sw_put_ftp(self, mock_ftp_put): dev.facts = facts sw = SW(dev) sw.put(package="test.tgz") - self.assertTrue(call("test.tgz", "/var/tmp") in mock_ftp_put.mock_calls) + self.assertTrue( + any("('test.tgz', '/var/tmp')" in str(s) for s in mock_ftp_put.mock_calls) + ) @patch("jnpr.junos.utils.scp.SCP.__exit__") @patch("jnpr.junos.utils.scp.SCP.__init__") From 96f25bb8aa006e12e48902a91a1dc6ff595bdd2d Mon Sep 17 00:00:00 2001 From: dineshbaburam91 Date: Wed, 27 Apr 2022 17:23:33 +0530 Subject: [PATCH 02/77] Fixed UT and provided support for lxml version. --- .github/workflows/pylint.yml | 4 ++-- lib/jnpr/junos/factory/table.py | 4 +++- tests/unit/factory/test_table.py | 6 +++--- tests/unit/utils/test_sw.py | 4 +++- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index daaf86ae2..b6f561ba9 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -14,7 +14,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, windows-latest] - python-version: [3.7, 3.8] + python-version: [3.7, 3.8, 3.9] exclude: - os: windows-latest python-version: 3.8 @@ -38,7 +38,7 @@ jobs: - name: Run black tool run: | pip install -U black; - black --check --exclude=docs/* . + black --check --diff --exclude="docs|build|tests|samples" . - name: Run unit tests run: | diff --git a/lib/jnpr/junos/factory/table.py b/lib/jnpr/junos/factory/table.py index 724c3d988..72eb6d79b 100644 --- a/lib/jnpr/junos/factory/table.py +++ b/lib/jnpr/junos/factory/table.py @@ -245,7 +245,9 @@ def savexml(self, path, hostname=False, timestamp=False, append=None): fname += "_%s" % append path = fname + fext - return etree.ElementTree(self.xml).write(open(path, "wb")) + with open(path, "wb+") as f: + pass + return etree.ElementTree(self.xml).write(path) def to_json(self): """ diff --git a/tests/unit/factory/test_table.py b/tests/unit/factory/test_table.py index 25e8b1b18..870a723ed 100644 --- a/tests/unit/factory/test_table.py +++ b/tests/unit/factory/test_table.py @@ -139,9 +139,9 @@ def test_table_get_RuntimeError(self): def test_table_savexml(self, mock_file, mock_execute): mock_execute.side_effect = self._mock_manager self.ppt.xml = etree.XML("test") - self.ppt.savexml("/vasr/tmssp/foo.xml", hostname=True, append="test") - mock_file.assert_called_once_with("/vasr/tmssp/foo_1.1.1.1_test.xml", "wb") - self.ppt.savexml("/vasr/tmssp/foo.xml", hostname=True, timestamp=True) + self.ppt.savexml("foo.xml", hostname=True, append="test") + mock_file.assert_called_once_with("foo_1.1.1.1_test.xml", "wb+") + self.ppt.savexml("foo.xml", hostname=True, timestamp=True) self.assertEqual(mock_file.call_count, 2) def _read_file(self, fname): diff --git a/tests/unit/utils/test_sw.py b/tests/unit/utils/test_sw.py index f7c566249..c48d6f1a9 100644 --- a/tests/unit/utils/test_sw.py +++ b/tests/unit/utils/test_sw.py @@ -161,7 +161,9 @@ def test_sw_put_ftp(self, mock_ftp_put): dev.facts = facts sw = SW(dev) sw.put(package="test.tgz") - self.assertTrue(call("test.tgz", "/var/tmp") in mock_ftp_put.mock_calls) + self.assertTrue( + any("('test.tgz', '/var/tmp')" in str(s) for s in mock_ftp_put.mock_calls) + ) @patch("jnpr.junos.utils.scp.SCP.__exit__") @patch("jnpr.junos.utils.scp.SCP.__init__") From e50606db402747c2711a4aa846a53961cd399f9d Mon Sep 17 00:00:00 2001 From: chidanandpujar <46497833+chidanandpujar@users.noreply.github.com> Date: Fri, 29 Apr 2022 11:53:06 +0530 Subject: [PATCH 03/77] Py junos eznc issue 1034 fix2 (#1172) * Fixed compilation errors issue#1034 * Fixed compilation errors issue#1034 * Fixed compilation errors issue#1034 * Fixed compilation errors issue#1034 * Fixed compilation errors issue#1034 * Fixed compilation errors issue#1034 * Fixed compilation errors issue#1034 * Fixed compilation errors issue#1034 * Fixed compilation errors issue#1034 * Fixed compilation errors issue#1034 * Fixed compilation errors issue#1034 * Fixed compilation errors issue#1034 * Fixed compilation errors issue#1034 --- lib/jnpr/junos/device.py | 8 +++--- lib/jnpr/junos/exception.py | 2 +- lib/jnpr/junos/factory/cmdtable.py | 2 +- lib/jnpr/junos/factory/optable.py | 2 +- lib/jnpr/junos/factory/state_machine.py | 34 +++++++++++++----------- lib/jnpr/junos/facts/personality.py | 4 +-- lib/jnpr/junos/facts/swver.py | 2 +- lib/jnpr/junos/ofacts/personality.py | 6 ++--- lib/jnpr/junos/ofacts/routing_engines.py | 2 +- lib/jnpr/junos/rpcmeta.py | 2 +- lib/jnpr/junos/transport/tty.py | 12 ++++----- lib/jnpr/junos/utils/config.py | 2 +- lib/jnpr/junos/utils/ftp.py | 2 +- lib/jnpr/junos/utils/start_shell.py | 4 +-- lib/jnpr/junos/utils/sw.py | 14 +++++----- 15 files changed, 50 insertions(+), 48 deletions(-) diff --git a/lib/jnpr/junos/device.py b/lib/jnpr/junos/device.py index e1123fedf..440ed9a7e 100644 --- a/lib/jnpr/junos/device.py +++ b/lib/jnpr/junos/device.py @@ -430,7 +430,7 @@ def re_name(self): if re_name is None: # Still haven't figured it out. Is this a bsys? for re_state in self.facts["current_re"]: - match = re.search("^re\d+$", re_state) + match = re.search(r"^re\d+$", re_state) if match: re_string = "bsys-" + match.group(0) if re_string in self.facts["hostname_info"].keys(): @@ -879,7 +879,7 @@ def execute(self, rpc_cmd, ignore_warning=False, **kvargs): except ValueError as ex: # when data is {}{.*} types if str(ex).startswith("Extra data"): - return json.loads(re.sub("\s?{\s?}\s?", "", rpc_rsp_e.text)) + return json.loads(re.sub(r"\s?{\s?}\s?", "", rpc_rsp_e.text)) else: raise JSONLoadError(ex, rpc_rsp_e.text) else: @@ -906,7 +906,7 @@ def execute(self, rpc_cmd, ignore_warning=False, **kvargs): # protocol: operation-failed # error: device asdf not found # - if rpc_rsp_e.text is not None and rpc_rsp_e.text.strip() is not "": + if rpc_rsp_e.text is not None and rpc_rsp_e.text.strip() != "": return rpc_rsp_e # no children, so assume it means we are OK return True @@ -1342,7 +1342,7 @@ def open(self, *vargs, **kvargs): """ auto_probe = kvargs.get("auto_probe", self._auto_probe) - if auto_probe is not 0: + if auto_probe != 0: if not self.probe(auto_probe): raise EzErrors.ProbeError(self) diff --git a/lib/jnpr/junos/exception.py b/lib/jnpr/junos/exception.py index 03a80b400..3393d860d 100644 --- a/lib/jnpr/junos/exception.py +++ b/lib/jnpr/junos/exception.py @@ -329,7 +329,7 @@ def __init__(self, exception, rpc_content): self.ex_msg = str(exception) self.rpc_content = rpc_content self.offending_line = "" - obj = re.search("line (\d+)", self.ex_msg) + obj = re.search(r"line (\d+)", self.ex_msg) if obj: line_no = int(obj.group(1)) rpc_lines = rpc_content.splitlines() diff --git a/lib/jnpr/junos/factory/cmdtable.py b/lib/jnpr/junos/factory/cmdtable.py index ea75ed396..83151bc63 100644 --- a/lib/jnpr/junos/factory/cmdtable.py +++ b/lib/jnpr/junos/factory/cmdtable.py @@ -54,7 +54,7 @@ def __init__(self, dev=None, raw=None, path=None, template_dir=None): # ------------------------------------------------------------------------- def get(self, *vargs, **kvargs): - """ + r""" Retrieve the XML (string blob under tag of table data from the Device instance and returns back the Table instance - for call-chaining purposes. diff --git a/lib/jnpr/junos/factory/optable.py b/lib/jnpr/junos/factory/optable.py index 4cc99b565..5cefd7c3e 100644 --- a/lib/jnpr/junos/factory/optable.py +++ b/lib/jnpr/junos/factory/optable.py @@ -21,7 +21,7 @@ class OpTable(Table): @checkSAXParserDecorator def get(self, *vargs, **kvargs): - """ + r""" Retrieve the XML table data from the Device instance and returns back the Table instance - for call-chaining purposes. diff --git a/lib/jnpr/junos/factory/state_machine.py b/lib/jnpr/junos/factory/state_machine.py index 372115dfe..7053b76dd 100644 --- a/lib/jnpr/junos/factory/state_machine.py +++ b/lib/jnpr/junos/factory/state_machine.py @@ -297,11 +297,11 @@ def _parse_item_iter(self, lines): self._data[master_key][key] = StateMachine(tbl).parse(lines) else: self._table.TITLE = lines[0] - delimiter = self._table.DELIMITER or "\s\s+" + delimiter = self._table.DELIMITER or r"\s\s+" temp_dict = {} pre_space_delimit = self._get_pre_space_delimiter(lines[1]) for line in lines[1:]: - if re.match(pre_space_delimit + "\s+", line): + if re.match(pre_space_delimit + r"\s+", line): break if line.startswith(pre_space_delimit): try: @@ -311,9 +311,9 @@ def _parse_item_iter(self, lines): temp_dict[key] = value except ValueError: regex = ( - "(\d+)\s(.*)" + r"(\d+)\s(.*)" if item_types[0] == int - else "(" ".*)\s(\d+)" + else "(" r".*)\s(\d+)" ) obj = re.search(regex, line) if obj: @@ -455,14 +455,14 @@ def parse_raw_columns(self, event): user_defined_columns.pop(x) break key = self._get_key(event.kwargs.get("key", self._table.KEY)) - items = re.split("\s\s+", self._lines[1].strip()) + items = re.split(r"\s\s+", self._lines[1].strip()) post_integer_data_types = event.kwargs.get("check", list(map(data_type, items))) index = event.kwargs.get("index", 1) # col_len = len(col_order) columns_list = list(col_order.values()) for index, line in enumerate(self._lines[index:], start=index): - items = re.split("\s\s+", line.strip()) + items = re.split(r"\s\s+", line.strip()) if len(items) >= len(columns_list): if len(items) > len(columns_list): if ( @@ -587,7 +587,7 @@ def prev_next_row_same_type(self, event): index = event.kwargs.get("index") post_integer_data_types = event.kwargs.get("check") line = self._lines[index] - items = re.split("\s\s+", line.strip()) + items = re.split(r"\s\s+", line.strip()) post_integer_data_types, pre_integer_data_types = ( list(map(data_type, items)), post_integer_data_types, @@ -620,7 +620,7 @@ def _get_pre_space_delimiter(self, line): TX Packets 512-1023 Octets 526513 """ pre_space_delimit = "" - obj = re.search("(\s+).*", line) + obj = re.search(r"(\s+).*", line) if obj: pre_space_delimit = obj.group(1) return pre_space_delimit @@ -705,10 +705,10 @@ def parse_title_data(self, event): and not self._view.REGEX ): return - delimiter = self._table.DELIMITER or "\s\s+" + delimiter = self._table.DELIMITER or r"\s\s+" pre_space_delimit = self._get_pre_space_delimiter(self._lines[1]) for line in self._lines[1:]: - if re.match(pre_space_delimit + "\s+", line): + if re.match(pre_space_delimit + r"\s+", line): break if line.startswith(pre_space_delimit): try: @@ -722,7 +722,9 @@ def parse_title_data(self, event): elif key in self._table.KEY_ITEMS: self._data[self._view.FIELDS.get(key, key)] = value except ValueError: - regex = "(\d+)\s(.*)" if item_types[0] == int else "(" ".*)\s(\d+)" + regex = ( + r"(\d+)\s(.*)" if item_types[0] == int else "(" r".*)\s(\d+)" + ) obj = re.search(regex, line) if obj: items = obj.groups() @@ -734,7 +736,7 @@ def parse_title_data(self, event): return self._data def parse_using_regex(self, event): - """ + r""" All the regex should add up to match a line Args: @@ -819,7 +821,7 @@ def parse_using_regex(self, event): ) def parse_using_item_and_regex(self, event): - """ + r""" when multiple map is provided for regex, they gets added to search each line. But when item is '*' each regex item is used to search given value regular expression in whole string blob. @@ -922,7 +924,7 @@ def parse_using_delimiter(self, event): 'PTP': 0} """ - delimiter = self._table.DELIMITER or "\s\s+" + delimiter = self._table.DELIMITER or r"\s\s+" pre_space_delimit = "" if self._table.TITLE is None: for line in self._lines[1:]: @@ -930,7 +932,7 @@ def parse_using_delimiter(self, event): self._lines = self._lines[self._lines.index(line) + 1 :] break else: - obj = re.search("^(\s+).*", self._lines[1]) + obj = re.search(r"^(\s+).*", self._lines[1]) if obj: pre_space_delimit = obj.group(1) for index, line in enumerate(self._lines[1:]): @@ -981,7 +983,7 @@ def parse_exists(self, event): self._data[key] = re.search(search, self._raw, re.I | re.M) is not None def _insert_eval_data(self, tmp_dict): - """ + r""" Args: tmp_dict: dictionary of key value from a iteration of view diff --git a/lib/jnpr/junos/facts/personality.py b/lib/jnpr/junos/facts/personality.py index 09aaaa21b..08d9a736a 100644 --- a/lib/jnpr/junos/facts/personality.py +++ b/lib/jnpr/junos/facts/personality.py @@ -65,14 +65,14 @@ def get_facts(device): virtual = True else: virtual = False - elif re.match("SRX\s?(\d){4}", model): + elif re.match(r"SRX\s?(\d){4}", model): srx_model = int(model[-4:]) if srx_model > 5000: personality = "SRX_HIGHEND" else: personality = "SRX_MIDRANGE" virtual = False - elif re.match("SRX\s?(\d){3}", model): + elif re.match(r"SRX\s?(\d){3}", model): personality = "SRX_BRANCH" virtual = False elif re.search("firefly", model, re.IGNORECASE): diff --git a/lib/jnpr/junos/facts/swver.py b/lib/jnpr/junos/facts/swver.py index 54d9bb199..11493be56 100644 --- a/lib/jnpr/junos/facts/swver.py +++ b/lib/jnpr/junos/facts/swver.py @@ -13,7 +13,7 @@ def __init__(self, verstr): if "X" == self.type: # assumes form similar to "45-D10", so extract the bits from this - xm = re.match("(\d+)-(\w)(\d+)", self.minor) + xm = re.match(r"(\d+)-(\w)(\d+)", self.minor) if xm is not None: self.minor = tuple([int(xm.group(1)), xm.group(2), int(xm.group(3))]) if len(after_type) < 2: diff --git a/lib/jnpr/junos/ofacts/personality.py b/lib/jnpr/junos/ofacts/personality.py index 46bdfdc07..37ab9bd00 100644 --- a/lib/jnpr/junos/ofacts/personality.py +++ b/lib/jnpr/junos/ofacts/personality.py @@ -9,7 +9,7 @@ def facts_personality(junos, facts): examine = model else: for fact in facts: - if re.match("^RE\d", fact): + if re.match(r"^RE\d", fact): examine = facts[fact]["model"] break @@ -34,9 +34,9 @@ def facts_personality(junos, facts): persona = "T" elif examine.startswith("PTX"): persona = "PTX" - elif re.match("SRX\s?(\d){4}", examine): + elif re.match(r"SRX\s?(\d){4}", examine): persona = "SRX_HIGHEND" - elif re.match("SRX\s?(\d){3}", examine): + elif re.match(r"SRX\s?(\d){3}", examine): persona = "SRX_BRANCH" elif re.search("firefly", examine, re.IGNORECASE): facts["virtual"] = True diff --git a/lib/jnpr/junos/ofacts/routing_engines.py b/lib/jnpr/junos/ofacts/routing_engines.py index b105d29e1..ac89ec982 100644 --- a/lib/jnpr/junos/ofacts/routing_engines.py +++ b/lib/jnpr/junos/ofacts/routing_engines.py @@ -65,7 +65,7 @@ def facts_routing_engines(junos, facts): re_name = re_name + slot_id else: # multi-instance routing platform - m = RE.search("(\d)", x_re_name[0].text) + m = RE.search(r"(\d)", x_re_name[0].text) if vc_info is not None: # => RE0-RE0 | RE0-RE1 re_name = "RE{}-RE{}".format(m.group(0), re.find("slot").text) diff --git a/lib/jnpr/junos/rpcmeta.py b/lib/jnpr/junos/rpcmeta.py index 85be90845..d667c1927 100644 --- a/lib/jnpr/junos/rpcmeta.py +++ b/lib/jnpr/junos/rpcmeta.py @@ -154,7 +154,7 @@ def get_config( self._junos.transform = transform # in case of model provided top level should be data # return response - if model and filter_xml is None and options.get("format") is not "json": + if model and filter_xml is None and options.get("format") != "json": response = response.getparent() response.tag = "data" return response diff --git a/lib/jnpr/junos/transport/tty.py b/lib/jnpr/junos/transport/tty.py index 2b418051f..b356ee61e 100644 --- a/lib/jnpr/junos/transport/tty.py +++ b/lib/jnpr/junos/transport/tty.py @@ -44,17 +44,17 @@ class Terminal(object): _ST_TTY_OPTION = 7 _ST_TTY_HOTKEY = 8 - _re_pat_login = "(?Pogin:\s*$)" + _re_pat_login = r"(?Pogin:\s*$)" _RE_PAT = [ - "(?Poader>\s*$)", + r"(?Poader>\s*$)", _re_pat_login, - "(?Password:\s*$)", + r"(?Password:\s*$)", "(?Pogin incorrect)", - "(?P\s*)", - "(?P%|#|(~\$)\s*$)", + r"(?P\s*)", + r"(?P%|#|(~\$)\s*$)", '(?P[^\\-"]>\s*$)', - "(?P