# Copyright 2014-2015 Ivan Kravets # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from os import chdir, getcwd, readlink, remove, symlink, walk from os.path import exists, islink, join, split from sys import exit as sys_exit def get_symrelpath(root, sympath, ending=None): head, tail = split(sympath) if ending: ending = join(tail, ending) relpath = join("..", ending) else: relpath = tail ending = tail if exists(join(root, relpath)): return relpath elif head: return get_symrelpath(root, head, ending) else: raise Exception() def fix_symlink(root, fname, brokenlink): prevcwd = getcwd() symrelpath = get_symrelpath(root, brokenlink) chdir(root) remove(fname) symlink(symrelpath, fname) chdir(prevcwd) def main(): for root, dirnames, filenames in walk("."): for f in filenames: path = join(root, f) if not islink(path) or exists(path): continue fix_symlink(root, f, readlink(path)) if __name__ == "__main__": sys_exit(main())