Skip to content
Snippets Groups Projects
Verified Commit a18500d4 authored by René Fritze's avatar René Fritze
Browse files

refactor broken link detection

parent 07ae812b
No related branches found
No related tags found
No related merge requests found
...@@ -6,18 +6,17 @@ with status 1. If none are found exit with status 0. ...@@ -6,18 +6,17 @@ with status 1. If none are found exit with status 0.
""" """
import sys import sys
import os import os
from pathlib import Path
def _target(path): def _resolve(path):
"""Resolve relative symlinks"""
try: try:
from pathlib import Path return path.resolve(False)
return Path(path).resolve() except TypeError:
except ImportError: # fallback for py < 3.6
pass try:
target_path = os.readlink(path) return path.resolve()
if not os.path.isabs(target_path): except FileNotFoundError:
target_path = os.path.join(os.path.dirname(path),target_path) return Path(os.readlink(str(path)))
return str(target_path)
broken = [] broken = []
for root, dirs, files in os.walk('.'): for root, dirs, files in os.walk('.'):
...@@ -25,11 +24,12 @@ for root, dirs, files in os.walk('.'): ...@@ -25,11 +24,12 @@ for root, dirs, files in os.walk('.'):
# Ignore the .git directory. # Ignore the .git directory.
continue continue
for filename in files: for filename in files:
path = os.path.join(root,filename) path = Path(os.path.join(root,filename))
if os.path.islink(path): if path.is_symlink():
target_path = _target(path) target = _resolve(path)
if not os.path.exists(target_path): # exists already checks if the pointed to file is there
broken.append('{} --> {}'.format(path, target_path)) if not path.exists():
broken.append('{} --> {}'.format(path, target))
else: else:
# If it's not a symlink we're not interested. # If it's not a symlink we're not interested.
continue continue
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment