示例#1
0
 def GetOutputFiles(self):
     '''Returns the list of <file> nodes that are children of this node's
 <outputs> child.'''
     for child in self.children:
         if child.name == 'outputs':
             return child.children
     raise exception.MissingElement()
示例#2
0
 def GetOutputFiles(self):
   """Returns the list of <output> nodes that are descendants of this node's
   <outputs> child and are not enclosed by unsatisfied <if> conditionals.
   """
   for child in self.children:
     if child.name == 'outputs':
       return [node for node in child.ActiveDescendants()
                    if node.name == 'output']
   raise exception.MissingElement()
示例#3
0
 def startElement(self, name, attrs):
     if self.depth:
         self.parent.startElement(name, attrs)
     else:
         if name != 'grit-part':
             raise exception.MissingElement("root tag must be <grit-part>")
         if attrs:
             raise exception.UnexpectedAttribute(
                 "<grit-part> tag must not have attributes")
     self.depth += 1
示例#4
0
 def GetOutputFiles(self):
     """Returns the list of <output> nodes that are descendants of this node's
 <outputs> child and are not enclosed by unsatisfied <if> conditionals.
 """
     for child in self.children:
         if child.name == 'outputs':
             output_files = []
             self._CollectOutputFiles(child.children, output_files)
             return output_files
     raise exception.MissingElement()
示例#5
0
def Parse(filename_or_stream,
          dir=None,
          stop_after=None,
          first_ids_file=None,
          debug=False,
          defines=None,
          tags_to_ignore=None,
          target_platform=None,
          predetermined_ids_file=None):
    '''Parses a GRD file into a tree of nodes (from grit.node).

  If filename_or_stream is a stream, 'dir' should point to the directory
  notionally containing the stream (this feature is only used in unit tests).

  If 'stop_after' is provided, the parsing will stop once the first node
  with this name has been fully parsed (including all its contents).

  If 'debug' is true, lots of information about the parsing events will be
  printed out during parsing of the file.

  If 'first_ids_file' is non-empty, it is used to override the setting for the
  first_ids_file attribute of the <grit> root node. Note that the first_ids_file
  parameter should be relative to the cwd, even though the first_ids_file
  attribute of the <grit> node is relative to the grd file.

  If 'target_platform' is set, this is used to determine the target
  platform of builds, instead of using |sys.platform|.

  Args:
    filename_or_stream: './bla.xml'
    dir: None (if filename_or_stream is a filename) or '.'
    stop_after: 'inputs'
    first_ids_file: 'GRIT_DIR/../gritsettings/resource_ids'
    debug: False
    defines: dictionary of defines, like {'chromeos': '1'}
    target_platform: None or the value that would be returned by sys.platform
        on your target platform.
    predetermined_ids_file: File path to a file containing a pre-determined
        mapping from resource names to resource ids which will be used to assign
        resource ids to those resources.

  Return:
    Subclass of grit.node.base.Node

  Throws:
    grit.exception.Parsing
  '''

    if isinstance(filename_or_stream, six.string_types):
        source = filename_or_stream
        if dir is None:
            dir = util.dirname(filename_or_stream)
    else:
        source = None

    handler = GrdContentHandler(stop_after=stop_after,
                                debug=debug,
                                dir=dir,
                                defines=defines,
                                tags_to_ignore=tags_to_ignore,
                                target_platform=target_platform,
                                source=source)
    try:
        xml.sax.parse(filename_or_stream, handler)
    except StopParsingException:
        assert stop_after
        pass
    except:
        if not debug:
            print(
                "parse exception: run GRIT with the -x flag to debug .grd problems"
            )
        raise

    if handler.root.name != 'grit':
        raise exception.MissingElement("root tag must be <grit>")

    if hasattr(handler.root, 'SetOwnDir'):
        # Fix up the base_dir so it is relative to the input file.
        assert dir is not None
        handler.root.SetOwnDir(dir)

    if isinstance(handler.root, misc.GritNode):
        handler.root.SetPredeterminedIdsFile(predetermined_ids_file)
        if first_ids_file:
            # Make the path to the first_ids_file relative to the grd file,
            # unless it begins with GRIT_DIR.
            GRIT_DIR_PREFIX = 'GRIT_DIR'
            if not (first_ids_file.startswith(GRIT_DIR_PREFIX)
                    and first_ids_file[len(GRIT_DIR_PREFIX)] in ['/', '\\']):
                rel_dir = os.path.relpath(os.getcwd(), dir)
                first_ids_file = util.normpath(
                    os.path.join(rel_dir, first_ids_file))
            handler.root.attrs['first_ids_file'] = first_ids_file
        # Assign first ids to the nodes that don't have them.
        handler.root.AssignFirstIds(filename_or_stream, defines)

    return handler.root
示例#6
0
def Parse(filename_or_stream, dir=None, stop_after=None, first_ids_file=None,
          debug=False, defines=None, tags_to_ignore=None):
  '''Parses a GRD file into a tree of nodes (from grit.node).

  If filename_or_stream is a stream, 'dir' should point to the directory
  notionally containing the stream (this feature is only used in unit tests).

  If 'stop_after' is provided, the parsing will stop once the first node
  with this name has been fully parsed (including all its contents).

  If 'debug' is true, lots of information about the parsing events will be
  printed out during parsing of the file.

  If 'first_ids_file' is non-empty, it is used to override the setting
  for the first_ids_file attribute of the <grit> root node.

  Args:
    filename_or_stream: './bla.xml'
    dir: None (if filename_or_stream is a filename) or '.'
    stop_after: 'inputs'
    first_ids_file: 'GRIT_DIR/../gritsettings/resource_ids'
    debug: False
    defines: dictionary of defines, like {'chromeos': '1'}

  Return:
    Subclass of grit.node.base.Node

  Throws:
    grit.exception.Parsing
  '''

  if dir is None and isinstance(filename_or_stream, types.StringType):
    dir = util.dirname(filename_or_stream)

  handler = GrdContentHandler(stop_after=stop_after, debug=debug, dir=dir,
                              defines=defines, tags_to_ignore=tags_to_ignore)
  try:
    xml.sax.parse(filename_or_stream, handler)
  except StopParsingException:
    assert stop_after
    pass
  except:
    if not debug:
      print "parse exception: run GRIT with the -x flag to debug .grd problems"
    raise

  if handler.root.name != 'grit':
    raise exception.MissingElement("root tag must be <grit>")

  if hasattr(handler.root, 'SetOwnDir'):
    # Fix up the base_dir so it is relative to the input file.
    assert dir is not None
    handler.root.SetOwnDir(dir)

  if isinstance(handler.root, misc.GritNode):
    if first_ids_file:
      handler.root.attrs['first_ids_file'] = first_ids_file
    # Assign first ids to the nodes that don't have them.
    handler.root.AssignFirstIds(filename_or_stream, defines)

  return handler.root