40 lines
1.3 KiB
Python
Executable File
40 lines
1.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Reject CJK characters in tracked source, documentation, and configuration."""
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
# Unicode ranges, expressed numerically so the checker follows its own rule.
|
|
RANGES = ((0x2E80, 0x2FFF), (0x3000, 0x303F), (0x3100, 0x312F),
|
|
(0x31A0, 0x31BF), (0x3400, 0x4DBF), (0x4E00, 0x9FFF),
|
|
(0xF900, 0xFAFF), (0x20000, 0x323AF))
|
|
|
|
def check(text):
|
|
return any(lo <= ord(char) <= hi for char in text for lo, hi in RANGES)
|
|
|
|
def main():
|
|
paths = subprocess.check_output(['git', 'ls-files', '-z', '--cached', '--others', '--exclude-standard'], cwd=ROOT).decode().split('\0')
|
|
failed = []
|
|
for name in paths:
|
|
if not name:
|
|
continue
|
|
path = ROOT / name
|
|
if not path.is_file():
|
|
continue
|
|
try:
|
|
content = path.read_text(encoding='utf-8')
|
|
except UnicodeDecodeError:
|
|
continue
|
|
for line, text in enumerate(content.splitlines(), 1):
|
|
if check(text):
|
|
failed.append(f'{name}:{line}: CJK character found')
|
|
if failed:
|
|
print('\n'.join(failed))
|
|
return 1
|
|
print('Text check passed: no CJK characters found')
|
|
return 0
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|