-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinker.py2
More file actions
executable file
·62 lines (45 loc) · 1.92 KB
/
linker.py2
File metadata and controls
executable file
·62 lines (45 loc) · 1.92 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
#!/usr/bin/env python
import os
from os.path import expanduser
"""
invadeHomedir()
ensures a dotfiles backup dir exists
looks at all files in the cwd prefixed with the character in dotfileprefix
if there's a collision, move the old file into the backup directory
symlink the file into your home directory
"""
def invadeHomedir(backupdir, home, dotfileprefix, ignorefiles):
backuppath = os.path.join(home, backupdir)
# my dotfiles live in a submodule that lives in contrib/sources
sourcespath = os.path.join(os.getcwd(), 'contrib', 'sources')
if not os.path.exists(backuppath):
os.mkdir(os.path.join(home, backupdir))
for filename in os.listdir(sourcespath):
if filename[:1] == dotfileprefix or filename[:1] == ".":
# if it's already a dotfile, don't mangle the name
# otherwise, sub "." for the dotfileprefix
realname = ".%s" % filename[1:]
fullpath = os.path.join(sourcespath, filename)
targetpath = os.path.join(home, realname)
if filename in ignorefiles:
# ignore git metadata so we don't make a repo out of the homedir
continue
if os.path.islink(targetpath):
# target path is a symlink, leave it alone
continue
if os.path.exists(targetpath):
print "+ Moving [%s] to backup directory [%s]" \
% (realname, backuppath)
os.rename(targetpath, os.path.join(backuppath, realname))
filetype = "file"
if os.path.isdir(fullpath):
filetype = "directory"
print '* linking %s %s to %s' % (filetype, fullpath, targetpath)
os.symlink(fullpath, targetpath)
print "* Done!"
if __name__ == "__main__":
home = expanduser("~")
backupdir = '.dotfiles-backup'
dotfileprefix = "_"
ignored_files = [ ".git", ".gitignore", ".DS_Store" ]
invadeHomedir(backupdir, home, dotfileprefix, ignored_files)