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_black_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 black available.') return 0 arguments = ['black'] arguments.extend(get_black_arguments(config)) with stash_unstaged_changes(files): for f in files: try: args = arguments.copy() args.append(str(f.absolute_path())) subprocess.check_call(args) ok('Running black on {}'.format(str(f.path))) except subprocess.CalledProcessError as e: error('Running black on {}'.format(str(f.path))) raise e from None stage_files_from_status_list(files) return 0
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 autoformat( context: Context = CONTEXT, filepaths: Optional[Iterable[str]] = None, staged: bool = False, ): """Autoformat Python code.""" if get_staged_status is not None and stash_unstaged_changes is not None: pass else: print('Cannot autoformat; missing required autohooks module.') commands = [ # https://isort.readthedocs.io/en/latest/ 'isort', # https://github.com/psf/black 'black -S -q', # https://github.com/myint/autoflake 'autoflake --imports=apps,django,requests,typing,urllib3 --ignore-init-module-imports -i -r', # noqa: E501 ] filepaths: Iterable[str] = filepaths or [] if staged: staged_filepaths = get_staged_status() filepaths += staged_filepaths filepaths = [ filepath for filepath in filepaths if filepath.endswith('.py') ] if filepaths: commands.append( 'unify --in-place') # does not support recursion (directories) if staged: if not filepaths: return with stash_unstaged_changes(staged_filepaths): for filepath in filepaths: for command in commands: context.run(f'{command} {filepath}', warn=True) elif filepaths: for filepath in filepaths: for command in commands: context.run(f'{command} {filepath}', warn=True) else: with context.cd(settings.BASE_DIR): for command in commands: context.run(f'{command} .')
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