-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathre_root_symlinks.py
executable file
·55 lines (48 loc) · 1.66 KB
/
re_root_symlinks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 21 19:36:30 2018
@author: alex
"""
import contextlib
import os
from pathlib import Path
def silent_remove(pathname):
print ("Removing "+pathname)
with contextlib.suppress(FileNotFoundError):
os.remove(pathname)
def re_root_symlink(link_from,link_to,new_root):
silent_remove(link_from)
new_link_to = os.path.join(new_root, link_to[1:])
print ("ln -s {} {}".format(new_link_to, link_from))
os.symlink(new_link_to, link_from)
def symlinks(top_dir):
"""
:returns: dict mapping from pathnames to destination pathnames
"""
link_dict = dict()
for dirName, subdirList, fileList in os.walk(top_dir):
for file in subdirList + fileList:
pathname = os.path.join(dirName,file)
print("Trying " + pathname)
path = Path(pathname)
print(path)
if path.is_symlink():
print("Resolving " + path.as_posix())
try:
destination_path = path.resolve()
if destination_path.root == "/":
link_dict[pathname] = destination_path.as_posix()
except Exception as e:
print("Corrupt symlink " + path.as_posix())
print(e)
return link_dict
def re_root_symlinks(top_dir, new_root):
"""
Change all symlinks that point to root ("/"), so they point to new_root
"""
for link_from,link_to in symlinks(top_dir).items():
re_root_symlink(link_from,link_to,new_root)
# Should be called after "cd new_root"
if __name__ == '__main__':
re_root_symlinks(".", os.getcwd())