Last active
August 29, 2015 14:07
-
-
Save yarko/d403551fb1a6e1388ed9 to your computer and use it in GitHub Desktop.
Find & fix bad Symlinks in Dropbox folder
This file contains hidden or 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 | |
import sys | |
import optparse | |
from os import path | |
import subprocess | |
OLD_PYTHON_VERS = "2.7.5" | |
NEW_PYTHON_VERS = "2.7.8_1" | |
# first char index of filename, from my ls: | |
FILE_POS = 49 | |
LINK_STRING = ' -> ' | |
usage = """usage: %prog [options] | |
expects input on stdin from "ls -lah" (or "ls -la", or "ls -l") | |
example: | |
$ find . -type l -exec ls -lah {}\; | %prog --dry-run | |
after reviewing all the settings, and convincing yourself that you | |
are helped by the re-linking commands displayed, then: | |
$ find . -type l -exec ls -lah {}\; | %prog | |
is appropriate. | |
""" | |
parser = optparse.OptionParser(usage=usage) | |
parser.add_option('-n', '--dry-run', | |
action="store_true", default=False, | |
help="dry run, prints what commands would be run.") | |
parser.add_option('--from', dest="from_string", | |
action="store", type="string", default=OLD_PYTHON_VERS, | |
help="string to convert from, in broken link target.\ndefault: {s}".format(s=OLD_PYTHON_VERS)) | |
parser.add_option('--to', dest="to_string", | |
action="store", type="string", default=NEW_PYTHON_VERS, | |
help="string to convert to, the corrected link target.\ndefault: {s}".format(s=NEW_PYTHON_VERS)) | |
parser.add_option('--file-pos', | |
action="store", type="int", default=FILE_POS, | |
help="character position of filename (e.g. from \"ls\")\ndefault: {i}".format(i=FILE_POS)) | |
parser.add_option('--link-string', | |
action="store", type="string", default=LINK_STRING, | |
help="""string separating link from target. | |
default: \"{s}\"""".format(s=LINK_STRING)) | |
opt, args = parser.parse_args() | |
# for zero-based list indexes: | |
opt.file_pos -= 1 | |
# call or dry-run: | |
if opt.dry_run is True: | |
do_call = lambda cmd,trg,lnk:\ | |
sys.stdout.write(cmd+" {t} {l}".format(t=trg, l=link) + '\n') | |
else: | |
do_call = lambda cmd,trg,lnk:\ | |
subprocess.check_output(cmd.split(), trg, lnk, | |
stderr=subprocess.STDOUT) | |
for line in sys.stdin: | |
# doesn't look like a link: | |
if opt.link_string not in line: | |
continue | |
# last char is newline, which we don't want | |
link, target = line[opt.file_pos:-1].split(opt.link_string) | |
if target[0] != "/": | |
realtarget = path.join(path.dirname(link), target) | |
if not path.exists(realtarget): | |
print "Failed link to a relative target: {t}".format(t=target) | |
print " -> link:", link | |
elif not path.exists(target): | |
print "Missing target: {t}".format(t=target) | |
if opt.from_string in target: | |
ntarget = target.replace(opt.from_string, opt.to_string) | |
if path.exists(ntarget): | |
do_call("ln -fsv", ntarget, link) | |
else: | |
print("Failed link: {l}".format(l=link)) | |
Oh, and no guarantees if you're using python2.6 or older (although I tried to make it 2.6 compatible).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My quick, hacky script to find bad Python virtualenv symlinks, due to an upgrade of python.
(a little less hacky, now that I've cleaned it up a bit).
I run it this way on my mac:
Run it with the
--dry-run
option once, to first validate that yours is a virtualenv type of problem, related to python paths. Also, trydroplinks.py -h
.