forked from aichallenge/aichallenge
/
install_tools.py
122 lines (104 loc) · 3.81 KB
/
install_tools.py
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
import getpass
import os
import re
from subprocess import Popen, PIPE
class Environ(object):
""" Context manager that sets and restores an environment variable """
def __init__(self, var, value):
self.env_var = var
self.new_value = value
def __enter__(self):
self.start_value = os.environ.get(self.env_var, None)
os.environ[self.env_var] = self.new_value
return self.new_value
def __exit__(self, type, value, traceback):
if self.start_value is not None:
os.environ[self.env_var] = self.start_value
else:
del os.environ[self.env_var]
class CD(object):
""" Context manager to change and restore the current working directory """
def __init__(self, new_dir):
self.new_dir = new_dir
def __enter__(self):
self.org_dir = os.getcwd()
os.chdir(self.new_dir)
return self.new_dir
def __exit__(self, type, value, traceback):
os.chdir(self.org_dir)
def file_contains(filename, line_pattern):
""" Checks if a file has a line matching the given pattern """
if not os.path.exists(filename):
return False
regex = re.compile(line_pattern)
with open(filename, 'r') as src:
for line in src:
if regex.search(line):
return True
return False
def append_line(filename, line):
""" Appends a line to a file """
with open(filename, "a+") as afile:
afile.write(line + '\n')
class CmdError(StandardError):
""" Exception raised on an error return code results from run_cmd """
def __init__(self, cmd, returncode):
self.cmd = cmd
self.returncode = returncode
StandardError.__init__(self, "Error %s returned from %s"
% (returncode, cmd))
def run_cmd(cmd, capture_stdout=False):
""" Run a command in a shell """
print "Executing:", cmd
stdout_loc = PIPE if capture_stdout else None
proc = Popen(cmd, shell=True, stdout=stdout_loc)
output, error_out = proc.communicate()
status = proc.wait()
if status != 0:
raise CmdError(cmd, status)
return output
def install_apt_packages(packages):
""" Install system packages using aptitude """
apt_cmd = "apt-get install -y "
try:
cmd = apt_cmd + packages
except TypeError:
cmd = apt_cmd + " ".join(packages)
run_cmd(cmd)
def get_choice(query, default=False):
negative_responses = ["no", "n"]
positive_responses = ["yes", "y"]
query += " [%s] " % ('yes' if default else 'no')
while True:
resp = raw_input(query).lower().strip()
if resp in negative_responses or (resp == "" and not default):
return False
if resp in positive_responses or (resp == "" and default):
return True
def get_password(pw_name):
while True:
passwd = getpass.getpass("%s password? " % (pw_name.capitalize()))
confirm = getpass.getpass("Confirm %s password? " % (pw_name,))
if passwd == confirm:
return passwd
print "Sorry, passwords did not match."
def get_ubuntu_release_info():
version="notubuntu"
arch="unknown"
try:
version=re.match(".*DISTRIB_CODENAME=(\w*).*",open("/etc/lsb-release").read(),re.DOTALL).group(1)
arch=run_cmd("dpkg --print-architecture",True).strip()
except CmdError, IOError:
arch=run_cmd("uname -p",True).strip()
except:
pass
return version, arch
def check_ubuntu_version():
version,arch=get_ubuntu_release_info()
if version!="notubuntu":
print "Install tools on Ubuntu version:%s arch:%s." % (version, arch)
else:
print "Installing on an %s non-Ubuntu host." % (arch)
if version!="natty":
raise Exception("This contest framework was designed to work on Ubuntu Natty(11.04) only.")
return version, arch