-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathinitial_setup.py
executable file
·174 lines (136 loc) · 5.55 KB
/
initial_setup.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
#!/usr/bin/env python
__author__ = 'johann'
import os
import subprocess
import fileinput
import shutil
from sys import stderr
from sys import stdin
from sys import stdout
import random
random = random.SystemRandom()
TERMINAL_WIDTH = 80
def get_random_string(length=12,
allowed_chars='abcdefghijklmnopqrstuvwxyz'
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'):
"""
Returns a securely generated random string.
The default length of 12 with the a-z, A-Z, 0-9 character set returns
a 71-bit value. log_2((26+26+10)^12) =~ 71 bits.
Taken from the django.utils.crypto module.
"""
return ''.join(random.choice(allowed_chars) for i in range(length))
def get_secret_key():
"""
Create a random secret key.
Taken from the Django project.
"""
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
return get_random_string(50, chars)
def get_answer(question, proper_answers=('yes','no')):
"""
Ask a question and keep asking until a proper answer is received.
Default proper answers are 'yes' and 'no'.
"""
answer = ''
while answer not in proper_answers:
stdout.write(question)
stdout.flush()
answer = stdin.readline().strip().lower()
return answer
print "*" * TERMINAL_WIDTH
print "Setting up Wasa2il from scratch with an SQLite database called 'wasa2il.sqlite'."
print "This script assumes that both Python and Pip are installed."
print "*" * TERMINAL_WIDTH
# Install (or upgrade) Python package dependencies
stdout.write('Installing dependencies:\n')
result = subprocess.call(["pip", "install", "--upgrade", "-r", "requirements.txt"])
if result == 0:
stdout.write('Dependency installation complete.\n')
else:
if get_answer('Dependency installation seems to have failed. Continue anyway? (yes/no): ') != 'yes':
stdout.write('Okay, quitting.\n')
quit(1)
# Check if Django is installed
try:
stdout.write('Checking if Django is installed...')
stdout.flush()
import django
stdout.write(' yes (%d.%d.%d)\n' % (django.VERSION[0], django.VERSION[1], django.VERSION[2]))
except ImportError:
stdout.write(' no\n')
stderr.write('Error: Cannot continue without Django which should be installed but is not. Quitting.\n')
quit(2)
# Check if local_settings.py exists and create it if it doesn't
setup_local_settings = False
if not os.path.exists('wasa2il/local_settings.py'):
# Create the file from local_settings.py-example
stdout.write('Creating local settings file (local_settings.py)...')
stdout.flush()
try:
shutil.copy('wasa2il/local_settings.py-example', 'wasa2il/local_settings.py')
stdout.write(' done\n')
except IOError as e:
stdout.write(' failed\n')
stderr.write('%s\n' % e.__str__())
quit(1)
# Go through local_settings.py and fix settings if needed
stdout.write('Checking local settings configuration:\n')
local_settings_changed = False
for line in fileinput.input('wasa2il/local_settings.py', inplace=1):
if line.startswith("SECRET_KEY = ''"):
stdout.write('- Setting secret key to random string...')
stdout.flush()
print "SECRET_KEY = '%s'" % get_secret_key()
stdout.write(' done\n')
local_settings_changed = True
elif line.startswith("DATABASE_ENGINE = 'django.db.backends.'"):
stdout.write('- Configuring database engine (SQLite)...')
stdout.flush()
print "DATABASE_ENGINE = 'django.db.backends.sqlite3'"
stdout.write(' done\n')
local_settings_changed = True
elif line.startswith("DATABASE_NAME = ''"):
stdout.write('- Setting database name (wasa2il.sqlite)...')
stdout.flush()
print "DATABASE_NAME = 'wasa2il.sqlite'"
stdout.write(' done\n')
local_settings_changed = True
else:
print line.strip()
if not local_settings_changed:
stdout.write('- No changes needed.\n')
# Change to Wasa2il's directory
os.chdir('wasa2il')
# Compile the translation files
for lang in next(os.walk(os.path.join(os.getcwd(), 'locale')))[1]:
subprocess.call(['pybabel', 'compile', '-d', os.path.join(os.getcwd(), 'locale'), '-D', 'django', '-l', lang])
# Setup database if needed
create_database = False
if os.path.exists('wasa2il.sqlite'):
if get_answer('SQLite database already exists. Kill it and start over? (yes/no): ') == 'yes':
stdout.write('Deleting wasa2il.sqlite...')
stdout.flush()
os.remove('wasa2il.sqlite')
stdout.write(' done\n')
create_database = True
else:
create_database = True
if create_database:
stdout.write('Setting up database (via "migrate"):\n')
migrate_result = subprocess.call(['python', os.path.join(os.getcwd(), 'manage.py'), 'migrate'])
if migrate_result != 0:
stderr.write('Error: Django migration gave errors. Quitting.\n')
quit(1)
stdout.write('We will now create a superuser to configure polities within Wasa2il once it has been set up.\n')
subprocess.call(['python', os.path.join(os.getcwd(), 'manage.py'), 'createsuperuser'])
print "*" * TERMINAL_WIDTH
print "All done!"
print "To run Wasa2il and start configuring polities, follow these steps:"
print "- Go to the 'wasa2il' directory and run 'python manage.py runserver'"
print "- Open your favorite browser and type in: http://localhost:8000"
print "- Log in with the superuser account created previously"
print
print "(If you don't have a superuser account yet, then go to the 'wasa2il' directory"
print "and run 'python manage.py createsuperuser')"
print "*" * TERMINAL_WIDTH