def GetDataPackPair(self, lang, encoding):
        """Returns a (id, string) pair that represents the resource id and raw
    bytes of the data.  This is used to generate the data pack data file.
    """
        # TODO(benrg/joi): Move this and other implementations of GetDataPackPair
        # to grit.format.data_pack?
        from grit.format import rc_header
        id_map = rc_header.GetIds(self.GetRoot())
        id = id_map[self.GetTextualIds()[0]]
        filename = self.ToRealPath(self.GetInputPath())
        if self.attrs['flattenhtml'] == 'true':
            allow_external_script = self.attrs['allowexternalscript'] == 'true'
            data = self._GetFlattenedData(
                allow_external_script=allow_external_script)
        else:
            data = util.ReadFile(filename, util.BINARY)
        # Note that the minifier will only do anything if a minifier command
        # has been set in the command line.
        data = minifier.Minify(data, filename)
        use_gzip = self.attrs.get('compress', '') == 'gzip'
        if use_gzip and self.GetRoot().target_platform != 'ios':
            # 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:
                data = grit.format.gzip_string.GzipStringRsyncable(data)
            else:
                data = grit.format.gzip_string.GzipString(data)

        # Include does not care about the encoding, because it only returns binary
        # data.
        return id, data
Ejemplo n.º 2
0
 def GetDataPackPair(self, lang, encoding):
     """Returns a (id, string|None) pair that represents the resource id and raw
 bytes of the data (or None if no resource is generated).  This is used to
 generate the data pack data file.
 """
     from grit.format import rc_header
     id_map = rc_header.GetIds(self.GetRoot())
     id = id_map[self.GetTextualIds()[0]]
     data = self.gatherer.GetData(lang, encoding)
     return id, data
Ejemplo n.º 3
0
def FormatInclude(item, lang, output_dir, type=None, process_html=False):
    '''Formats an item that is included in an .rc file (e.g. an ICON).

  Args:
    item: an IncludeNode or StructureNode
    lang, output_dir: standard formatter parameters
    type: .rc file resource type, e.g. 'ICON' (ignored unless item is a
          StructureNode)
    process_html: False/True (ignored unless item is a StructureNode)
  '''
    assert isinstance(lang, types.StringTypes)
    from grit.node import structure
    from grit.node import include
    assert isinstance(item, (structure.StructureNode, include.IncludeNode))

    if isinstance(item, include.IncludeNode):
        type = item.attrs['type'].upper()
        process_html = item.attrs['flattenhtml'] == 'true'
        filename_only = item.attrs['filenameonly'] == 'true'
        relative_path = item.attrs['relativepath'] == 'true'
    else:
        assert (isinstance(item, structure.StructureNode)
                and item.attrs['type'] in [
                    'admin_template', 'chrome_html', 'chrome_scaled_image',
                    'igoogle', 'muppet', 'tr_html', 'txt'
                ])
        filename_only = False
        relative_path = False

    # By default, we use relative pathnames to included resources so that
    # sharing the resulting .rc files is possible.
    #
    # The FileForLanguage() Function has the side effect of generating the file
    # if needed (e.g. if it is an HTML file include).
    file_for_lang = item.FileForLanguage(lang, output_dir)
    if file_for_lang is None:
        return ''

    filename = os.path.abspath(file_for_lang)
    if process_html:
        filename = item.Process(output_dir)
    elif filename_only:
        filename = os.path.basename(filename)
    elif relative_path:
        filename = util.MakeRelativePath(output_dir, filename)

    filename = filename.replace('\\', '\\\\')  # escape for the RC format

    if isinstance(item, structure.StructureNode) and item.IsExcludedFromRc():
        return ''

    name = item.attrs['name']
    item_id = rc_header.GetIds(item.GetRoot())[name]
    return '// ID: %d\n%-18s %-18s "%s"\n' % (item_id, name, type, filename)
Ejemplo n.º 4
0
    def GetDataPackPair(self, lang, encoding):
        '''Returns a (id, string) pair that represents the string id and the string
    in the specified encoding, where |encoding| is one of the encoding values
    accepted by util.Encode.  This is used to generate the data pack data file.
    '''
        from grit.format import rc_header
        id_map = rc_header.GetIds(self.GetRoot())
        id = id_map[self.GetTextualIds()[0]]

        message = self.ws_at_start + self.Translate(lang) + self.ws_at_end
        return id, util.Encode(message, encoding)
Ejemplo n.º 5
0
def _FormatSource(get_key, root, lang, output_dir):
    from grit.format import rc_header
    from grit.node import include, structure
    yield _FormatSourceHeader(root)
    tids = rc_header.GetIds(root)
    seen = set()
    for item in root:
        if isinstance(item, (include.IncludeNode, structure.StructureNode)):
            key = get_key(item)
            tid = item.attrs['name']
            if tid in tids and key not in seen:
                seen.add(key)
                yield '  {"%s", %s},\n' % (key, tid)
    yield _FormatSourceFooter(root)
Ejemplo n.º 6
0
 def GetDataPackPair(self, lang, encoding):
     """Returns a (id, string|None) pair that represents the resource id and raw
 bytes of the data (or None if no resource is generated).  This is used to
 generate the data pack data file.
 """
     from grit.format import rc_header
     id_map = rc_header.GetIds(self.GetRoot())
     id = id_map[self.GetTextualIds()[0]]
     if self.ExpandVariables():
         text = self.gatherer.GetText()
         data = util.Encode(self._Substitute(text), encoding)
     else:
         data = self.gatherer.GetData(lang, encoding)
     return id, self.CompressDataIfNeeded(data)
