pre-commit-hooks/pre_commit_hooks/check_symlinks.py

28 lines
705 B
Python
Raw Permalink Normal View History

from __future__ import annotations
2016-01-15 07:25:46 +08:00
import argparse
import os.path
from collections.abc import Sequence
2016-01-15 07:25:46 +08:00
def main(argv: Sequence[str] | None = None) -> int:
2016-01-15 07:25:46 +08:00
parser = argparse.ArgumentParser(description='Checks for broken symlinks.')
parser.add_argument('filenames', nargs='*', help='Filenames to check')
args = parser.parse_args(argv)
retv = 0
for filename in args.filenames:
if (
os.path.islink(filename) and
not os.path.exists(filename)
2016-02-04 03:12:51 +08:00
): # pragma: no cover (symlink support required)
2020-02-06 03:10:42 +08:00
print(f'{filename}: Broken symlink')
2016-01-15 07:25:46 +08:00
retv = 1
return retv
if __name__ == '__main__':
raise SystemExit(main())