def precommit(config=None, **kwargs): out('Running pylint pre-commit hook') check_pylint_installed() include = get_include_from_config(config) files = [f for f in get_staged_status() if match(f.path, include)] if not files: ok('No files to lint') return 0 with stash_unstaged_changes(files): args = ['pylint'] args.extend([str(f.absolute_path()) for f in files]) status = subprocess.call(args) str_files = ', '.join([str(f.path) for f in files]) if status: fail('Linting error(s) found in {}'.format(str_files)) else: ok('Linting {} was successful'.format(str_files)) return status
def precommit(config=None, **kwargs): # pylint: disable=unused-argument check_pylint_installed() include = get_include_from_config(config) files = [f for f in get_staged_status() if match(f.path, include)] if not files: ok('No staged files to lint.') return 0 arguments = get_pylint_arguments(config) with stash_unstaged_changes(files): ret = 0 for f in files: cmd = ['pylint'] cmd.extend(arguments) cmd.append(str(f.absolute_path())) try: subprocess.run(cmd, check=True, capture_output=True) except subprocess.CalledProcessError as e: ret = e.returncode error(f'Linting error(s) found in {str(f.path)}:') lint_errors = e.stdout.decode( encoding=sys.getdefaultencoding(), errors='replace').split('\n') # Skip the first line that only shows ******** Module blah for line in lint_errors[1:]: out(line) continue ok(f'Linting {str(f.path)} was successful.') return ret
def precommit(config=None, **kwargs): # pylint: disable=unused-argument check_pylint_installed() include = get_include_from_config(config) files = [f for f in get_staged_status() if match(f.path, include)] if not files: ok('No staged files to lint.') return 0 arguments = get_pylint_arguments(config) with stash_unstaged_changes(files): for f in files: cmd = ['pylint'] cmd.extend(arguments) cmd.append(str(f.absolute_path())) proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out_, _ = proc.communicate() if out_: out_ = out_.decode(encoding=sys.getdefaultencoding(), errors='replace').split('\n') for line in out_: out(line) if proc.returncode: error('Linting error(s) found in {}.'.format(str(f.path))) else: ok('Linting {} was successful.'.format(str(f.path))) return proc.returncode
def precommit(config=Union[None, Config], **kwargs) -> int: # pylint: disable=unused-argument out('Running autopep8 pre-commit hook') check_autopep8_installed() include = get_include_from_config(config) files = [f for f in get_staged_status() if match(f.path, include)] ignore_errors = get_ignore_errors_from_config(config) max_line_length = get_default_line_length_from_config(config) experimental = get_experimental_features_from_config(config) if len(files) == 0: ok('No staged files for autopep8 available') return 0 call_str = [ 'autopep8', '-i', '-a', '-r', '--ignore', ",".join(ignore_errors), '--max-line-length', str(max_line_length) ] if experimental: call_str.append('--experimental') with stash_unstaged_changes(files): for f in files: try: subprocess.check_call(call_str + [str(f.absolute_path())]) ok('Running autopep8 on {}'.format(str(f.path))) except subprocess.CalledProcessError as e: error('Running autopep8 on {}'.format(str(f.path))) raise e stage_files_from_status_list(files) return 0
def precommit(config=None, **kwargs): # pylint: disable=unused-argument out('Running isort pre-commit hook') check_isort_installed() include = get_include_from_config(config) files = [f for f in get_staged_status() if match(f.path, include)] if len(files) == 0: ok('No staged files for isort available') return 0 with stash_unstaged_changes(files): for f in files: try: subprocess.check_call(['isort', '-q', str(f.absolute_path())]) ok('Running isort on {}'.format(str(f.path))) except subprocess.CalledProcessError as e: error('Running isort on {}'.format(str(f.path))) raise e stage_files_from_status_list(files) return 0
def test_out(self): out('foo bar') self.term.print.assert_called_with('foo bar')