Skip to content

Instantly share code, notes, and snippets.

@myyc
Last active August 27, 2016 10:56
Show Gist options
  • Save myyc/393a9b2654a7bc4389418be9d0ca8dcf to your computer and use it in GitHub Desktop.
Save myyc/393a9b2654a7bc4389418be9d0ca8dcf to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from __future__ import print_function
import sys
import platform
import os.path
import os
import subprocess
from distutils import spawn
import getpass
if sys.version_info < (3, 0):
input = raw_input
def fr(s):
return """\n{}""".format(s)
def red(s):
return "\033[41m\033[37m\033[1m{s}\033[0m".format(s=s)
def yellow(s):
return "\033[43m\033[30m\033[1m{s}\033[0m".format(s=s)
def warn(s):
return "{}: {}".format(red("Warning"), s)
def q(question, default="n"):
default = default.lower()
r = input(fr("[{}] {} ({}) > ".format(yellow("??"), question, "y/N" if default == "n" else "Y/n")))
if type(r) == "str":
r = r.lower()
if r == "y" or default == "y" and r != "n":
return True
else:
return False
p = platform.system()
if p == "Linux":
pass
elif p == "Darwin":
p = "OSX"
else:
raise Exception("Unsupported system: {}".format(p))
osx = p == "OSX"
linux = p == "Linux"
basedir = os.path.expanduser("~")
sudo = False
stuff = {
}
if getpass.getuser() == "root":
raise Exception("Don't run this as root you dick be smart")
print(fr("""{}, I'm gonna set up a bunch of stuff for you.""".format(red("Hello"))))
if osx:
stuff["brew"] = False
if not os.path.exists("/usr/local/bin/brew"):
if q("It seems that Homebrew isn't installed. Should I do that?", "y"):
# TODO: handle the return value.
cmd = "/usr/bin/ruby -e \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)\""
if subprocess.call(cmd, shell=True) == 0:
stuff["brew"] = True
else:
raise Exception("Couldn't install homebrew and I can't go on doing other stuff. Sorry bro.")
if linux:
if q("Do you have sudo rights?"):
sudo = True
if linux or osx:
stuff["python3"] = False
if spawn.find_executable("python3") is None:
if osx or linux and sudo:
if q("It seems that Python 3 isn't installed. Should I do that?", "y"):
if subprocess.call("brew install python3" if osx else "sudo apt-get install --yes python3", shell=True) == 0:
stuff["python3"] = True
else:
print(warn("Couldn't install Python 3 for some reason."))
elif linux and not sudo:
print(warn("Python 3 isn't installed. Ask your sysadmin to run `apt-get install python3` for you."))
elif osx:
print(warn("It looks like you have non-homebrew Python 3 installed. Get rid of it and launch\n"
"brew install python3"))
# tmux
if linux or osx:
stuff["tmux"] = False
conf = ('# remap prefix to Control + a\nset -g prefix C-a\nunbind C-b\nbind C-a send-prefix\n\n'
'# force a reload of the config file\nunbind r\nbind r source-file ~/.tmux.conf\n\n'
'unbind C-v\nbind C-v paste-buffer\n\n# quick pane cycling\nunbind ^A\nbind ^A select-pane -t :.+\n\n'
'unbind h\nbind h split-window\n\nunbind v\nbind v split-window -h\n\n'
'bind C-s copy-mode\nsetw -g mode-keys emacs\n\nset-window-option -g xterm-keys on\n\n'
'setw -g mouse on\n'
'bind -n WheelUpPane if-shell -Ft= \'#{mouse_any_flag}\' \'send-keys -M\' '
'\'if -Ft = "#{pane_in_mode}" "send-keys -M" "copy-mode -e"\'\n'
)
if spawn.find_executable("tmux") is None:
if osx or linux and sudo:
if q("It seems that `tmux` isn't installed. Should I do that?", "y"):
if subprocess.call("brew install tmux" if osx else "sudo apt-get install --yes tmux", shell=True) == 0:
stuff["tmux"] = True
else:
print(warn("Couldn't install tmux for some reason."))
else:
stuff["tmux"] = True
if stuff["tmux"]:
if os.path.exists("{}/.tmux.conf".format(basedir)):
print(warn("`tmux` is already installed and ~/.tmux.conf already exists. Not overwriting it."))
else:
if q("I am going to create some `tmux` defaults for you. Is this OK?", "y"):
with open("{}/.tmux.conf".format(basedir), "w") as f:
f.write(conf)
# zsh
if linux or osx:
stuff["zsh"] = False
stuff["oh-my-zsh"] = False
zshpath = "/usr/bin/zsh" if linux else "/usr/local/bin/zsh"
zshrc = (" export ZSH={home}/.oh-my-zsh\n\nZSH_THEME=\"myyc\"\n\n"
"plugins=({plugins})\n\nsource $ZSH/oh-my-zsh.sh\n{locales}\n"
"bindkey \\^U backward-kill-line\n{morestuff}\nfortune -o\necho \"\""
"\n").format(home=basedir,
plugins="git brew" if osx else "git",
locales=("\n# locale setting\nexport LANG=en_IE.UTF-8\n"
"export LC_ALL=en_IE.UTF-8\n") if osx else "",
morestuff="\n# other stuff\nexport HOMEBREW_CASK_OPTS=\"--appdir=/Applications\"\n"
"alias emacs=\"emacs -nw\"\n" if osx else "")
theme = ("PROMPT=\'%{$fg[magenta]%}%n@%B%m%b %B%c%b $(git_prompt_info)% %{$fg[white]%}> %{$reset_color%}\'\n\n"
"ZSH_THEME_GIT_PROMPT_PREFIX=\"%{$fg[blue]%}#%{$fg[red]%}\"\n"
"ZSH_THEME_GIT_PROMPT_SUFFIX=\"%{$fg[green]%} \"\n")
if spawn.find_executable("zsh") is None:
if osx or linux and sudo:
if q("It seems that `zsh` isn't installed. Should I do that?", "y"):
if subprocess.call("brew install zsh" if osx else "sudo apt-get install --yes git zsh", shell=True) == 0:
stuff["zsh"] = True
else:
print(warn("Couldn't install zsh for some reason."))
elif linux and not sudo:
print(warn("`zsh` isn't installed. Ask your sysadmin to run `apt-get install `zsh` for you."))
else:
stuff["zsh"] = True
if stuff["zsh"]:
if os.environ["SHELL"] != zshpath:
if q("I'm going to set `zsh` as your default shell. Is this OK?", "n"):
subprocess.call("chsh -s {}".format(zshpath), shell=True)
if osx:
# dangerous but #yolo
subprocess.call("sudo chmod 666 /etc/shells", shell=False)
r = []
with open("/etc/shells", "r") as f:
r = f.readlines()
if zshpath not in [x.strip() for x in f.readlines()]:
r.append(zshpath + "\n")
with open("/etc/shells", "a") as f:
for x in r:
f.write(x.strip() + "\n")
subprocess.call("sudo chmod 644 /etc/shells", shell=False)
if stuff["zsh"] and not os.path.exists("{}/.zshrc".format(basedir)):
# install oh-my-zsh
if not os.path.exists("{}/.oh-my-zsh".format(basedir)):
if osx or linux:
if q("Do you want to use `oh-my-zsh`? You should...", "y"):
cmd = "git clone https://github.com/robbyrussell/oh-my-zsh ~/.oh-my-zsh"
subprocess.call(cmd, shell=True)
stuff["oh-my-zsh"] = True
else:
print(warn("Couldn't install on-my-zsh for some reason."))
if stuff["oh-my-zsh"]:
if q("I want to set some defaults for you, and a nice theme. "
"Is this OK? This will overwrite your `.zshrc`"):
with open("{}/.zshrc".format(basedir), "w") as f:
f.write(zshrc)
themepath = "{}/.oh-my-zsh/custom/themes".format(basedir)
if not os.path.exists(themepath):
os.mkdir(themepath)
if not os.path.exists("{}/myyc.zsh-theme".format(themepath)):
with open("{}/myyc.zsh-theme".format(themepath), "w") as f:
f.write(theme)
else:
print(warn("There's already a `myyc` theme installed. Not overwriting it!"))
else:
print(warn("I wanted to install `oh-my-zsh` but you already have a `~/.zshrc`. Maybe some other time."))
# fortunes
if linux or osx:
stuff["fortunes"] = False
if spawn.find_executable("fortune") is None:
if osx or linux and sudo:
if q("It seems that `fortune` isn't installed. Should I do that?", "y"):
if subprocess.call("brew install fortune" if osx else "sudo apt-get install --yes fortunes-off", shell=True) == 0:
stuff["fortunes"] = True
else:
print(warn("Couldn't install Python 3 for some reason."))
elif linux and not sudo:
print(warn("`fortune` isn't installed. Ask your sysadmin to run `apt-get install `fortune` for you."))
# hushlogin
if linux:
hush = "{}/.hushlogin".format(basedir)
if not os.path.exists(hush) and q("Do you want to suppress the MOTD?", "y"):
open(hush, "a").close()
print(fr("All done. Log out to apply the changes. {}!".format(red("Bye"))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment