-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkdotfiles
More file actions
executable file
·63 lines (51 loc) · 2.03 KB
/
linkdotfiles
File metadata and controls
executable file
·63 lines (51 loc) · 2.03 KB
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
#!/usr/bin/env python
"""
Link all the files from the current dir to the homedir as dotfiles.
Works only on Unix-like operating systems that support symlinks (obviously)
"""
import os
from fnmatch import fnmatch
from shutil import rmtree
from optparse import OptionParser
# Parse commmandline options
parser = OptionParser()
parser.add_option("-f", "--force", dest="force", default=False, action="store_true",
help="forcibly overwrite files in the homedir when creating links")
parser.add_option("-d" ,"--dry-run", dest="dry", default=False, action="store_true", help="Do nothing, just print what would be done")
(options, args) = parser.parse_args()
# Skip these files (uses fnmatch matching)
skip_list = ['linkdotfiles', 'README.markdown', ".git", "3rd-party"]
cwd = os.path.dirname(os.path.realpath(__file__))
cwd = os.path.realpath(cwd)
homedir = os.path.expanduser('~')
files = os.listdir(cwd)
def create_symlink(src, dst):
if os.environ['TERM'] == 'cygwin':
os.system('ln -s "%s" "%s"' % (src, dst))
else:
os.symlink(source, destination)
for filename in files:
if True in [fnmatch(filename, pattern) for pattern in skip_list]:
print ('Skipping %s' % filename)
continue
source = os.path.join(cwd, filename)
destination = os.path.join(homedir, filename)
if os.path.lexists(destination):
if options.force:
print ('Deleting %s' % destination)
if not options.dry:
try:
os.remove(destination)
except OSError:
try:
rmtree(destination)
except OSError as e:
print ('Failed to delete %s' % destination)
continue
else:
print ('Not overwriting %s since the file exists already and force (-f) is not in effect' % destination)
continue
print ('Creating a link to %s at %s.' % (source, destination))
if not options.dry:
create_symlink(source, destination)
print ('Done.')