-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink_files
executable file
·39 lines (34 loc) · 1.18 KB
/
link_files
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
#!/usr/bin/env python3
import os.path
SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
DOT_DIR = os.path.join(SCRIPT_DIR, 'dot')
HOME_DIR = os.path.expanduser('~')
def list_recursive(dirname):
for entry in os.scandir(dirname):
if entry.is_dir():
for fname in list_recursive(entry.path):
yield os.path.join(entry.name, fname)
else:
yield entry.name
def main():
if not os.path.isdir(DOT_DIR):
print(DOT_DIR, 'is not a directory')
exit(1)
for path in list_recursive(DOT_DIR):
dirname, filename = os.path.split(path)
if filename.startswith('.') or filename.endswith('~'):
print('Not moving', path)
continue
dest = os.path.join(HOME_DIR, '.' + path)
dest_dir = os.path.dirname(dest)
source = os.path.join(DOT_DIR, path)
os.makedirs(dest_dir, exist_ok=True)
print('ln -s', source, dest)
if os.path.islink(dest):
os.remove(dest)
elif os.path.exists(dest):
print(' ERROR:', dest, 'exists and is not symlink')
continue
os.symlink(source, dest)
if __name__ == '__main__':
main()