Anthony Sottile | 8f61529 | 2022-01-15 19:24:05 -0500 | [diff] [blame] | 1 | from __future__ import annotations |
| 2 | |
Benjamin Chess | 896c0cf | 2016-01-14 15:25:46 -0800 | [diff] [blame] | 3 | import argparse |
Benjamin Chess | 896c0cf | 2016-01-14 15:25:46 -0800 | [diff] [blame] | 4 | import os.path |
Anthony Sottile | 030bfac | 2019-01-31 19:19:10 -0800 | [diff] [blame] | 5 | from typing import Sequence |
Benjamin Chess | 896c0cf | 2016-01-14 15:25:46 -0800 | [diff] [blame] | 6 | |
| 7 | |
Anthony Sottile | 8f61529 | 2022-01-15 19:24:05 -0500 | [diff] [blame] | 8 | def main(argv: Sequence[str] | None = None) -> int: |
Benjamin Chess | 896c0cf | 2016-01-14 15:25:46 -0800 | [diff] [blame] | 9 | parser = argparse.ArgumentParser(description='Checks for broken symlinks.') |
| 10 | parser.add_argument('filenames', nargs='*', help='Filenames to check') |
| 11 | args = parser.parse_args(argv) |
| 12 | |
| 13 | retv = 0 |
| 14 | |
| 15 | for filename in args.filenames: |
Anthony Sottile | 8d80d51 | 2016-01-15 07:41:58 -0800 | [diff] [blame] | 16 | if ( |
| 17 | os.path.islink(filename) and |
| 18 | not os.path.exists(filename) |
Anthony Sottile | d740fae | 2016-02-03 11:12:51 -0800 | [diff] [blame] | 19 | ): # pragma: no cover (symlink support required) |
Anthony Sottile | f5c42a0 | 2020-02-05 11:10:42 -0800 | [diff] [blame] | 20 | print(f'{filename}: Broken symlink') |
Benjamin Chess | 896c0cf | 2016-01-14 15:25:46 -0800 | [diff] [blame] | 21 | retv = 1 |
| 22 | |
| 23 | return retv |
| 24 | |
| 25 | |
| 26 | if __name__ == '__main__': |
Anthony Sottile | 39ab2ed | 2021-10-23 13:23:50 -0400 | [diff] [blame] | 27 | raise SystemExit(main()) |