41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
import hashlib
|
|
import json
|
|
import resource
|
|
import traceback
|
|
import gc
|
|
import struct
|
|
from pathlib import Path
|
|
|
|
from messenger.attachments.infrastructure.images import RenderLimits, render_variants
|
|
|
|
resource.setrlimit(resource.RLIMIT_AS, (448 << 20, 448 << 20))
|
|
def without_trns(data):
|
|
result = data[:8]
|
|
offset = 8
|
|
while offset < len(data):
|
|
size = struct.unpack('>I', data[offset:offset + 4])[0] + 12
|
|
if data[offset + 4:offset + 8] != b'tRNS':
|
|
result += data[offset:offset + size]
|
|
offset += size
|
|
return result
|
|
|
|
|
|
def probe(path, data, transparency):
|
|
row = {'file': path.name, 'transparency': transparency, 'input_sha256': hashlib.sha256(data).hexdigest(), 'limit_bytes': 448 << 20}
|
|
try:
|
|
row['variants'] = [r.name for r in render_variants(data, RenderLimits(10485760, 40000000))]
|
|
row['result'] = 'ready'
|
|
except Exception as error:
|
|
row['error_type'] = type(error).__name__
|
|
row['frames'] = [{'function': f.name, 'file': Path(f.filename).name, 'line': f.lineno} for f in traceback.extract_tb(error.__traceback__)]
|
|
row['max_rss_kib'] = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
|
|
print(json.dumps(row), flush=True)
|
|
|
|
|
|
for path in sorted(Path('/tmp/validation-fixtures').glob('*.png')):
|
|
data = path.read_bytes()
|
|
probe(path, data, True)
|
|
gc.collect()
|
|
probe(path, without_trns(data), False)
|
|
gc.collect()
|