-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtasks_utils.py
More file actions
73 lines (55 loc) · 1.89 KB
/
tasks_utils.py
File metadata and controls
73 lines (55 loc) · 1.89 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
"""
Goal: store functions used in tasks.py
@authors:
Andrei Sura <sura.andrei@gmail.com>
"""
import sys
from invoke import run
def get_db_name(db_type='mysql'):
cmd = "grep -i 'create database' schema/{}/000/upgrade.sql " \
" | cut -d ' ' -f3 | tr -d ';'".format(db_type)
try:
result = run(cmd, hide=True)
return result.stdout.strip()
except Exception as exc:
print("Failed to run [{}] due: {}".format(cmd, exc))
def check_db_exists(db_name, db_type='mysql'):
"""
TODO: always returns True for non-mysql databases
"""
if db_type != 'mysql':
return True
cmd = "echo 'select count(*) from information_schema.SCHEMATA " \
"WHERE SCHEMA_NAME = \"{}\"' | mysql -uroot " \
"| sort | head -1".format(db_name)
try:
result = run(cmd, hide=True)
return result.stdout.strip() == '1'
except Exception as exc:
print("Failed to run [{}] due: {}".format(cmd, exc))
def ask_yes_no(question, default="y"):
"""Ask a yes/no question via raw_input() and return the answer
as a boolean.
:param question: the question displayed to the user
:param default: the default answer if the user hits <Enter>
"""
valid = {"y": True, "n": False}
if default is None:
prompt = " [y/n] "
elif default == "y":
prompt = " [Y/n] "
elif default == "n":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = input().lower()
if default is not None and choice == '':
return valid[default]
choice_letter = choice[0]
if choice_letter in valid:
return valid[choice_letter]
else:
sys.stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")