示例#1
0
文件: checker.py 项目: Gointer/pants
  def check_file(self, filename):
    """Process python file looking for indications of problems.

    :param filename: (str) Python source filename
    :return: (int) number of failures
    """
    try:
      python_file = PythonFile.parse(os.path.join(get_buildroot(), filename))
    except SyntaxError as e:
      print('{filename}:SyntaxError: {error}'.format(filename=filename, error=e))
      return 1

    # If the user specifies an invalid severity use comment.
    severity = Nit.SEVERITY.get(self.options.severity, Nit.COMMENT)

    failure_count = 0
    fail_threshold = Nit.WARNING if self.options.strict else Nit.ERROR

    for i, nit in enumerate(self.get_nits(python_file)):
      if i == 0:
        print()  # Add an extra newline to clean up the output only if we have nits.
      if nit.severity >= severity:
        print('{nit}\n'.format(nit=nit))
      if nit.severity >= fail_threshold:
        failure_count += 1
    return failure_count
示例#2
0
  def check_file(self, filename):
    """Process python file looking for indications of problems.

    :param filename: (str) Python source filename
    :return: (bool) flag indicating failure
    """
    try:
      python_file = PythonFile.parse(filename)
    except SyntaxError as e:
      print('{filename}:SyntaxError: {error}'.format(filename=filename, error=e))
      return True

    # If the user specifies an invalid severity use comment.
    severity = Nit.SEVERITY.get(self.options.severity, Nit.COMMENT)

    should_fail = False
    fail_threshold = Nit.WARNING if self.options.strict else Nit.ERROR

    for i, nit in enumerate(self.get_nits(python_file)):
      if i == 0:
        print()  # Add an extra newline to clean up the output only if we have nits.
      if nit.severity >= severity:
        print('{nit}\n'.format(nit=nit))
      should_fail |= (nit.severity >= fail_threshold)
    return should_fail
示例#3
0
  def get_nits(self, filename):
    """Iterate over the instances style checker and yield Nits.

    :param filename: str pointing to a file within the buildroot.
    """
    try:
      python_file = PythonFile.parse(filename, root=get_buildroot())
    except CheckSyntaxError as e:
      yield e.as_nit()
      return

    if noqa_file_filter(python_file):
      return

    if self.options.suppress:
      # Filter out any suppressed plugins
      check_plugins = [plugin for plugin in self._plugins
                       if self.excluder.should_include(filename, plugin.name)]
    else:
      check_plugins = self._plugins

    for plugin in check_plugins:

      for i, nit in enumerate(plugin.checker(python_file)):
        if i == 0:
          # NB: Add debug log header for nits from each plugin, but only if there are nits from it.
          self.context.log.debug('Nits from plugin {} for {}'.format(plugin.name, filename))

        if not nit.has_lines_to_display:
          yield nit
          continue

        if all(not line_contains_noqa(line) for line in nit.lines):
          yield nit
示例#4
0
  def get_nits(self, filename):
    """Iterate over the instances style checker and yield Nits.

    :param filename: str pointing to a file within the buildroot.
    """
    try:
      python_file = PythonFile.parse(filename, root=get_buildroot())
    except CheckSyntaxError as e:
      yield e.as_nit()
      return

    if noqa_file_filter(python_file):
      return

    if self.options.suppress:
      # Filter out any suppressed plugins
      check_plugins = [plugin for plugin in self._plugins
                       if self.excluder.should_include(filename, plugin.name)]
    else:
      check_plugins = self._plugins

    for plugin in check_plugins:

      for i, nit in enumerate(plugin.checker(python_file)):
        if i == 0:
          # NB: Add debug log header for nits from each plugin, but only if there are nits from it.
          self.context.log.debug('Nits from plugin {} for {}'.format(plugin.name, filename))

        if not nit.has_lines_to_display:
          yield nit
          continue

        if all(not line_contains_noqa(line) for line in nit.lines):
          yield nit
示例#5
0
    def check_file(self, filename):
        """Process python file looking for indications of problems.

    :param filename: (str) Python source filename
    :return: (bool) flag indicating failure
    """
        try:
            python_file = PythonFile.parse(filename)
        except SyntaxError as e:
            print('{filename}:SyntaxError: {error}'.format(filename=filename,
                                                           error=e))
            return True

        # If the user specifies an invalid severity use comment.
        severity = Nit.SEVERITY.get(self.options.severity, Nit.COMMENT)

        should_fail = False
        fail_threshold = Nit.WARNING if self.options.strict else Nit.ERROR

        for i, nit in enumerate(self.get_nits(python_file)):
            if i == 0:
                print(
                )  # Add an extra newline to clean up the output only if we have nits.
            if nit.severity >= severity:
                print('{nit}\n'.format(nit=nit))
            should_fail |= (nit.severity >= fail_threshold)
        return should_fail
示例#6
0
    def check_file(self, filename):
        """Process python file looking for indications of problems.

    :param filename: (str) Python source filename
    :return: (int) number of failures
    """
        try:
            python_file = PythonFile.parse(
                os.path.join(get_buildroot(), filename))
        except SyntaxError as e:
            print('{filename}:SyntaxError: {error}'.format(filename=filename,
                                                           error=e))
            return 1

        # If the user specifies an invalid severity use comment.
        severity = Nit.SEVERITY.get(self.options.severity, Nit.COMMENT)

        failure_count = 0
        fail_threshold = Nit.WARNING if self.options.strict else Nit.ERROR

        for i, nit in enumerate(self.get_nits(python_file)):
            if i == 0:
                print(
                )  # Add an extra newline to clean up the output only if we have nits.
            if nit.severity >= severity:
                print('{nit}\n'.format(nit=nit))
            if nit.severity >= fail_threshold:
                failure_count += 1
        return failure_count
示例#7
0
 def create_python_file(self, file_content):
     if self.file_required:
         tmpdir = safe_mkdtemp()
         with open(os.path.join(tmpdir, 'file.py'), 'wb') as fp:
             fp.write(file_content)
             fp.close()
             return PythonFile.parse(fp.name)
     else:
         return PythonFile.from_statement(file_content)
示例#8
0
 def create_python_file(self, file_content):
   if self.file_required:
     tmpdir = safe_mkdtemp()
     with open(os.path.join(tmpdir, 'file.py'), 'wb') as fp:
       fp.write(file_content)
       fp.close()
       return PythonFile.parse(fp.name)
   else:
     return PythonFile.from_statement(file_content)
示例#9
0
文件: pep8.py 项目: zhaohaobing/pants
 def init_file(self, filename, lines, expected, line_offset):
     super(PantsReporter, self).init_file(filename, lines, expected,
                                          line_offset)
     self._python_file = PythonFile.parse(filename)
     self._errors = []
示例#10
0
 def init_file(self, filename, lines, expected, line_offset):
   super(PantsReporter, self).init_file(filename, lines, expected, line_offset)
   self._python_file = PythonFile.parse(filename)
   self._errors = []
示例#11
0
 def test_python_file_absolute_path_and_root_fails(self):
     with self.assertRaises(ValueError):
         PythonFile.parse('/absolute/dir', root='/other/abs/dir')
示例#12
0
 def test_python_file_absolute_path_and_root_fails(self):
   with self.assertRaises(ValueError):
     PythonFile.parse('/absolute/dir', root='/other/abs/dir')