Commit a62dcc82 authored by Andrew Grieve's avatar Andrew Grieve Committed by Commit Bot

Supersize: Fix archive producing different results for the same input

A) Removes gzip header
B) Fixes sort key for string literals

Bug: 779895
Change-Id: Ia1c9872c42c5564134215b0973d5d9e27dcf966d
Reviewed-on: https://chromium-review.googlesource.com/746377Reviewed-by: default avatarSamuel Huang <huangs@chromium.org>
Commit-Queue: Samuel Huang <huangs@chromium.org>
Commit-Queue: agrieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#512854}
parent 3705fe7e
...@@ -42,13 +42,11 @@ import paths ...@@ -42,13 +42,11 @@ import paths
_MAX_SAME_NAME_ALIAS_COUNT = 40 # 50kb is basically negligable. _MAX_SAME_NAME_ALIAS_COUNT = 40 # 50kb is basically negligable.
def _OpenMaybeGz(path, mode=None): def _OpenMaybeGz(path):
"""Calls `gzip.open()` if |path| ends in ".gz", otherwise calls `open()`.""" """Calls `gzip.open()` if |path| ends in ".gz", otherwise calls `open()`."""
if path.endswith('.gz'): if path.endswith('.gz'):
if mode and 'w' in mode: return gzip.open(path, 'rb')
return gzip.GzipFile(path, mode, 1) return open(path, 'rb')
return gzip.open(path, mode)
return open(path, mode or 'r')
def _StripLinkerAddedSymbolPrefixes(raw_symbols): def _StripLinkerAddedSymbolPrefixes(raw_symbols):
...@@ -390,7 +388,7 @@ def _CreateMergeStringsReplacements(merge_string_syms, ...@@ -390,7 +388,7 @@ def _CreateMergeStringsReplacements(merge_string_syms,
logging.debug('Created %d string literal symbols', sum(len(x) for x in ret)) logging.debug('Created %d string literal symbols', sum(len(x) for x in ret))
logging.debug('Sorting string literals') logging.debug('Sorting string literals')
for symbols in ret: for symbols in ret:
symbols.sort(key=lambda x: x.address) symbols.sort(key=lambda x: (x.address, -x.size))
logging.debug('Deduping string literals') logging.debug('Deduping string literals')
num_removed = 0 num_removed = 0
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
import cStringIO import cStringIO
import calendar import calendar
import contextlib
import datetime import datetime
import gzip import gzip
import json import json
...@@ -187,10 +188,18 @@ def _LoadSizeInfoFromFile(file_obj, size_path): ...@@ -187,10 +188,18 @@ def _LoadSizeInfoFromFile(file_obj, size_path):
size_path=size_path) size_path=size_path)
@contextlib.contextmanager
def _OpenGzipForWrite(path):
# Open in a way that doesn't set any gzip header fields.
with open(path, 'wb') as f:
with gzip.GzipFile(filename='', mode='wb', fileobj=f, mtime=0) as fz:
yield fz
def SaveSizeInfo(size_info, path): def SaveSizeInfo(size_info, path):
"""Saves |size_info| to |path}.""" """Saves |size_info| to |path}."""
if os.environ.get('SUPERSIZE_MEASURE_GZIP') == '1': if os.environ.get('SUPERSIZE_MEASURE_GZIP') == '1':
with gzip.open(path, 'wb') as f: with _OpenGzipForWrite(path) as f:
_SaveSizeInfoToFile(size_info, f) _SaveSizeInfoToFile(size_info, f)
else: else:
# It is seconds faster to do gzip in a separate step. 6s -> 3.5s. # It is seconds faster to do gzip in a separate step. 6s -> 3.5s.
...@@ -199,7 +208,7 @@ def SaveSizeInfo(size_info, path): ...@@ -199,7 +208,7 @@ def SaveSizeInfo(size_info, path):
logging.debug('Serialization complete. Gzipping...') logging.debug('Serialization complete. Gzipping...')
stringio.seek(0) stringio.seek(0)
with gzip.open(path, 'wb') as f: with _OpenGzipForWrite(path) as f:
shutil.copyfileobj(stringio, f) shutil.copyfileobj(stringio, f)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment