Skip to content

Commit

Permalink
Added code to retrieve a block log using the tool and improved filter…
Browse files Browse the repository at this point in the history
…JsonObject to also handle json arrays. GH EOSIO#5674
  • Loading branch information
brianjohnson5972 committed Oct 9, 2018
1 parent 4558043 commit 173139c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion tests/Node.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def runMongoCmdReturnJson(cmd, subcommand, trace=False, exitOnError=False):
outStr=Node.byteArrToStr(outs)
if not outStr:
return None
extJStr=Utils.filterJsonObject(outStr)
extJStr=Utils.filterJsonObjectOrArray(outStr)
if not extJStr:
return None
jStr=Node.normalizeJsonObject(extJStr)
Expand Down
45 changes: 40 additions & 5 deletions tests/testUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Utils:
ShuttingDown=False
CheckOutputDeque=deque(maxlen=10)

EosBlockLogPath="programs/eosio-blocklog/eosio-blocklog"

@staticmethod
def Print(*args, **kwargs):
stackDepth=len(inspect.stack())-2
Expand Down Expand Up @@ -136,16 +138,25 @@ def waitForBool(lam, timeout=None):
return False if ret is None else ret

@staticmethod
def filterJsonObject(data):
firstIdx=data.find('{')
lastIdx=data.rfind('}')
retStr=data[firstIdx:lastIdx+1]
def filterJsonObjectOrArray(data):
firstObjIdx=data.find('{')
lastObjIdx=data.rfind('}')
firstArrayIdx=data.find('[')
lastArrayIdx=data.rfind(']')
if firstArrayIdx==-1 or lastArrayIdx==-1:
retStr=data[firstObjIdx:lastObjIdx+1]
elif firstObjIdx==-1 or lastObjIdx==-1:
retStr=data[firstArrayIdx:lastArrayIdx+1]
elif lastArrayIdx < lastObjIdx:
retStr=data[firstObjIdx:lastObjIdx+1]
else:
retStr=data[firstArrayIdx:lastArrayIdx+1]
return retStr

@staticmethod
def runCmdArrReturnJson(cmdArr, trace=False, silentErrors=True):
retStr=Utils.checkOutput(cmdArr)
jStr=Utils.filterJsonObject(retStr)
jStr=Utils.filterJsonObjectOrArray(retStr)
if trace: Utils.Print ("RAW > %s" % (retStr))
if trace: Utils.Print ("JSON> %s" % (jStr))
if not jStr:
Expand Down Expand Up @@ -213,6 +224,30 @@ def pgrepCmd(serverName):

return "pgrep %s %s" % (pgrepOpts, serverName)

@staticmethod
def getBlockLog(blockLogLocation, silentErrors=False, exitOnError=False):
assert(isinstance(blockLogLocation, str))
cmd="%s --blocks-dir %s --as-json-array" % (Utils.EosBlockLogPath, blockLogLocation)
if Utils.Debug: Utils.Print("cmd: %s" % (cmd))
rtn=None
try:
rtn=Utils.runCmdReturnJson(cmd, silentErrors=silentErrors)
except subprocess.CalledProcessError as ex:
if not silentErrors:
msg=ex.output.decode("utf-8")
errorMsg="Exception during \"%s\". %s" % (cmd, msg)
if exitOnError:
Utils.cmdError(errorMsg)
Utils.errorExit(errorMsg)
else:
Utils.Print("ERROR: %s" % (errorMsg))
return None

if exitOnError and rtn is None:
Utils.cmdError("could not \"%s\"" % (cmd))
Utils.errorExit("Failed to \"%s\"" % (cmd))

return rtn

###########################################################################################
class Account(object):
Expand Down

0 comments on commit 173139c

Please sign in to comment.