forked from grzuber/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.py
More file actions
executable file
·131 lines (106 loc) · 4.21 KB
/
install.py
File metadata and controls
executable file
·131 lines (106 loc) · 4.21 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
#!/usr/bin/env python3
"""
Filename : install.py
Author : Simon Redman
Date : 17 May 2017
Description :
"""
from enum import Enum
import ntpath # ntpath is more reliable than os.path, apparently
import os
import sys
def add_bash_linkings():
DOT_FILES['dot_bashrc'] = '.bashrc'
DOT_FILES['dot_bashrc_d'] = '.bashrc.d'
def add_zsh_linkings():
DOT_FILES['dot_zshrc'] = '.zshrc'
DOT_FILES['dot_oh-my-zsh/custom/aliases.zsh'] = '.oh-my-zsh/custom/aliases.zsh'
DOT_FILES['dot_oh-my-zsh/custom/functions.zsh'] = '.oh-my-zsh/custom/functions.zsh'
DOT_FILES['dot_oh-my-zsh/custom/completion.zsh'] = '.oh-my-zsh/custom/completion.zsh'
DOT_FILES['dot_oh-my-zsh/custom/correction.zsh'] = '.oh-my-zsh/custom/correction.zsh'
DOT_FILES['dot_oh-my-zsh/custom/misc.zsh'] = '.oh-my-zsh/custom/misc.zsh'
DOT_FILES['dot_oh-my-zsh/custom/schemes/schemes/gzuber.itermcolors'] = '.oh-my-zsh/custom/schemes/schemes/gzuber.itermcolors'
DOT_FILES['dot_oh-my-zsh/custom/themes/gzuber.zsh-theme'] = '.oh-my-zsh/custom/themes/gzuber.zsh-theme'
# Just how many configuration files does zsh need?
"""
Return a set containing all the new directories to be created
Expects a dictionary formatted as DOT_FILES.values(), which a list of every link destination
"""
def extract_new_directories ( dot_file_destinations ):
to_return = set(); # Python bows to the need to declare some types
for destination in dot_file_destinations:
path, filename = ntpath.split(destination);
to_return.add(path);
to_return.remove('')
return to_return
"""
Differentiate between different shells which, among other things, have different settings files
"""
class Shell(Enum):
BASH = 1
ZSH = 2 # It should be obvious to all that zsh is number two
"""
Exit with non-arbritrarily-random numbers
"""
class ExitCodes(Enum):
SUCCESS = 0
ENVIRON_ERROR = 1
# Mapping of source files (In this repo) to destination links
DOT_FILES = {
'dot_gitignore_global' : '.gitignore_global',
'dot_vim/vimrc' : '.vim/vimrc',
'dot_tmux.conf' : '.tmux.conf',
'dot_gitconfig_d/gitalias.txt' : '.gitconfig.d/gitalias.txt',
}
# Will be populated with directories to create as a result of the call to extract_new_directories
NEW_DIRECTORIES = []
# The folder where the links will be created
INSTALL_DIR = ""
# Set up environment
PWD = os.environ.get('PWD')
HOME = os.environ.get('HOME')
SHELL = os.environ.get('SHELL')
if (PWD is None):
sys.stderr.write("PWD environment variable is not set. Please set this to the current path")
exit(ExitCodes.ENVIRON_ERROR)
if (HOME is None):
sys.stderr.write("HOME is not set. Please set this to a directory where you would like the dotfiles installed (probably your user's home directory")
exit(ExitCodes.ENVIRON_ERROR)
if (os.environ.get('DOT_FILEPATH') is not None):
DOT_FILEPATH = os.environ.get('DOT_FILEPATH')
else:
print("Using default DOT_FILEPATH as " + PWD)
print("If this is not the correct directory, please set the DOT_FILEPATH environtment variable")
DOT_FILEPATH = PWD
INSTALL_DIR = HOME
# Python 3 handles Unicode!
CHECK_MARK="✔"
X_MARK="✘"
# Add shell-specific linkings -- If you want everything to link in set $SHELL to "all"
if (SHELL == "all"):
add_bash_linkings()
add_zsh_linkings()
elif (SHELL.endswith("bash")):
add_bash_linkings()
elif (SHELL.endswith("zsh")):
add_zsh_linkings()
else:
print(SHELL + " is not set or not supported. Skipping shell linkings")
NEW_DIRECTORIES = extract_new_directories (DOT_FILES.values())
# Create any directories which need to be created
for directory in NEW_DIRECTORIES:
path = INSTALL_DIR + "/" + directory
if not os.path.lexists(path):
# Stackoverflow points out that there is a race between checking and creating the directory
# if the directory is created after checking but before creating. Don't do that.
print("Creating " + directory)
os.makedirs(path)
# Finally create the links
for dot_file in DOT_FILES.keys():
src = DOT_FILEPATH + "/" + dot_file
dest = INSTALL_DIR + "/" + DOT_FILES[dot_file]
if os.path.lexists(dest):
print("Refusing to trample " + dest)
else:
print("Creating link " + src + " -> " + dest)
os.symlink(src, dest)