This repository was archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathinstall_utils.py
More file actions
177 lines (134 loc) · 4.26 KB
/
install_utils.py
File metadata and controls
177 lines (134 loc) · 4.26 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
166
167
168
169
170
171
172
173
174
175
176
177
import sys
import os
import subprocess
# input/output utils
def ask(question, default='yes'):
"""
Ask a yes/no question via raw_input() and return their answer.
source: http://stackoverflow.com/questions/3041986/python-command-line-yes-no-input
"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().strip()
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 get_input(prompt, default=None):
"""
Get user's input, only checking for non-empty response.
"""
user_input = ''
if default is not None:
prompt = (
'{prompt} (default: {})').format(default, prompt=prompt)
while user_input == '':
user_input = raw_input(prompt + '\n').strip()
if user_input == '':
if default is not None:
user_input = default
else:
print 'The value cannot be blank.'
return user_input
# helper functions converted from one line script utils to python callable
def print_warn(msg):
call_command('tput setaf 3') # 3 = yellow
sys.stdout.write('[ WARNING ]\n')
call_command('tput sgr0')
sys.stderr.write(msg + '\n')
def print_failure():
call_command('tput setaf 1') # 1 = red
print_right('[ FAILED ]')
call_command('tput sgr0')
def print_success():
call_command('tput setaf 2') # 2 = green
print_right('[ OK ]')
call_command('tput sgr0')
def print_step(msg):
call_command('tput setaf 6')
sys.stdout.write(msg + '\n')
call_command('tput sgr0')
def print_right(msg):
call_command('tput cuu1')
call_command('tput cuf $(tput cols)')
call_command('tput cub %d' % len(msg))
sys.stdout.write(msg + '\n')
def exit_with_message(msg):
sys.stderr.write(msg + '\n')
sys.exit(1)
def exit_with_failrue(msg):
print_failure()
exit_with_message(msg)
# utils using subprocess
def call_command(command):
"""
Process the given command in a bash shell and return the returncode.
Warning: Make sure the command is sanitized before
calling this function to prevent any vulnerability.
"""
res = subprocess.call(
command, shell=True,
executable='/bin/bash')
return res
def command_exists(command):
"""check if hash can find a path for the command
From install.sh
"""
res = call_command('hash ' + command + ' >/dev/null 2>&1')
if res != 0:
return False
else:
return True
def get_command_output(command):
try:
res = subprocess.check_output(
command, shell=True,
stderr=subprocess.STDOUT,
executable='/bin/bash')
except:
res = None
return res
# utils using os
def check_path_exists(path, expand=False):
if(expand):
path = os.path.expanduser(path)
return os.path.exists(path)
# Other helpers
def write_file(filename):
try:
out = open(filename, 'w')
except:
sys.stderr.write('Unable to write %s.\n' % filename)
out = None
return out
def get_http_status(url):
status_cmd = 'curl --head -s ' + url + ' | head -n 1'
return get_command_output(status_cmd)
if __name__ == '__main__':
print_warn('This is for testing install_utils.py')
ask('Begin testing')
print call_command('ls > /dev/null')
print command_exists('python')
print_step('Next step is')
print_warn('REALLY long text'*10)
get_input('just checking:', 'default')