| 49 | |
| 50 | |
| 51 | def process_file( |
| 52 | orig_filename: str, ruff_bin: str, orig_config: Path, source_root: str, check_format: bool, run_in_source_root: bool |
| 53 | ) -> str: |
| 54 | logger.debug('Check %s with config %s', orig_filename, orig_config) |
| 55 | |
| 56 | file_path = os.path.relpath(orig_filename, source_root) |
| 57 | |
| 58 | if run_in_source_root: |
| 59 | filename = os.path.realpath(orig_filename) if os.path.islink(orig_filename) else orig_filename |
| 60 | config = orig_config.resolve() if orig_config.is_symlink() else orig_config |
| 61 | else: |
| 62 | filename = orig_filename |
| 63 | config = orig_config |
| 64 | |
| 65 | if check_format: |
| 66 | ruff_format_check_out = run_ruff(ruff_bin, ['format', '--diff'], filename, config) |
| 67 | if len(ruff_format_check_out) > FORMAT_SNIPPET_LINES_LIMIT: |
| 68 | ruff_format_check_out = ruff_format_check_out[:FORMAT_SNIPPET_LINES_LIMIT] |
| 69 | ruff_format_check_out.append('[truncated]...\n') |
| 70 | # first two lines are absolute file paths, replace with relative ones |
| 71 | if ruff_format_check_out: |
| 72 | ruff_format_check_out[0] = f'--- [[imp]]{file_path}[[rst]]\n' |
| 73 | ruff_format_check_out[1] = f'+++ [[imp]]{file_path}[[rst]]\n' |
| 74 | else: |
| 75 | ruff_format_check_out = [] |
| 76 | |
| 77 | ruff_check_out = run_ruff(ruff_bin, ['check', '-q'], filename, config) |
| 78 | # Every line starts with an absolute path to a file, replace with relative one |
| 79 | for idx, line in enumerate(ruff_check_out): |
| 80 | ruff_check_out[idx] = f'[[imp]]{file_path}[[rst]]:{line.split(':', 1)[-1]}' |
| 81 | |
| 82 | msg = '' |
| 83 | if ruff_format_check_out: |
| 84 | msg += '[[bad]]Formatting errors[[rst]]:\n' |
| 85 | msg += ''.join(ruff_format_check_out) |
| 86 | |
| 87 | if ruff_format_check_out and ruff_check_out: |
| 88 | msg += '\n' |
| 89 | |
| 90 | if ruff_check_out: |
| 91 | msg += '[[bad]]Linting errors[[rst]]:\n' |
| 92 | msg += ''.join(ruff_check_out) |
| 93 | |
| 94 | return msg |
| 95 | |
| 96 | |
| 97 | def main(): |