-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_symlinks
executable file
·132 lines (106 loc) · 3.72 KB
/
setup_symlinks
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python
"""
=================
Config Symlinkier
=================
:Author: QueezyTheGreat
:Version: 0.1.2
Description
-----------
Creates symlinks to the files and directories that reside in the
directory where this script is, excluding this script and any hidden
files/directories.
The destination location of of the symlinks is the home directory of
the user running the script. The file name is prefixed with a
dot('.'). For example::
file_name -> ~/.file_name
All files/directories that are to be symlinked need to have the dot
prefix removed from their name.
Usage
-----
setup_symlinks [-v | -h]
-v verbose output
-h displays this message
"""
from os.path import dirname, basename, join, exists, islink, lexists, expanduser, abspath
import os, sys
def cleanup_symlinks(logfile):
"""
Removes all symlinks located in the log file .
Location of the log file is ~/.setup_symlinks.log
Only symlinks are removed!
"""
if exists(logfile):
removed = []
try:
log = open(logfile,'r')
symlinks = [ symlink.rstrip() for symlink in log ]
for symlink in symlinks:
if lexists(symlink) and islink(symlink):
try:
os.remove(symlink)
removed.append(symlink)
if verbose :
print('Removing symlink %s' % symlink)
except OSError as err:
print('An error occured removing %s symlink.' % symlink)
elif verbose:
print('Not removing %s' % symlink)
log.close()
log = open(logfile,'w')
for symlink in symlinks:
if symlink not in removed and lexists(symlink):
log.write(symlink+'\n')
except OSError as err:
print('An error occured while trying to read %s log file' % logfile)
print(err)
else:
log.close()
def log_symlinks(logfile, symlinks):
"""
Logs the supplied symlinks to ~/.setup_symlinks.log log file.
symlinks - list of symlinks to log
"""
try:
log = open(logfile,'a')
for symlink in symlinks:
log.write(symlink+'\n')
except:
print('An error occured while writing log file(%s)' % logfile)
else:
log.close()
def create_symlinks(src_dir, configs, dest_dir, prefix):
"""
Creates symlinks.
"""
symlinks = []
for config in configs:
if config != basename(__file__) and not config.startswith('.'):
try:
sym_src = join(src_dir, config)
sym_dst = expanduser(join(dest_dir, prefix+config))
os.symlink(sym_src, sym_dst)
symlinks.append(sym_dst)
if verbose:
print('Created symlink %s to %s' % (sym_dst, sym_src))
except OSError as err:
if err.errno == 17:
print('\nFailed to create symlink %s to %s\n' % (sym_dst, sym_src))
except:
print('\nAn unknown error occured during the creation of symlinks. Exiting...')
return symlinks
if __name__ == '__main__':
verbose = False
if len(sys.argv) == 2 and sys.argv[-1] in ['-v', '-h']:
if sys.argv[-1] == '-v':
verbose = True
elif sys.argv[-1] == '-h':
print(__doc__)
sys.exit(0)
fdir = abspath(dirname(__file__))
fname = basename(__file__)
configs = os.listdir(fdir)
logfile = expanduser(join('~', '.%s.log' % fname))
cleanup_symlinks(logfile)
symlinks = create_symlinks(fdir, configs, '~', '.')
log_symlinks(logfile, symlinks)