コード例 #1
0
    def CompressDataIfNeeded(self, data):
        '''Compress data using the format specified in the compress attribute.

    Args:
      data: The data to compressed.
    Returns:
      The data in gzipped or brotli compressed format. If the format is
      unspecified then this returns the data uncompressed.
    '''

        compress = self.attrs.get('compress')
        assert not (
            compress != 'default' and compress != 'false'
            and self.attrs.get('file').endswith(
                self._COMPRESS_DISALLOWED_EXTENSIONS)
        ), 'Disallowed |compress| attribute found for %s' % self.attrs.get(
            'name')

        # Compress JS, HTML, CSS and SVG files by default (gzip), unless |compress|
        # is explicitly specified.
        compress_by_default = (compress == 'default'
                               and self.attrs.get('file').endswith(
                                   self._COMPRESS_BY_DEFAULT_EXTENSIONS))

        if compress == 'gzip' or compress_by_default:
            # We only use rsyncable compression on Linux.
            # We exclude ChromeOS since ChromeOS bots are Linux based but do not have
            # the --rsyncable option built in for gzip. See crbug.com/617950.
            if sys.platform == 'linux2' and 'chromeos' not in self.GetRoot(
            ).defines:
                return grit.format.gzip_string.GzipStringRsyncable(data)
            return grit.format.gzip_string.GzipString(data)

        if compress == 'brotli':
            # The length of the uncompressed data as 8 bytes little-endian.
            size_bytes = struct.pack("<q", len(data))
            data = brotli_util.BrotliCompress(data)
            # BROTLI_CONST is prepended to brotli decompressed data in order to
            # easily check if a resource has been brotli compressed.
            # The length of the uncompressed data is also appended to the start,
            # truncated to 6 bytes, little-endian. size_bytes is 8 bytes,
            # need to truncate further to 6.
            formatter = b'%ds %dx %ds' % (6, 2, len(size_bytes) - 8)
            return (constants.BROTLI_CONST +
                    b''.join(struct.unpack(formatter, size_bytes)) + data)

        if compress == 'false' or compress == 'default':
            return data

        raise Exception('Invalid value for compression')
コード例 #2
0
    def CompressDataIfNeeded(self, data):
        '''Compress data using the format specified in the compress attribute.

    Args:
      data: The data to compressed.
    Returns:
      The data in gzipped or brotli compressed format. If the format is
      unspecified then this returns the data uncompressed.
    '''
        if self.attrs.get('compress') in ('gzip', 'true'):
            # We only use rsyncable compression on Linux.
            # We exclude ChromeOS since ChromeOS bots are Linux based but do not have
            # the --rsyncable option built in for gzip. See crbug.com/617950.
            if sys.platform == 'linux2' and 'chromeos' not in self.GetRoot(
            ).defines:
                return grit.format.gzip_string.GzipStringRsyncable(data)
            return grit.format.gzip_string.GzipString(data)

        elif self.attrs.get('compress') == 'brotli':
            # The length of the uncompressed data as 8 bytes little-endian.
            size_bytes = struct.pack("<q", len(data))
            data = brotli_util.BrotliCompress(data)
            # BROTLI_CONST is prepended to brotli decompressed data in order to
            # easily check if a resource has been brotli compressed.
            # The length of the uncompressed data is also appended to the start,
            # truncated to 6 bytes, little-endian. size_bytes is 8 bytes,
            # need to truncate further to 6.
            formatter = '%ds %dx %ds' % (6, 2, len(size_bytes) - 8)
            return (constants.BROTLI_CONST +
                    b''.join(struct.unpack(formatter, size_bytes)) + data)

        elif self.attrs.get('compress') == 'false':
            return data

        else:
            raise Exception('Invalid value for compression')