-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
executable file
·182 lines (144 loc) · 6.43 KB
/
setup.py
File metadata and controls
executable file
·182 lines (144 loc) · 6.43 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python
import os
import glob
import shutil
import errno
import subprocess
import sys
# Check python version, requires python 3
if sys.version_info < (3, 0):
sys.stdout.write("Sorry, requires Python 3.x, not Python 2.x\n")
sys.exit(1)
import pathlib
HOME= os.path.expanduser('~')
HOST_OS = None
SOURCE_DOTS = HOME + '/dotfiles/dots'
SOURCE_SUBS = HOME + '/dotfiles/subs'
SOURCES = [SOURCE_DOTS, SOURCE_SUBS]
DOT_ARCHIVE = HOME + '/.dotArchive/'
EXCLUDE = [".git", ".gitignore", ".gitmodules"]
NO_DOT_PREFIX = []
PRESERVE_EXTENSION = []
def force_remove(path):
if os.path.isdir(path) and not os.path.islink(path):
shutil.rmtree(path, False)
else:
os.unlink(path)
def is_link_to(link, dest):
is_link = os.path.islink(link)
is_link = is_link and os.readlink(link).rstrip('/') == dest.rstrip('/')
return is_link
def archive(dest):
try:
os.mkdir(DOT_ARCHIVE)
except OSError as exc:
if exc.errno != errno.EEXIST:
raise
pass
print ("Archiving %s to %s" % (dest, DOT_ARCHIVE + os.path.basename(dest)))
shutil.move(dest, DOT_ARCHIVE + os.path.basename(dest))
def link():
for source_dir in SOURCES:
if source_dir == SOURCE_DOTS:
print ("Linking dotfiles to ~/ ...")
else:
print ("Linking submodules to ~/ ...")
os.chdir(os.path.expanduser(source_dir))
for filename in [file for file in glob.glob('*') if file not in EXCLUDE]:
dotfile = filename
if filename not in NO_DOT_PREFIX:
dotfile = '.' + dotfile
if filename not in PRESERVE_EXTENSION:
dotfile = os.path.splitext(dotfile)[0]
dotfile = os.path.join(os.path.expanduser('~'), dotfile)
source = os.path.join(source_dir, filename).replace('~', '.')
# Check that we aren't overwriting anything
if os.path.lexists(dotfile):
if os.path.islink(dotfile):
if is_link_to(dotfile, source):
continue
response = input("Overwrite symbolic link for '%s'? [y/N] " % dotfile)
if not response.lower().startswith('y'):
print ("Skipping `%s'..." % dotfile)
continue
force_remove(dotfile)
else:
response = input("Archive '%s' to '%s'? [y/N] " %(dotfile, DOT_ARCHIVE))
if not response.lower().startswith('y'):
print ("Archiving `%s'..." % dotfile)
continue
archive(dotfile)
os.symlink(source, dotfile)
print ("%s => %s" % (dotfile, source))
def unlink():
if os.path.exists(os.path.expanduser(DOT_ARCHIVE)):
# TODO: This does not restore dotfiles that were initially symbolic linked to $HOME
for source_dir in SOURCES:
if source_dir == SOURCE_DOTS:
print ("Removing symlinks to dotfiles in ~/ ...")
else:
print ("Removing symlinks to submodules in ~/ ...")
os.chdir(os.path.expanduser(source_dir))
for filename in [file for file in glob.glob('*') if file not in EXCLUDE]:
dotfile = filename
if filename not in NO_DOT_PREFIX:
dotfile = '.' + dotfile
if filename not in PRESERVE_EXTENSION:
dotfile = os.path.splitext(dotfile)[0]
dotfile = os.path.join(os.path.expanduser('~'), dotfile)
source = os.path.join(source_dir, filename).replace('~', '.')
if os.path.lexists(dotfile):
if os.path.islink(dotfile):
if is_link_to(dotfile, source):
force_remove(dotfile)
print("Moving archived files back")
os.chdir(os.path.expanduser(DOT_ARCHIVE))
file_refs = pathlib.Path(".").glob("*")
for file_obj in file_refs:
archive_path = os.path.join(DOT_ARCHIVE, os.path.basename(str(file_obj)))
moved_path = os.path.join(HOME, os.path.basename(str(file_obj)))
print ("Moving '%s' back to '%s'" %(archive_path, moved_path))
shutil.move(str(file_obj), moved_path)
print("Removing dotArchive directory")
os.rmdir(os.path.expanduser(DOT_ARCHIVE))
else:
print(os.path.expanduser(DOT_ARCHIVE) + " Doesnt exist! Maybe you didnt have any dotfiles before setting up?")
def setup():
link();
# Test for vim clipboard
if (subprocess.run("vim --version | grep '+clipboards;'", shell=True)).returncode == 1:
print("To enable clipboard copy and paste within vim, you must have the +clipboards flag enabled within vim")
#subprocess.run("chsh -s $(which zsh)", shell=True, check=True)
print("To change your default shell, run: sudo chsh $(which zsh). Remeber you need to restart your shell for this to take effect!")
subprocess.run("vim +PluginInstall +qall", shell=True, check=True)
def main():
print("======================== Welcome to the dotfile configure-er ========================")
print("Commands: ")
print("setup - make sym links within ~/ for all dotfiles and submodules, pre appending a dot. Run setup scripts.")
print("unlink - remove all symlinks from ~/ to any resources contained in this directory.")
print("exit - exit the dotfile helper")
print("=====================================================================================")
print("OS types: macOS, linux, windows")
response = input("Please enter which OS you are using: \n")
print("OS selected: " + response.lower())
if response.lower() == "windows":
print("Sorry windows is not supported yet. Quitting...")
sys.exit(0)
elif response.lower() != "macos" and response.lower() != "linux":
print("Unsupported OS. Qutting...")
sys.exit(1)
else:
HOST_OS=response.lower()
response = input("What would you like to do? \n")
while 1:
if response.lower() == "exit":
sys.exit(1)
elif response.lower() == "setup":
setup()
elif response.lower() == "unlink":
unlink()
else:
print("Valid options are unlink, setup and exit")
response = input("Would you like to do anything else? If not, enter exit \n")
if __name__ == '__main__':
main()