1
0
mirror of https://github.com/ARM-software/workload-automation.git synced 2025-09-07 22:02:42 +01:00

json: Replaced json results processor with a more comprehensive one

This commit is contained in:
Sebastian Goscik
2016-01-28 11:00:17 +00:00
parent de133cddb4
commit cd0186d14e
6 changed files with 413 additions and 27 deletions

View File

@@ -815,3 +815,27 @@ def sha256(path, chunk=2048):
def urljoin(*parts):
return '/'.join(p.rstrip('/') for p in parts)
# From: http://eli.thegreenplace.net/2011/10/19/perls-guess-if-file-is-text-or-binary-implemented-in-python/
def istextfile(fileobj, blocksize=512):
""" Uses heuristics to guess whether the given file is text or binary,
by reading a single block of bytes from the file.
If more than 30% of the chars in the block are non-text, or there
are NUL ('\x00') bytes in the block, assume this is a binary file.
"""
_text_characters = (b''.join(chr(i) for i in range(32, 127)) +
b'\n\r\t\f\b')
block = fileobj.read(blocksize)
if b'\x00' in block:
# Files with null bytes are binary
return False
elif not block:
# An empty file is considered a valid text file
return True
# Use translate's 'deletechars' argument to efficiently remove all
# occurrences of _text_characters from the block
nontext = block.translate(None, _text_characters)
return float(len(nontext)) / len(block) <= 0.30