-
Notifications
You must be signed in to change notification settings - Fork 0
/
install
executable file
·61 lines (51 loc) · 2.04 KB
/
install
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
#!/usr/bin/env python
import os
import time
import yaml
import shutil
def install_link(target, link, **kwargs):
"""install symlinks, backing up existing files"""
backupdir = os.path.expanduser('~/.config/dotfiles/backups')
backupext = '.bak'
if kwargs['backupext']:
backupext = kwargs['backupext']
if not os.path.islink(link) or not os.path.samefile(link, target):
if not os.path.exists(backupdir):
os.mkdir(backupdir)
linkfile = os.path.basename(link)
print "backing up existing file {}".format(linkfile)
try:
# TODO: if a symlink, just copy the symlink
backupfile = "{}/{}{}".format(backupdir, linkfile, backupext)
if os.stat(link):
shutil.copy2(link, backupfile)
os.unlink(link)
except IOError as e:
print("(skip) unable to backup {}".format(link))
return
except OSError as e:
# no file means we don't need to back it up
pass
print("linking {} -> {}".format(target, link))
os.symlink(target, link)
else:
print("(skip) link {} already installed...".format(link))
if __name__ == '__main__':
configdir = os.path.dirname(os.path.realpath(__file__))
backupext = ".bak-{}".format(time.strftime("%Y%m%d%H%M%S"))
with open("install.yml", 'r') as stream:
try:
installdata = yaml.load(stream)
except yaml.YAMLError as exc:
print(exc)
exit(1)
for installdir in installdata.keys():
realdir = os.path.expanduser(installdir)
for link in installdata[installdir]:
if type(link) == type(str()):
linkpath = "{}/{}".format(realdir, link)
targetpath = "{}/{}".format(configdir, link)
elif type(link) == type(dict()):
linkpath = "{}/{}".format(realdir, link['link'])
targetpath = "{}/{}".format(configdir, link['target'])
install_link(targetpath, linkpath, backupext=backupext)