~doctormo/django-deployer/trunk

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
import sys, os, shutil, commands
from getpass import getpass

rootUser = ( os.getuid() == 0 )

def rootRequired():
	if not rootUser:
		sys.stderr.write("You must be root to run '%s'\n" % sys.argv[0])
		sys.exit()


def askQuestions(qs, defaults={}, results={}, fill_empty=False):
	"""
	Asks multiple questions from the user and collects the results
	As a dictionary. using the form:
	[
		{ 'type' : 'string', 'label' : 'Value Foo', default : 'Bar', 'null' : True }
	]
	"""
	for q in qs:
		type = 'string'
		id   = str(q['id'])
		if q.has_key('type'):
			type = q.pop('type')

		if not fill_empty or not results.has_key(id):
			if defaults.has_key(id):
				q['default'] = defaults[id]
		
			if type == 'string':
				results[id] = askForString(**q)
			elif type == 'integer':
				results[id] = askForInteger(**q)
			elif type == 'choices':
				results[id] = askQuestion(**q)
			elif type == 'password':
				results[id] = askForPassword(**q)
			elif type == 'boolean':
				results[id] = askYesNo(**q)

	return results


def askForString(label, default='', id=None, null=False):
	default_label = ''
	if default != '' and default != None:
		if label[-1] == "\n":
			default_label = '[ default: %s ]' % str(default)
		else:
			default_label = ' [ %s ]' % str(default)

	ans = raw_input('\n%s%s: ' % (label, default_label))
	if ans.lower() in ['quit','exit','abort']:
		sys.exit()
	if not ans:
		ans = default
		if (ans == '' or ans == None) and not null:
			sys.stderr.write("You must specify a value for %s\n" % label)
			return askForString(label, default, id, null)
	return ans


def askForInteger(label, default='', id=None, null=False):
	ans = askForString(label, default, id, null)
	try:
		return int(ans)
	except:
		sys.stderr.write("Answer must be an Integer (whole number), please try again.\n" )
		return askForInteger(label, default, id, null)


def askForList(label, id=None):
	data = []
	item = askForString(label, null=True)
	while item:
		data.append(item)
		item = askForString(label, null=True)
	return data


def askYesNo(label, default="n", id=None):
	ans = askForString(label, default, id)
	if ans.lower()[0] == 'y' or ans.lower() == 'true' or ans == '1':
		return True
	return False


def askQuestion(label, options, default=0, id=None, null=False, asked=False):
	answers = ''
	index   = 0
	labels  = options
	values  = options
	if type(options) == dict:
		labels = options.values()
		values = options.keys()
	if not asked:
		for option in labels:
			answers += "  %d. %s\n" % (index, option)
			index += 1
	ans = askForString("\n" + label + "\n\n" + answers, default, id, null)
	if ans in values:
		return ans
	elif ans in labels:
		for value in values:
			if ans == options[value]:
				return value
	try:
		ans = int(ans)
		if ( ans >= 0 and ans < len(labels) ):
			return values[ans]
	except:
		pass
	sys.stderr.write("Please choose from the options provided. %d isn't valid\n" % ans )
	return askQuestion(label, options, default, id, null, asked=True)


def askForPassword(label, id=None, null=False):
	ans = getpass(label+': ')
	if not ans and not null:
		sys.stderr.write("You must specify the password for %s\n" % label)
		return askForPassword(label, id, null)
	return ans