Created
November 8, 2009 19:48
-
-
Save seanh/229454 to your computer and use it in GitHub Desktop.
A Python script to check for broken symlinks in the current working directory. I use this as part of a pre-commit hook with git.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" Recursively walk the current working directory looking for broken | |
symlinks. If any broken symlinks are found print out a report and exit | |
with status 1. If none are found print out OK and exit with status 0. | |
""" | |
import sys,os | |
print "Checking for broken symlinks...", | |
links = [] | |
broken = [] | |
for root, dirs, files in os.walk('.'): | |
if root.startswith('./.git'): | |
# Ignore the .git directory. | |
continue | |
for filename in files: | |
path = os.path.join(root,filename) | |
if os.path.islink(path): | |
target_path = os.readlink(path) | |
# Resolve relative symlinks | |
if not os.path.isabs(target_path): | |
target_path = os.path.join(os.path.dirname(path),target_path) | |
if not os.path.exists(target_path): | |
links.append(path) | |
broken.append(path) | |
else: | |
links.append(path) | |
else: | |
# If it's not a symlink we're not interested. | |
continue | |
print len(links), 'symlinks found...', | |
if broken == []: | |
print 'OK' | |
sys.exit(0) | |
else: | |
print "broken symlink(s) found:" | |
for link in broken: | |
print link | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment