Ejemplo n.º 1
0
    def __GetFlagFileLines(self, filename, parsed_file_stack=None):
        """Returns the useful (!=comments, etc) lines from a file with flags.

    Args:
      filename: A string, the name of the flag file.
      parsed_file_stack: A list of the names of the files that we have
        recursively encountered at the current depth. MUTATED BY THIS FUNCTION
        (but the original value is preserved upon successfully returning from
        function call).

    Returns:
      List of strings. See the note below.

    NOTE(springer): This function checks for a nested --flagfile=<foo>
    tag and handles the lower file recursively. It returns a list of
    all the lines that _could_ contain command flags. This is
    EVERYTHING except whitespace lines and comments (lines starting
    with '#' or '//').
    """
        if parsed_file_stack is None:
            parsed_file_stack = []
        # We do a little safety check for reparsing a file we've already encountered
        # at a previous depth.
        if filename in parsed_file_stack:
            sys.stderr.write(
                'Warning: Hit circular flagfile dependency. Ignoring'
                ' flagfile: %s\n' % (filename, ))
            return []
        else:
            parsed_file_stack.append(filename)

        line_list = []  # All line from flagfile.
        flag_line_list = [
        ]  # Subset of lines w/o comments, blanks, flagfile= tags.
        try:
            file_obj = open(filename, 'r')
        except IOError, e_msg:
            raise exceptions.CantOpenFlagFileError(
                'ERROR:: Unable to open flagfile: %s' % e_msg)
Ejemplo n.º 2
0
    def __GetFlagFileLines(self, filename, parsed_file_stack=None):
        """Returns the useful (!=comments, etc) lines from a file with flags.

    Args:
      filename: A string, the name of the flag file.
      parsed_file_stack: A list of the names of the files that we have
        recursively encountered at the current depth. MUTATED BY THIS FUNCTION
        (but the original value is preserved upon successfully returning from
        function call).

    Returns:
      List of strings. See the note below.

    NOTE(springer): This function checks for a nested --flagfile=<foo>
    tag and handles the lower file recursively. It returns a list of
    all the lines that _could_ contain command flags. This is
    EVERYTHING except whitespace lines and comments (lines starting
    with '#' or '//').
    """
        if parsed_file_stack is None:
            parsed_file_stack = []
        # We do a little safety check for reparsing a file we've already encountered
        # at a previous depth.
        if filename in parsed_file_stack:
            sys.stderr.write(
                'Warning: Hit circular flagfile dependency. Ignoring'
                ' flagfile: %s\n' % (filename, ))
            return []
        else:
            parsed_file_stack.append(filename)

        line_list = []  # All line from flagfile.
        flag_line_list = [
        ]  # Subset of lines w/o comments, blanks, flagfile= tags.
        try:
            file_obj = open(filename, 'r')
        except IOError as e_msg:
            raise exceptions.CantOpenFlagFileError(
                'ERROR:: Unable to open flagfile: %s' % e_msg)

        with file_obj:
            line_list = file_obj.readlines()

        # This is where we check each line in the file we just read.
        for line in line_list:
            if line.isspace():
                pass
            # Checks for comment (a line that starts with '#').
            elif line.startswith('#') or line.startswith('//'):
                pass
            # Checks for a nested "--flagfile=<bar>" flag in the current file.
            # If we find one, recursively parse down into that file.
            elif self.__IsFlagFileDirective(line):
                sub_filename = self.ExtractFilename(line)
                included_flags = self.__GetFlagFileLines(
                    sub_filename, parsed_file_stack=parsed_file_stack)
                flag_line_list.extend(included_flags)
            else:
                # Any line that's not a comment or a nested flagfile should get
                # copied into 2nd position.  This leaves earlier arguments
                # further back in the list, thus giving them higher priority.
                flag_line_list.append(line.strip())

        parsed_file_stack.pop()
        return flag_line_list