Ejemplo n.º 7
0
  def GetDataPackPair(self, lang, encoding):
    '''Returns a (id, string) pair that represents the string id and the string
    in utf8.  This is used to generate the data pack data file.
    '''
    from grit.format import rc_header
    id_map = rc_header.GetIds(self.GetRoot())
    id = id_map[self.GetTextualIds()[0]]

    message = self.ws_at_start + self.Translate(lang) + self.ws_at_end

    # |message| is a python unicode string, so convert to a byte stream that
    # has the correct encoding requested for the datapacks. We skip the first
    # 2 bytes of text resources because it is the BOM.
    if encoding == UTF8:
      return id, message.encode('utf8')
    if encoding == UTF16:
      return id, message.encode('utf16')[2:]
    # Default is BINARY
    return id, message
Ejemplo n.º 8
0
def _FormatSource(get_key, root, lang, output_dir):
    from grit.format import rc_header
    from grit.node import include, structure, message
    yield _FormatSourceHeader(root)
    tids = rc_header.GetIds(root)
    seen = set()
    active_descendants = [item for item in root.ActiveDescendants()]
    for item in root:
        if isinstance(item, (include.IncludeNode, structure.StructureNode,
                             message.MessageNode)):
            key = get_key(item)
            tid = item.attrs['name']
            if tid in tids and key not in seen:
                seen.add(key)
                # For messages, only include the active ones
                if not isinstance(item, message.MessageNode) \
                    or item in active_descendants:
                    yield '  {"%s", %s},\n' % (key, tid)
    yield _FormatSourceFooter(root)
Ejemplo n.º 9
0
def _FormatSource(get_key, root, lang, output_dir):
    from grit.format import rc_header
    from grit.node import include, structure, message
    yield _FormatSourceHeader(root, output_dir)
    tids = rc_header.GetIds(root)
    seen = set()
    active_descendants = [item for item in root.ActiveDescendants()]
    output_all_resource_defines = root.ShouldOutputAllResourceDefines()
    for item in root:
        if not item.IsResourceMapSource():
            continue
        key = get_key(item)
        tid = item.attrs['name']
        if tid not in tids or key in seen:
            continue
        if item.GeneratesResourceMapEntry(output_all_resource_defines, item
                                          in active_descendants):
            seen.add(key)
            yield '  {"%s", %s},\n' % (key, tid)
    yield _FormatSourceFooter(root)
Ejemplo n.º 10
0
    def GetDataPackPair(self, lang, encoding):
        """Returns a (id, string) pair that represents the resource id and raw
    bytes of the data.  This is used to generate the data pack data file.
    """
        # TODO(benrg/joi): Move this and other implementations of GetDataPackPair
        # to grit.format.data_pack?
        from grit.format import rc_header
        id_map = rc_header.GetIds(self.GetRoot())
        id = id_map[self.GetTextualIds()[0]]
        if self.attrs['flattenhtml'] == 'true':
            allow_external_script = self.attrs['allowexternalscript'] == 'true'
            data = self._GetFlattenedData(
                allow_external_script=allow_external_script)
        else:
            filename = self.ToRealPath(self.GetInputPath())
            data = util.ReadFile(filename, util.BINARY)

        # Include does not care about the encoding, because it only returns binary
        # data.
        return id, data
Ejemplo n.º 11
0
    def GetDataPackPair(self, lang, encoding):
        """Returns a (id, string) pair that represents the resource id and raw
    bytes of the data.  This is used to generate the data pack data file.
    """
        # TODO(benrg/joi): Move this and other implementations of GetDataPackPair
        # to grit.format.data_pack?
        from grit.format import rc_header
        id_map = rc_header.GetIds(self.GetRoot())
        id = id_map[self.GetTextualIds()[0]]
        if self.attrs['flattenhtml'] == 'true':
            allow_external_script = self.attrs['allowexternalscript'] == 'true'
            data = self._GetFlattenedData(
                allow_external_script=allow_external_script)
        else:
            filename = self.ToRealPath(self.GetInputPath())
            data = util.ReadFile(filename, util.BINARY)

        if 'compress' in self.attrs and self.attrs['compress'] == 'gzip':
            # 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:
                data = grit.format.gzip_string.GzipStringRsyncable(data)
            else:
                data = grit.format.gzip_string.GzipString(data)
            data = self.RESERVED_HEADER[0] + data
        elif data[:3] == self.RESERVED_HEADER:
            # We are reserving these 3 bytes as the header for gzipped files in the
            # data pack. 1f:8b is the first two bytes of a gzipped header, and ff is
            # a custom byte we throw in front of the gzip header so that we prevent
            # accidentally throwing this error on a resource we gzipped beforehand and
            # don't wish to compress again. If this exception is hit, change the first
            # byte of RESERVED_HEADER, and then mirror that update in
            # ui/base/resource/resource_bundle.h
            raise exception.ReservedHeaderCollision()

        # Include does not care about the encoding, because it only returns binary
        # data.
        return id, data