def check_file(self, path): """ Check one regular file with pylint for py syntax errors. :param path: Path to a regular file. :return: False, if pylint found syntax problems, True, if pylint didn't find problems, or path is not a python module or script. """ inspector = PathInspector(path=path, args=self.args) if inspector.is_toignore(): return True if not inspector.is_python(): return True try: runner = Run(self.get_opts() + [path], exit=False) if runner.linter.msg_status != 0: log.error('Pylint check fail: %s', path) self.failed_paths.append(path) return runner.linter.msg_status == 0 except Exception, details: log.error('Pylint check fail: %s (pylint exception: %s)', path, details) self.failed_paths.append(path) return False
def check_file(self, path): inspector = PathInspector(path=path, args=self.args) if inspector.is_toignore(): return True # Don't put license info in empty __init__.py files. if not inspector.is_python() or inspector.is_empty(): return True first_line = None if inspector.is_script("python"): first_line = inspector.get_first_line() new_content = None with open(path, 'r') as inspected_file: content = inspected_file.readlines() if first_line is not None: content = content[1:] content = "".join(content) if self.base_license_contents not in content: new_content = "" if first_line is not None: new_content += first_line new_content += '\n' new_content += self.license_contents + '\n' + content if new_content is not None: with open(path, 'w') as inspected_file: inspected_file.write(new_content) self.failed_paths.append(path) return False return True
def check_file(self, path): """ Check one regular file with pylint for py syntax errors. :param path: Path to a regular file. :return: False, if pylint found syntax problems, True, if pylint didn't find problems, or path is not a python module or script. """ inspector = PathInspector(path=path, args=self.args) if inspector.is_toignore(): return True if not inspector.is_python(): return True try: opt_obj = pep8.StyleGuide().options ignore_list = self.args.disable.split(',') + list(opt_obj.ignore) opt_obj.ignore = tuple(set(ignore_list)) # pylint: disable=E1123 runner = pep8.Checker(filename=path, options=opt_obj) except Exception: opts = ['--ignore'] + self.args.disable.split(',') pep8.process_options(opts) runner = pep8.Checker(filename=path) try: status = runner.check_all() except: log.error('Unexpected exception while checking %s', path) exc_info = sys.exc_info() stacktrace.log_exc_info(exc_info, 'inspektor.style') status = 1 if status != 0: log.error('PEP8 check fail: %s', path) self.failed_paths.append(path) if AUTOPEP8_CAPABLE: if self.args.fix: log.info('Trying to fix errors with autopep8') try: auto_args = [path, '--in-place', ('--max-line-length=%s' % self.args.max_line_length)] if self.args.disable: auto_args.append("--ignore='%s'" % self.args.disable) opt_obj = autopep8.parse_args(auto_args) autopep8.fix_file(path, opt_obj) except Exception: log.error('Unable to fix errors') exc_info = sys.exc_info() stacktrace.log_exc_info(exc_info, 'inspektor.style') return status == 0
def check_file(self, path): """ Check one regular file with pylint for py syntax errors. :param path: Path to a regular file. :return: False, if pylint found syntax problems, True, if pylint didn't find problems, or path is not a python module or script. """ inspector = PathInspector(path=path, args=self.args) if inspector.is_toignore(): return True if not inspector.is_python(): return True try: opt_obj = pycodestyle.StyleGuide().options ignore_list = self.args.disable.split(',') + list(opt_obj.ignore) opt_obj.ignore = tuple(set(ignore_list)) # pylint: disable=E1123 runner = pycodestyle.Checker(filename=path, options=opt_obj) status = runner.check_all() except Exception: log.error('Unexpected exception while checking %s', path) exc_info = sys.exc_info() stacktrace.log_exc_info(exc_info, 'inspektor.style') status = 1 if status != 0: log.error('Style check fail: %s', path) self.failed_paths.append(path) if AUTOPEP8_CAPABLE: if self.args.fix: log.info('Trying to fix errors with autopep8') try: process.run( 'autopep8 --in-place --max-line-length=%s --ignore %s %s' % (self.args.max_line_length, self.args.disable, path)) except Exception: log.error('Unable to fix errors') exc_info = sys.exc_info() stacktrace.log_exc_info(exc_info, 'inspektor.style') return status == 0
def check_file(self, path): """ Check one regular file with pylint for py syntax errors. :param path: Path to a regular file. :return: False, if reindenter found problems, True, if reindenter didn't find problems, or path is not a python module or script. """ inspector = PathInspector(path=path, args=self.args) if inspector.is_toignore(): return True if not inspector.is_python(): return True f = open(path) r = Run(f) f.close() try: if r.run(): log.error('Indentation check fail : %s', path) self.failed_paths.append(path) if self.args.fix: f = open(path, "w") r.write(f) f.close() log.info('FIX OK') return False else: return True except IndentationError: log.error("Indentation check fail : %s", path) log.error("Automated fix impossible: %s", path) log.error("Look at the stack trace " "below and fix it manually") exc_info = sys.exc_info() stacktrace.log_exc_info(exc_info, 'inspektor.reindent') return False