-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
executable file
·72 lines (62 loc) · 2.36 KB
/
install.py
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
#!/usr/bin/env python
"""
install.py, a script to backup current config files
and replace with symlinks to files in DOTFILES folder.
Credits:
99.5% of this code is from "make_links.py", Copyright 2010 Stephen A. Goss
original source: https://raw.github.com/deliciousrobots/dotfiles/master/make_links.py
"""
import os
import shutil
import stat
DOTFILES_DIR = os.path.dirname(os.path.realpath(__file__))
HOME_DIR = os.path.expanduser("~")
def should_ask(filename):
"""
conditions for which the user should be prompted for confirmation
"""
return filename.startswith('.bash') or filename.endswith('profile')
print("DOTFILES in this folder : %s" % DOTFILES_DIR)
DOTFILES = set(os.listdir(DOTFILES_DIR))
DOTFILES = DOTFILES - set(['.git', 'install.py', 'README.md'])
DOTFILES = list(DOTFILES)
print("\n".join(DOTFILES))
print("target home folder : %s" % HOME_DIR)
BACKUP_FOLDER_PATH = os.path.join(HOME_DIR, "backups")
if not os.path.isdir(BACKUP_FOLDER_PATH):
print("creating backup folder : %s" % BACKUP_FOLDER_PATH)
os.mkdir(BACKUP_FOLDER_PATH)
for filename in DOTFILES:
if should_ask(filename):
answer = raw_input('Symlink %s ? ' % filename)
if not answer in ['y', 'Y']:
continue
print("processing %s ..." % filename)
target = os.path.join(HOME_DIR, filename)
dotfile = os.path.join(DOTFILES_DIR, filename)
try:
statinfo = os.lstat(target)
except:
print("file does not exist : %s" % target)
statinfo = None
if statinfo:
if stat.S_ISLNK(statinfo.st_mode):
print("skipping symlink : %s" % target)
continue
else:
backup = os.path.join(BACKUP_FOLDER_PATH, filename)
if stat.S_ISDIR(statinfo.st_mode):
if not os.path.isdir(backup):
print("copying (folder) %s to %s " % (target, backup))
shutil.copytree(target, backup)
print("deleting folder: %s" % target)
shutil.rmtree(target)
else:
if not os.path.isfile(backup):
print("copying %s to %s " % (target, backup))
shutil.copyfile(target, backup)
print("delete file : %s" % target)
os.remove(target)
print("creating symlink : %s to %s" % (dotfile, target))
os.symlink(dotfile, target)
print("done!")