-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_vcs_permalinks.py
42 lines (32 loc) · 1.02 KB
/
check_vcs_permalinks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import argparse
import re
import sys
from typing import Optional
from typing import Sequence
GITHUB_NON_PERMALINK = re.compile(
br'https://github.com/[^/ ]+/[^/ ]+/blob/master/[^# ]+#L\d+',
)
def _check_filename(filename: str) -> int:
retv = 0
with open(filename, 'rb') as f:
for i, line in enumerate(f, 1):
if GITHUB_NON_PERMALINK.search(line):
sys.stdout.write(f'{filename}:{i}:')
sys.stdout.flush()
sys.stdout.buffer.write(line)
retv = 1
return retv
def main(argv: Optional[Sequence[str]] = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*')
args = parser.parse_args(argv)
retv = 0
for filename in args.filenames:
retv |= _check_filename(filename)
if retv:
print()
print('Non-permanent github link detected.')
print('On any page on github press [y] to load a permalink.')
return retv
if __name__ == '__main__':
exit(main())