-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.py
More file actions
executable file
·106 lines (85 loc) · 3.25 KB
/
install.py
File metadata and controls
executable file
·106 lines (85 loc) · 3.25 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
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
#!/usr/bin/env python3
from __future__ import print_function
import glob
import os
import shutil
import sys
DEBUG = True
IGNORED_FILES = set(['.git'])
OVERWRITE_TO_ALL = '' # can be: 'y', 'n', '' (ask)
def main(argv):
dot_dir = os.path.dirname(os.path.abspath(__file__))
home_dir = os.path.dirname(dot_dir)
config_dir = os.path.join(home_dir, '.config')
print('Home directory: %s' % home_dir)
print('Dot directory: %s' % dot_dir)
print()
for rc_file in glob.glob(os.path.join(dot_dir, '.*')):
rc_filename = os.path.basename(rc_file) # eg: .pyrc
if rc_filename in IGNORED_FILES:
continue
print('Check for "%s" ...' % rc_filename, end=' ')
# default action: create a symlink
symlink = os.path.join(home_dir, rc_filename) # eg: /home/user/.pyrc
symlink_target = os.path.join(os.path.basename(os.path.dirname(rc_file)), rc_filename) # eg: .dotfiles/.pyrc
if os.path.lexists(symlink):
# already exists
if os.path.islink(symlink):
if os.readlink(symlink) != symlink_target:
if get_overwrite_answer('Symlink "%s" -> "%s" already exists. Overwrite? (y)es or [Enter] / (Y)es to all / (n)o / (N)o to all / (a)bort: ' % (symlink, os.readlink(symlink))):
create_symlink(symlink_target, symlink, overwrite=True)
else:
print('OK.')
elif os.path.isfile(symlink):
if get_overwrite_answer('File "%s" already exists. Overwrite? (y)es or [Enter] / (Y)es to all / (n)o / (N)o to all / (a)bort: ' % symlink):
create_symlink(symlink_target, symlink, overwrite=True)
elif os.path.isdir(symlink):
print('Directory "%s" already exists. Do something about it. Abort.' % symlink)
sys.exit(1)
else:
create_symlink(symlink_target, symlink)
# htoprc
htoprc_source = os.path.join(dot_dir, 'htoprc')
htop_dir = os.path.join(config_dir, 'htop')
htoprc_target = os.path.join(htop_dir, 'htoprc')
print('Copy the "%s" -> "%s".' % (htoprc_source, htoprc_target))
if not os.path.exists(htop_dir):
os.makedirs(htop_dir)
shutil.copy2(htoprc_source, htoprc_target)
print('Done.')
def get_overwrite_answer(message):
global OVERWRITE_TO_ALL
if OVERWRITE_TO_ALL == 'y':
return True
elif OVERWRITE_TO_ALL == 'n':
return False
else:
# ask
answer = get_input(message).strip()
if answer in ['y', '', 'Y']:
# yes
if answer == 'Y':
OVERWRITE_TO_ALL = 'y'
return True
elif answer == 'a':
# abort
print('Abort.')
sys.exit(0)
else:
# no
if answer == 'N':
OVERWRITE_TO_ALL = 'n'
return False
def create_symlink(source, link_name, overwrite=False):
print('Create symlink "%s" -> "%s"' % (link_name, source))
if overwrite and os.path.lexists(link_name):
os.remove(link_name)
os.symlink(source, link_name)
if sys.version_info < (3,):
# python 2
get_input = raw_input
else:
# python 3
get_input = input
if __name__ == '__main__':
main(sys.argv)