Anthony Sottile | 8f61529 | 2022-01-15 19:24:05 -0500 | [diff] [blame] | 1 | from __future__ import annotations |
| 2 | |
Anthony Sottile | 9db0a74 | 2017-10-06 23:32:11 -0700 | [diff] [blame] | 3 | import argparse |
| 4 | import re |
| 5 | import sys |
Youngmin Koo | 1f8151a | 2020-11-17 02:33:47 +0900 | [diff] [blame] | 6 | from typing import Pattern |
Anthony Sottile | 030bfac | 2019-01-31 19:19:10 -0800 | [diff] [blame] | 7 | from typing import Sequence |
Anthony Sottile | 9db0a74 | 2017-10-06 23:32:11 -0700 | [diff] [blame] | 8 | |
| 9 | |
Youngmin Koo | 1f8151a | 2020-11-17 02:33:47 +0900 | [diff] [blame] | 10 | def _get_pattern(domain: str) -> Pattern[bytes]: |
jack1142 | 4729918 | 2021-04-06 20:07:08 +0200 | [diff] [blame] | 11 | regex = ( |
| 12 | rf'https://{domain}/[^/ ]+/[^/ ]+/blob/' |
| 13 | r'(?![a-fA-F0-9]{4,64}/)([^/. ]+)/[^# ]+#L\d+' |
| 14 | ) |
Youngmin Koo | 1f8151a | 2020-11-17 02:33:47 +0900 | [diff] [blame] | 15 | return re.compile(regex.encode()) |
Anthony Sottile | 9db0a74 | 2017-10-06 23:32:11 -0700 | [diff] [blame] | 16 | |
| 17 | |
Anthony Sottile | 8f61529 | 2022-01-15 19:24:05 -0500 | [diff] [blame] | 18 | def _check_filename(filename: str, patterns: list[Pattern[bytes]]) -> int: |
Anthony Sottile | 9db0a74 | 2017-10-06 23:32:11 -0700 | [diff] [blame] | 19 | retv = 0 |
| 20 | with open(filename, 'rb') as f: |
| 21 | for i, line in enumerate(f, 1): |
Youngmin Koo | 1f8151a | 2020-11-17 02:33:47 +0900 | [diff] [blame] | 22 | for pattern in patterns: |
| 23 | if pattern.search(line): |
| 24 | sys.stdout.write(f'{filename}:{i}:') |
| 25 | sys.stdout.flush() |
| 26 | sys.stdout.buffer.write(line) |
| 27 | retv = 1 |
Anthony Sottile | 9db0a74 | 2017-10-06 23:32:11 -0700 | [diff] [blame] | 28 | return retv |
| 29 | |
| 30 | |
Anthony Sottile | 8f61529 | 2022-01-15 19:24:05 -0500 | [diff] [blame] | 31 | def main(argv: Sequence[str] | None = None) -> int: |
Anthony Sottile | 9db0a74 | 2017-10-06 23:32:11 -0700 | [diff] [blame] | 32 | parser = argparse.ArgumentParser() |
| 33 | parser.add_argument('filenames', nargs='*') |
Youngmin Koo | 1f8151a | 2020-11-17 02:33:47 +0900 | [diff] [blame] | 34 | parser.add_argument( |
| 35 | '--additional-github-domain', |
| 36 | dest='additional_github_domains', |
| 37 | action='append', |
| 38 | default=['github.com'], |
| 39 | ) |
Anthony Sottile | 9db0a74 | 2017-10-06 23:32:11 -0700 | [diff] [blame] | 40 | args = parser.parse_args(argv) |
| 41 | |
Youngmin Koo | 1f8151a | 2020-11-17 02:33:47 +0900 | [diff] [blame] | 42 | patterns = [ |
| 43 | _get_pattern(domain) |
| 44 | for domain in args.additional_github_domains |
| 45 | ] |
| 46 | |
Anthony Sottile | 9db0a74 | 2017-10-06 23:32:11 -0700 | [diff] [blame] | 47 | retv = 0 |
Youngmin Koo | 1f8151a | 2020-11-17 02:33:47 +0900 | [diff] [blame] | 48 | |
Anthony Sottile | 9db0a74 | 2017-10-06 23:32:11 -0700 | [diff] [blame] | 49 | for filename in args.filenames: |
Youngmin Koo | 1f8151a | 2020-11-17 02:33:47 +0900 | [diff] [blame] | 50 | retv |= _check_filename(filename, patterns) |
Anthony Sottile | 9db0a74 | 2017-10-06 23:32:11 -0700 | [diff] [blame] | 51 | |
| 52 | if retv: |
| 53 | print() |
| 54 | print('Non-permanent github link detected.') |
| 55 | print('On any page on github press [y] to load a permalink.') |
| 56 | return retv |
| 57 | |
| 58 | |
| 59 | if __name__ == '__main__': |
Anthony Sottile | 39ab2ed | 2021-10-23 13:23:50 -0400 | [diff] [blame] | 60 | raise SystemExit(main()) |