-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup
More file actions
executable file
·166 lines (141 loc) · 6.17 KB
/
Copy pathsetup
File metadata and controls
executable file
·166 lines (141 loc) · 6.17 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
#!/usr/bin/python3
'''
.setup.py
This script sets up my Ubuntu environment, from a fresh install, just the way
I like it. Installs necessary packages, and provides config files by
symlinking from the dotfiles folder.
'''
# TODO add i3gaps installation
# Imports
import os
import subprocess
import shutil
from getpass import getuser
from sys import stdout
def query_yes_no(question, default="yes"):
"""Ask a yes/no question via raw_input() and return their answer.
"question" is a string that is presented to the user.
"default" is the presumed answer if the user just hits <Enter>.
It must be "yes" (the default), "no" or None (meaning
an answer is required of the user).
The "answer" return value is one of "yes" or "no".
"""
valid = {"yes": "yes", "y": "yes", "ye": "yes",
"no": "no", "n": "no"}
if default is None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while 1:
stdout.write(question + prompt)
choice = input().lower()
if default is not None and choice == '':
return default == 'yes'
elif choice in valid.keys():
return valid[choice] == 'yes'
else:
stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")
# Variables
dotfiles_dir = '~/dotfiles' # dotfiles directory
dotfiles_dir_old = '~/dotfiles_old' # old dotfiles backup directory
# List of ubunutu packages to install
packages = ['suckless-tools', 'i3status', 'i3lock', 'htop', 'redshift-gtk',
'ipython3', 'ipython3', 'python-pip', 'python3-pip', 'feh',
'compton', 'guake', 'xclip', 'scrot', 'imagemagick', 'git',
'autoconf', 'okular', 'xbacklight', 'blueman', 'acalc',
'powertop']
extra_packages = ['texlive']
# List of python3 packages to install
# pip3_packages = ['pywal', 'matplotlib', 'numpy', 'jupyter', 'pep8']
pip3_packages = []
# list of files/folders to symlink and their symlink locations
config_files = {'bashrc': '~/.bashrc',
'i3': '~/.config/i3/config',
'redshift': '~/.config/redshift/config',
'gitconfig': '~/.gitconfig',
'ST3_User_Dir': '~/.config/sublime-text-3/Packages/User',
'compton': '~/.config/compton/config',
'profile', '~/.profile'}
# Expad user directory as Python doesn't understand ~ in paths
dotfiles_dir = os.path.expanduser(dotfiles_dir)
dotfiles_dir_old = os.path.expanduser(dotfiles_dir_old)
for file in config_files:
config_files[file] = os.path.expanduser(config_files[file])
if query_yes_no('Perform any installations?', default='no'):
# Install ubuntu packages
stdout.write('Installing Ubuntu packages...\r\n')
# Update first
packages_to_install = packages
for package in extra_packages:
if query_yes_no('Install {}?'.format(package), default="no"):
packages_to_install.append(package)
# Install Sublime Text 3
if query_yes_no("Install Sublime Text 3?", default="no"):
# Exectue things from he Sublime Text 3 linux installation
# instructions
os.system('wget -qO - '
'https://download.sublimetext.com/sublimehq-pub.gpg | '
'apt-key add -')
subprocess.call(['apt-get', 'install', 'apt-transport-https'])
os.system('echo "deb https://download.sublimetext.com/ '
'apt/stable/" | '
'sudo tee /etc/apt/sources.list.d/sublime-text.list')
packages_to_install.append('sublime-text')
# Install Spotify
if query_yes_no("Install Spotify?", default="no"):
# Exectue things from he Sublime Text 3 linux installation
# instructions
subprocess.call(['apt-key', 'adv', '--keyserver',
'hkp://keyserver.ubuntu.com:80', '--recv-keys',
'BBEBDCB318AD50EC6865090613B00F1FD2C19886',
'0DF731E45CE24F27EEEB1450EFDC8610341D9410'])
os.system('echo deb http://repository.spotify.com stable non-free '
'| sudo tee /etc/apt/sources.list.d/spotify.list')
packages_to_install.append('spotify-client')
subprocess.call(['apt-get', 'update'])
cmd = ['apt-get', 'install', '-y']
subprocess.call(cmd + packages)
subprocess.call(['apt-get', 'upgrade'])
stdout.write('...done\r\n')
# Install pip packages
# stdout.write('Installing Python3 packages...\r\n')
# cmd = ['pip3', 'install']
# subprocess.call(cmd + pip3_packages)
# stdout.write('...done\r\n')
# create dotfiles_old in homedir
stdout.write('Creating {} for backup of any existing '
'dotfiles in ~'.format(dotfiles_dir_old))
if not os.path.exists(dotfiles_dir_old):
os.makedirs(dotfiles_dir_old)
stdout.write('...done\r\n')
# create the symlinks
for config, sympath in config_files.items():
stdout.write('Creating symlink for {}'.format(config))
if os.path.exists(sympath):
if os.path.isdir(sympath):
try:
# Removing as a symlink to a directory
os.unlink(sympath)
except IsADirectoryError:
# Remove as a directory
shutil.rmtree(sympath)
else:
# Remove as a symlink to a file
os.unlink(sympath)
# A dirty hack to get the user name even if script is executed with sudo
username = os.path.split(os.path.expanduser('~'))[1]
# Create relevant directory structure if needed
if not os.path.exists(os.path.split(sympath)[0]):
os.makedirs(os.path.split(sympath)[0], mode=0x755)
shutil.chown(os.path.split(sympath)[0], user=username)
# Create the actual symlink if needed
os.symlink(os.path.join(dotfiles_dir, config), sympath)
# shutil.chown changes the target directory owner and not the symlink
# so need to acutally call the system
os.system('chown -h {0}:{0} {1}'.format(username, sympath))
stdout.write(' ...done\r\n')