| 1 |
#!/usr/bin/env python |
| 2 |
# BSD Licence |
| 3 |
# Copyright (c) 2011, Science & Technology Facilities Council (STFC) |
| 4 |
# All rights reserved. |
| 5 |
# |
| 6 |
# See the LICENSE file in the source distribution of this software for |
| 7 |
# the full license text. |
| 8 |
|
| 9 |
import os |
| 10 |
import re |
| 11 |
|
| 12 |
""" |
| 13 |
Finds symlinks recursively under a given top directory, and replaces any symlinks |
| 14 |
found pointing to absolute paths with ones pointing to relative links. |
| 15 |
|
| 16 |
Optionally a regular expression can be supplied to |
| 17 |
""" |
| 18 |
|
| 19 |
class LinkFixer: |
| 20 |
|
| 21 |
def __init__(self, justTesting = False): |
| 22 |
self.justTesting = justTesting |
| 23 |
self._repeatSlashesSub = re.compile("//+").sub |
| 24 |
|
| 25 |
|
| 26 |
def noRepeatSlashes(self, path): |
| 27 |
""" |
| 28 |
get path with any repeated slashes replaced by single / |
| 29 |
""" |
| 30 |
return self._repeatSlashesSub("/", path) |
| 31 |
|
| 32 |
|
| 33 |
def getRelTarget(self, link, target): |
| 34 |
""" |
| 35 |
get relative link target given existing link path and absolute target |
| 36 |
""" |
| 37 |
link = self.noRepeatSlashes(link) |
| 38 |
target = self.noRepeatSlashes(target) |
| 39 |
|
| 40 |
commonPrefix = os.path.commonprefix([link, target]) |
| 41 |
lenCommonPrefix = len(commonPrefix) |
| 42 |
linkWithoutCommonPrefix = link[lenCommonPrefix :] |
| 43 |
targetWithoutCommonPrefix = target[lenCommonPrefix :] |
| 44 |
|
| 45 |
# answer is then the remaining part of the target, |
| 46 |
# prepended by a "../" for every slash in the remaining part of the link |
| 47 |
answer = "" |
| 48 |
for char in linkWithoutCommonPrefix: |
| 49 |
if char == "/": |
| 50 |
answer += "../" |
| 51 |
answer += targetWithoutCommonPrefix |
| 52 |
|
| 53 |
return answer |
| 54 |
|
| 55 |
|
| 56 |
def fixLink(self, link, target): |
| 57 |
""" |
| 58 |
Replace an absolute link with a relative link. |
| 59 |
""" |
| 60 |
newTarget = self.getRelTarget(link, target) |
| 61 |
|
| 62 |
print "link:", link |
| 63 |
print " current target:", target |
| 64 |
print " new target:", newTarget |
| 65 |
|
| 66 |
if not self.justTesting: |
| 67 |
assert(os.path.islink(link)) # sanity check |
| 68 |
os.remove(link) |
| 69 |
os.symlink(newTarget, link) |
| 70 |
print "done" |
| 71 |
print |
| 72 |
|
| 73 |
|
| 74 |
def findAndFixLinks(self, |
| 75 |
startingDirectory, |
| 76 |
callback = None, |
| 77 |
regexp = None): |
| 78 |
""" |
| 79 |
Call fixLink or other callback function on every absolute symlink found |
| 80 |
under starting directory, matching regexp if supplied. |
| 81 |
Callback should take args: link path, link target |
| 82 |
""" |
| 83 |
if not startingDirectory.startswith("/"): |
| 84 |
raise ValueError("starting directory should be an absolute path") |
| 85 |
|
| 86 |
if callback == None: |
| 87 |
callback = self.fixLink |
| 88 |
if regexp != None: |
| 89 |
tester = re.compile(regexp).search |
| 90 |
else: |
| 91 |
tester = None |
| 92 |
self._findAndFixLinks(startingDirectory, callback, tester) |
| 93 |
|
| 94 |
|
| 95 |
def _findAndFixLinks(self, |
| 96 |
startingDirectory, |
| 97 |
callback, |
| 98 |
tester = None): |
| 99 |
|
| 100 |
for fileName in os.listdir(startingDirectory): |
| 101 |
path = os.path.join(startingDirectory, fileName) |
| 102 |
if os.path.islink(path): |
| 103 |
if (tester == None) or tester(fileName): |
| 104 |
target = os.readlink(path) |
| 105 |
if target.startswith("/"): |
| 106 |
callback(path, target) |
| 107 |
else: |
| 108 |
if os.path.isdir(path): |
| 109 |
self._findAndFixLinks(path, callback, tester) |
| 110 |
|
| 111 |
|
| 112 |
if __name__ == '__main__': |
| 113 |
|
| 114 |
fixer = LinkFixer(justTesting = False) |
| 115 |
|
| 116 |
fixer.findAndFixLinks('/badc/cmip5/data/cmip5/output1/MOHC', |
| 117 |
regexp = "\.nc$") |