Skip to content

Commit

Permalink
Merge pull request #71 from Jainpriyal/display_xml
Browse files Browse the repository at this point in the history
Support for "display xml" with commands
  • Loading branch information
vnitinv committed Apr 1, 2016
2 parents 5da050c + 56a7ca7 commit cc2b085
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
14 changes: 10 additions & 4 deletions lib/jnpr/jsnapy/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,19 @@ def get_xml_reply(self, db, snap):
"ERROR, Database for either pre or post snapshot is not present in given path !!",
extra=self.log_detail)
return
elif os.path.isfile(snap):
elif os.path.isfile(snap) and os.stat(snap).st_size > 0:
xml_value = etree.parse(snap)
##### sometimes snapshot files are empty, when cmd/rpc reply do not contain any value
elif os.path.isfile(snap) and os.stat(snap).st_size <= 0:
self.logger_check.error(
colorama.Fore.RED +
"ERROR, Snapshot file is empty !!",
extra=self.log_detail)
return
else:
self.logger_check.error(
colorama.Fore.RED +
"ERROR, Pre snapshot file: %s is not present in given path !!" %
snap,
"ERROR, Snapshot file is not present in given path !!",
extra=self.log_detail)
return
return xml_value
Expand Down Expand Up @@ -372,7 +378,7 @@ def generate_test_files(
extra=self.log_detail)
try:
if tests[val][0].keys()[0] == 'command':
command = tests[val][0].get('command')
command = tests[val][0].get('command').split('|')[0].strip()
reply_format = tests[val][0].get('format', 'xml')
self.logger_check.info(
colorama.Fore.BLUE +
Expand Down
64 changes: 42 additions & 22 deletions lib/jnpr/jsnapy/snap.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from jnpr.jsnapy import get_path
from jnpr.junos.exception import RpcError
from jnpr.jsnapy.sqlite_store import JsnapSqlite

import lxml

class Parser:

Expand All @@ -23,12 +23,21 @@ def _write_file(self, rpc_reply, format, output_file):
:param format: xml/text
:param output_file: name of file
"""
if isinstance(rpc_reply, bool) and format == "text":
self.logger_snap.error(
colorama.Fore.RED +
"ERROR!! requested node is not present", extra=self.log_detail)
### pyEz returns true if there is no output of given command ###
### Ex. show configuration security certificates returns nothing if its not set


if rpc_reply is True :
with open(output_file, 'w') as f:
f.write("")
self.logger_snap.info(
colorama.Fore.BLUE +
"\nOutput of requested Command/RPC is empty", extra=self.log_detail)

else:
err = rpc_reply.xpath("//rpc-error")
"""
### No need of checking reply, as writing all rpc-replies including error and warbing in snap files
err = rpc_reply.xpath("//rpc-error") if isinstance(rpc_reply, lxml.etree._Element) else list()
if len(err):
self.logger_snap.error(
colorama.Fore.RED +
Expand All @@ -40,8 +49,9 @@ def _write_file(self, rpc_reply, format, output_file):
colorama.Fore.RED +
'.//error-message'), extra=self.log_detail)
else:
with open(output_file, 'w') as f:
f.write(etree.tostring(rpc_reply))
"""
with open(output_file, 'w') as f:
f.write(etree.tostring(rpc_reply))

def _write_warning(
self, reply, db, snap_file, hostname, cmd_name, cmd_format, output_file):
Expand All @@ -64,12 +74,15 @@ def _check_reply(self, rpc_reply, format):
:param format: xml/ text
:return: return false if reply contains error ow return rpc reply
"""
if isinstance(rpc_reply, bool) and format == "text":
self.logger_snap.error(
colorama.Fore.RED +
"ERROR!! requested node is not present", extra=self.log_detail)
if rpc_reply is True :
self.logger_snap.info(
colorama.Fore.BLUE +
"\nOutput of requested Command/RPC is empty", extra=self.log_detail)
return ""
else:
err = rpc_reply.xpath("//rpc-error")
"""
### No need of checking rpc replies as writing all replies inluding warnings and errors in snap files
err = rpc_reply.xpath("//rpc-error") if isinstance(rpc_reply, lxml.etree._Element) else list()
if len(err):
self.logger_snap.error(
colorama.Fore.RED +
Expand All @@ -84,9 +97,11 @@ def _check_reply(self, rpc_reply, format):
err_node.findtext(
colorama.Fore.RED +
'.//error-message'), extra=self.log_detail)
return(False)
else:
return etree.tostring(rpc_reply)
return(False)
"""
return etree.tostring(rpc_reply)


def generate_snap_file(self, output_file, hostname, name, cmd_format):
"""
Expand All @@ -96,7 +111,8 @@ def generate_snap_file(self, output_file, hostname, name, cmd_format):
:param cmd_format: xml/text
:return: return output file
"""
cmd_rpc = re.sub('/|\*|\.|-', '_', name)
name = name.split('|')[0].strip()
cmd_rpc = re.sub('/|\*|\.|-|\|', '_', name)
if os.path.isfile(output_file):
return output_file
else:
Expand Down Expand Up @@ -142,14 +158,20 @@ def run_cmd(self, test_file, t, formats, dev, output_file, hostname, db):
cmd_format = test_file[t][0].get('format', 'xml')
cmd_format = cmd_format if cmd_format in formats else 'xml'
self.command_list.append(command)
cmd_name = '_'.join(command.split())
cmd_name = command.split('|')[0].strip()
cmd_name = '_'.join(cmd_name.split())
try:
self.logger_snap.info(
colorama.Fore.BLUE +
"Taking snapshot for %s ................" %
command,
extra=self.log_detail)
rpc_reply_command = dev.rpc.cli(command, format=cmd_format)
##### for commands containing "| display xml" only text format works in PyEz
if re.search('\|\s+display\s+xml',command):
rpc_reply_command = dev.rpc.cli(command, format='text')
else:
rpc_reply_command = dev.rpc.cli(command, format=cmd_format)

except RpcError as err:
snap_file = self.generate_snap_file(
output_file,
Expand Down Expand Up @@ -189,8 +211,7 @@ def run_cmd(self, test_file, t, formats, dev, output_file, hostname, db):
cmd_name,
cmd_format)
self._write_file(rpc_reply_command, cmd_format, snap_file)
if db['store_in_sqlite'] is True and self._check_reply(
rpc_reply_command, cmd_format):
if db['store_in_sqlite'] is True:
self.store_in_sqlite(
db,
hostname,
Expand Down Expand Up @@ -337,8 +358,7 @@ def run_rpc(self, test_file, t, formats, dev, output_file, hostname, db):
reply_format)
self._write_file(rpc_reply, reply_format, snap_file)

if db['store_in_sqlite'] is True and self._check_reply(
rpc_reply, reply_format):
if db['store_in_sqlite'] is True:
self.store_in_sqlite(
db,
hostname,
Expand Down

0 comments on commit cc2b085

Please sign in to comment.