filereader.py 5.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
# Copyright (C) 2009 Google Inc. All rights reserved.
# Copyright (C) 2010 Chris Jerdonek (chris.jerdonek@gmail.com)
# Copyright (C) 2010 ProFUSION embedded systems
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Supports reading and processing text files."""

import logging
import sys


_log = logging.getLogger(__name__)


class TextFileReader(object):

    """Supports reading and processing text files.

       Attributes:
         file_count: The total number of files passed to this instance
                     for processing, including non-text files and files
                     that should be skipped.
48 49 50 51
         delete_only_file_count: The total number of files that are not
                                 processed this instance actually because
                                 the files don't have any modified lines
                                 but should be treated as processed.
52 53 54

    """

55
    def __init__(self, filesystem, processor):
56 57 58 59 60 61
        """Create an instance.

        Arguments:
          processor: A ProcessorBase instance.

        """
62

63
        self.filesystem = filesystem
64 65
        self._processor = processor
        self.file_count = 0
66
        self.delete_only_file_count = 0
67 68 69 70 71 72 73 74 75 76

    def _read_lines(self, file_path):
        """Read the file at a path, and return its lines.

        Raises:
          IOError: If the file does not exist or cannot be read.

        """
        # Support the UNIX convention of using "-" for stdin.
        if file_path == '-':
77
            file = self.filesystem.open_stdin()
78 79 80 81 82
        else:
            # We do not open the file with universal newline support
            # (codecs does not support it anyway), so the resulting
            # lines contain trailing "\r" characters if we are reading
            # a file with CRLF endings.
83
            file = self.filesystem.open_text_file_for_reading(file_path, 'replace')
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

        try:
            contents = file.read()
        finally:
            file.close()

        lines = contents.split('\n')
        return lines

    def process_file(self, file_path, **kwargs):
        """Process the given file by calling the processor's process() method.

        Args:
          file_path: The path of the file to process.
          **kwargs: Any additional keyword parameters that should be passed
                    to the processor's process() method.  The process()
                    method should support these keyword arguments.

        Raises:
          SystemExit: If no file at file_path exists.

        """
        self.file_count += 1

108
        if not self.filesystem.exists(file_path) and file_path != "-":
109
            _log.error("File does not exist: '%s'" % file_path)
110
            raise IOError("File does not exist")
111

112 113 114 115 116 117 118 119
        if not self._processor.should_process(file_path):
            _log.debug("Skipping file: '%s'" % file_path)
            return
        _log.debug("Processing file: '%s'" % file_path)

        try:
            lines = self._read_lines(file_path)
        except IOError, err:
120
            message = ("Could not read file. Skipping: '%s'\n  %s" % (file_path, err))
121 122 123 124 125 126
            _log.warn(message)
            return

        self._processor.process(lines, file_path, **kwargs)

    def _process_directory(self, directory):
127
        """Process all files in the given directory, recursively."""
128 129
        for file_path in self.filesystem.files_under(directory):
            self.process_file(file_path)
130 131 132

    def process_paths(self, paths):
        for path in paths:
133
            if self.filesystem.isdir(path):
134 135 136
                self._process_directory(directory=path)
            else:
                self.process_file(path)
137 138 139 140 141 142 143 144 145

    def count_delete_only_file(self):
        """Count up files that contains only deleted lines.

        Files which has no modified or newly-added lines don't need
        to check style, but should be treated as checked. For that
        purpose, we just count up the number of such files.
        """
        self.delete_only_file_count += 1