def _clang_format(*args: str, **kwargs) -> bytes: return log_run('clang-format', '--style=file', *args, stdout=subprocess.PIPE, check=True, **kwargs).stdout
def _yapf(*args, **kwargs) -> subprocess.CompletedProcess: return log_run('python', '-m', 'yapf', '--parallel', *args, capture_output=True, **kwargs)
def check_gn_format(files: Iterable[Path]) -> Dict[Path, str]: """Checks formatting; returns {path: diff} for files with bad formatting.""" return _check_files( files, lambda _, data: log_run('gn', 'format', '--stdin', input=data, stdout=subprocess.PIPE, check=True).stdout)
def _get_paths_from_command(*args, ctx: PresubmitContext, **kwargs): """Runs a command and reads Bazel or GN //-style paths from it.""" process = log_run(*args, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, cwd=ctx.repository_root, **kwargs) files = set() for line in process.stdout.splitlines(): path = line.strip().lstrip(b'/').replace(b':', b'/').decode() path = ctx.repository_root.joinpath(path) if path.is_file(): _LOG.debug('Found file %s', path) files.add(path) return files
def _get_paths_from_command(source_dir: Path, *args, **kwargs) -> Set[Path]: """Runs a command and reads Bazel or GN //-style paths from it.""" process = log_run(*args, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, cwd=source_dir, **kwargs) files = set() for line in process.stdout.splitlines(): path = line.strip().lstrip(b'/').replace(b':', b'/').decode() path = source_dir.joinpath(path) if path.is_file(): _LOG.debug('Found file %s', path) files.add(path) return files
def _get_paths_from_command(source_dir: Path, *args, **kwargs) -> Set[Path]: """Runs a command and reads Bazel or GN //-style paths from it.""" process = log_run(args, capture_output=True, cwd=source_dir, **kwargs) if process.returncode: _LOG.error('Build invocation failed with return code %d!', process.returncode) _LOG.error('[COMMAND] %s\n%s\n%s', *tools.format_command(args, kwargs), process.stderr.decode()) raise PresubmitFailure files = set() for line in process.stdout.splitlines(): path = line.strip().lstrip(b'/').replace(b':', b'/').decode() path = source_dir.joinpath(path) if path.is_file(): files.add(path) return files
def fix_go_format(files: Iterable[Path]) -> None: """Fixes formatting for the provided files in place.""" log_run('gofmt', '-w', *files, check=True)
def check_go_format(files: Iterable[Path]) -> Dict[Path, str]: """Checks formatting; returns {path: diff} for files with bad formatting.""" return _check_files( files, lambda path, _: log_run( 'gofmt', path, stdout=subprocess.PIPE, check=True).stdout)