Snippets
Created by
Paweł R
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 | #!/usr/bin/python
import sys
import getopt
import os
from os.path import expanduser
def main(argv):
userName = "user"
gopath = "golang"
try:
opts, args = getopt.getopt(argv, "hug:d", ["help", "user=", "gopath="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt == '-d':
global _debug
_debug = 1
elif opt in ("-u", "--user"):
userName = arg
elif opt in ("-g", "--gopath"):
gopath = arg
home = expanduser("~")
bashrc = home + "/.bashrc"
zshrc = home + "/.zshrc"
os.system("mkdir %s" % home + "/" + gopath)
if not userName == "user":
os.system("mkdir -p %s" % home + "/src/github.com/" + userName)
append_line_to_file("export GOPATH=$gopath", bashrc)
append_line_to_file("export GOROOT=/usr/local/opt/go/libexec", bashrc)
append_line_to_file("export PATH=$PATH:$GOPATH/bin", bashrc)
append_line_to_file("export PATH=$PATH:$GOROOT/bin", bashrc)
append_line_to_file("export GOPATH=$gopath", zshrc)
append_line_to_file("export GOROOT=/usr/local/opt/go/libexec", zshrc)
append_line_to_file("export PATH=$PATH:$GOPATH/bin", zshrc)
append_line_to_file("export PATH=$PATH:$GOROOT/bin", zshrc)
if (which("brew") == None):
installBrew = query_yes_no("Do you want to intstall brew?")
if installBrew:
success = os.system('ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"')
if success == False:
print "Failed to install brew! Maybe try manual instal?"
sys.exit(2)
else:
print "Aborting instalation"
sys.exit(2)
os.system('brew update')
os.system('brew install go')
os.system('brew install git')
os.system('brew install mercurial')
success = True
if success:
success = os.system('go get golang.org/x/tools/cmd/godoc | go get golang.org/x/tools/cmd/vet')
if success:
print "Successfully installed golang"
else:
print "Failed to install golang"
sys.exit(2)
else:
print "Failed to install golang"
sys.exit(2)
def usage():
print "python golangInstall.py [-u gitUserName] [-g gopath] [-h]"
def which(program):
import os
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
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 True for "yes" or False for "no".
"""
valid = {"yes": True, "y": True, "ye": True,
"no": False, "n": False}
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 True:
sys.stdout.write(question + prompt)
choice = raw_input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")
def append_line_to_file(line, file):
value = str(line)
with open(file, 'a+') as f:
f.seek(0)
if not any(value == x.rstrip('\r\n') for x in f):
f.write(value + '\n')
if __name__ == "__main__":
main(sys.argv[1:])
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.