-
Notifications
You must be signed in to change notification settings - Fork 110
/
shutit_util.py
347 lines (303 loc) · 11.6 KB
/
shutit_util.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env pythen
"""ShutIt utility functions.
"""
# The MIT License (MIT)
#
# Copyright (C) 2014 OpenBet Limited
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# ITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import print_function
import binascii
import getpass
import logging
import os
import random
import re
import readline
import signal
import socket
import stat
import string
import sys
import threading
import time
import traceback
import shutit_assets
import shutit_class
import shutit_global
import shutit
if shutit_global.shutit_global_object.ispy3:
from builtins import input
else:
input=raw_input
def is_file_secure(file_name):
"""Returns false if file is considered insecure, true if secure.
If file doesn't exist, it's considered secure!
"""
if not os.path.isfile(file_name):
return True
file_mode = os.stat(file_name).st_mode
if file_mode & (stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IWOTH | stat.S_IXOTH):
return False
return True
def colorise(code, msg):
"""Colorize the given string for a terminal.
See https://misc.flogisoft.com/bash/tip_colors_and_formatting
"""
return '\033[%sm%s\033[0m' % (code, msg) if code else msg
def emblinken(msg):
"""Blink the message for a terminal
"""
return '\033[5m%s\033[0m' % msg
def random_id(size=8, chars=string.ascii_letters + string.digits):
"""Generates a random string of given size from the given chars.
@param size: The size of the random string.
@param chars: Constituent pool of characters to draw random characters from.
@type size: number
@type chars: string
@rtype: string
@return: The string of random characters.
"""
return ''.join(random.choice(chars) for _ in range(size))
def random_word(size=6):
"""Returns a random word in lower case.
"""
words = shutit_assets.get_words().splitlines()
word = ''
while len(word) != size or "'" in word:
word = words[int(random.random() * (len(words) - 1))]
return word.lower()
def get_hash(string_to_hash):
"""Helper function to get preceding integer
eg com.openbet == 1003189494
>>> import binascii
>>> abs(binascii.crc32(b'shutit.tk'))
782914092
Recommended means of determining run order integer part.
"""
return abs(binascii.crc32(string_to_hash.encode()))
# get the ordinal for a given char, in a friendly way
def get_wide_hex(char):
if len(char) != 2:
return r'\x' + hex(ord(char))[2:]
return r'\u' + hex(0x10000 + (ord(char[0]) - 0xD800) * 0x400 + (ord(char[1]) - 0xDC00))[2:]
# CTRL-\ HANDLING CODE STARTS
def ctrl_quit_signal_handler(_, frame):
shutit_global.shutit_global_object.shutit_print(r'CRTL-\ caught, hard-exiting ShutIt')
shutit_frame = get_shutit_frame(frame)
if shutit_frame:
shutit_class.do_finalize()
shutit_global.shutit_global_object.handle_exit(exit_code=1)
# CTRL-\ HANDLING CODE ENDS
# CTRL-C HANDLING CODE STARTS
in_ctrlc = False
def ctrlc_background():
global ctrl_c_calls
global in_ctrlc
ctrl_c_calls += 1
if ctrl_c_calls > 10:
shutit_global.shutit_global_object.handle_exit(exit_code=1)
in_ctrlc = True
time.sleep(1)
in_ctrlc = False
def ctrl_c_signal_handler(_, frame):
"""CTRL-c signal handler - enters a pause point if it can.
"""
global ctrl_c_calls
ctrl_c_calls += 1
if ctrl_c_calls > 10:
shutit_global.shutit_global_object.handle_exit(exit_code=1)
shutit_frame = get_shutit_frame(frame)
if in_ctrlc:
msg = 'CTRL-C hit twice, quitting'
if shutit_frame:
shutit_global.shutit_global_object.shutit_print('\n')
shutit = shutit_frame.f_locals['shutit']
shutit.log(msg, level=logging.CRITICAL)
else:
shutit_global.shutit_global_object.shutit_print(msg)
shutit_global.shutit_global_object.handle_exit(exit_code=1)
if shutit_frame:
shutit = shutit_frame.f_locals['shutit']
if shutit.build['ctrlc_passthrough']:
shutit.self.get_current_shutit_pexpect_session().pexpect_child.sendline(r'')
return
shutit_global.shutit_global_object.shutit_print(colorise(31, "\r" + r"You may need to wait for a command to complete before a pause point is available. Alternatively, CTRL-\ to quit."))
shutit.build['ctrlc_stop'] = True
t = threading.Thread(target=ctrlc_background)
t.daemon = True
t.start()
# Reset the ctrl-c calls
ctrl_c_calls = 0
return
shutit_global.shutit_global_object.shutit_print(colorise(31, '\n' + '*' * 80))
shutit_global.shutit_global_object.shutit_print(colorise(31, "CTRL-c caught, CTRL-c twice to quit."))
shutit_global.shutit_global_object.shutit_print(colorise(31, '*' * 80))
t = threading.Thread(target=ctrlc_background)
t.daemon = True
t.start()
# Reset the ctrl-c calls
ctrl_c_calls = 0
def get_shutit_frame(frame):
global ctrl_c_calls
ctrl_c_calls += 1
if ctrl_c_calls > 10:
shutit_global.shutit_global_object.handle_exit(exit_code=1)
if not frame.f_back:
return None
else:
if 'shutit' in frame.f_locals:
return frame
return get_shutit_frame(frame.f_back)
ctrl_c_calls = 0
# CTRL-C HANDLING CODE ENDS
def print_frame_recurse(frame):
if frame.f_back:
shutit_global.shutit_global_object.shutit_print('=' * 77)
shutit_global.shutit_global_object.shutit_print(frame.f_locals)
print_frame_recurse(frame.f_back)
def check_regexp(regex):
if regex is None:
# Is this ok?
return True
try:
re.compile(regex)
return True
except re.error:
return False
def sendline(child, line):
"""Handles sending of line to pexpect object.
"""
child.sendline(line)
def sanitize_terminal():
os.system('stty sane')
def exit_cleanup():
countdown = range(60, 1, -1)
sys.stdout.write('\n\r')
for s in countdown:
sys.stdout.write('ShutIt has exited, resetting terminal in ' + str(s) + ' unless you CTRL-C...\n\r')
time.sleep(1)
sys.stdout.flush()
os.system('reset')
def util_raw_input(prompt='', default=None, ispass=False, use_readline=True):
"""Handles raw_input calls, and switches off interactivity if there is apparently
no controlling terminal (or there are any other problems)
"""
if use_readline:
try:
readline.read_init_file('/etc/inputrc')
except IOError:
pass
readline.parse_and_bind('tab: complete')
prompt = '\r\n' + prompt
if ispass:
prompt += '\r\nInput Secret: '
sanitize_terminal()
if shutit_global.shutit_global_object.interactive == 0:
return default
## See: https//github.com/ianmiell/shutit/issues/299 - python3 made input == python 2's raw_input
#if not shutit_global.shutit_global_object.ispy3:
# input = raw_input
#try:
# input
#except NameError:
# shutit_global.shutit_global_object.shutit_print('input not available, printing debug')
# print_debug()
# sys.exit(1)
if not shutit_global.shutit_global_object.determine_interactive():
return default
while True:
try:
if ispass:
return getpass.getpass(prompt=prompt)
else:
return input(prompt).strip() or default
except KeyboardInterrupt:
continue
except IOError:
msg = 'Problems getting raw input, assuming no controlling terminal.'
if ispass:
return getpass.getpass(prompt=prompt)
else:
return input(prompt).strip() or default
shutit_global.shutit_global_object.set_noninteractive(msg=msg)
return default
def get_input(msg, default='', valid=None, boolean=False, ispass=False, color=None):
"""Gets input from the user, and returns the answer.
@param msg: message to send to user
@param default: default value if nothing entered
@param valid: valid input values (default == empty list == anything allowed)
@param boolean: whether return value should be boolean
@param ispass: True if this is a password (ie whether to not echo input)
@param color: Color code to colorize with (eg 32 = green)
"""
# switch off log tracing when in get_input
log_trace_when_idle_original_value = shutit_global.shutit_global_object.log_trace_when_idle
shutit_global.shutit_global_object.log_trace_when_idle = False
if boolean and valid is None:
valid = ('yes', 'y', 'Y', '1', 'true', 'no', 'n', 'N', '0', 'false')
if color:
answer = util_raw_input(prompt=colorise(color, msg),ispass=ispass)
else:
answer = util_raw_input(msg, ispass=ispass)
if boolean and answer in ('', None) and default != '':
# Revert log trace value to original
shutit_global.shutit_global_object.log_trace_when_idle = log_trace_when_idle_original_value
return default
if valid is not None:
while answer not in valid:
shutit_global.shutit_global_object.shutit_print('Answer must be one of: ' + str(valid),transient=True)
if color:
answer = util_raw_input(prompt=colorise(color, msg),ispass=ispass)
else:
answer = util_raw_input(msg, ispass=ispass)
if boolean:
if answer.lower() in ('yes','y','1','true','t'):
# Revert log trace value to original
shutit_global.shutit_global_object.log_trace_when_idle = log_trace_when_idle_original_value
return True
elif answer.lower() in ('no','n','0','false','f'):
# Revert log trace value to original
shutit_global.shutit_global_object.log_trace_when_idle = log_trace_when_idle_original_value
return False
# Revert log trace value to original
shutit_global.shutit_global_object.log_trace_when_idle = log_trace_when_idle_original_value
return answer or default
def print_debug(exc_info=None, msg=''):
# TODO: put the file in a smarter place
f = open('/tmp/shutit_debug.txt', 'w')
if msg:
shutit_global.shutit_global_object.shutit_print('Message: ' + msg, debugfile=f)
environ_string = ''
for env in os.environ:
environ_string += 'export ' + env + '=' + str(os.environ[env]) + ';'
shutit_global.shutit_global_object.shutit_print('\n=============================== DEBUG INFO START ==============================', debugfile=f)
shutit_global.shutit_global_object.shutit_print('This file: ' + os.path.dirname(os.path.realpath(__file__)), debugfile=f)
shutit_global.shutit_global_object.shutit_print('Python version: ' + 'sys.version_info: ' + str(sys.version_info) + ', sys.version: ' + str(sys.version), debugfile=f)
shutit_global.shutit_global_object.shutit_print('Shutit version: ' + shutit.shutit_version, debugfile=f)
shutit_global.shutit_global_object.shutit_print('Server: ' + socket.gethostname(), debugfile=f)
shutit_global.shutit_global_object.shutit_print('Environment: ' + environ_string.replace(';', '\n'), debugfile=f)
shutit_global.shutit_global_object.shutit_print('Command was: ' + sys.executable + (' ').join(sys.argv), debugfile=f)
shutit_global.shutit_global_object.shutit_print('ShutIt global state: ' + str(shutit_global.shutit_global_object), debugfile=f)
if exc_info:
stack_trace = ''
for line in traceback.format_exception(*exc_info):
stack_trace += line
shutit_global.shutit_global_object.shutit_print('Stacktrace:\n' + stack_trace, debugfile=f)
shutit_global.shutit_global_object.shutit_print('\n=============================== DEBUG INFO DONE ==============================', debugfile=f)