forked from UUDigitalHumanitieslab/gretel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.py
345 lines (298 loc) · 11.4 KB
/
bootstrap.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
""" Run this script directly after cloning the project. """
import os
import os.path as op
from distutils import dir_util
import glob
import json
import platform
import sys
import subprocess
import shlex
import shutil
SLUG = 'gretel5'
WINDOWS = (platform.system() == 'Windows')
VIRTUALENV_BINDIR = 'Scripts' if WINDOWS else 'bin'
LOGFILE_NAME = 'bootstrap.log'
class Command(object):
""" Representation of a command to be run in project setup.
We create a bunch of these to implement most of the steps in main.
"""
@classmethod
def get_log(cls):
if not hasattr(cls, 'log'):
cls.log = open(LOGFILE_NAME, 'w', buffering=1)
return cls.log
def __init__(self, description, command, *args, **kwargs):
self.description = description
if isinstance(command, str):
command = shlex.split(command)
self.command = command
self.args = args
if 'stdout' not in kwargs:
kwargs['stdout'] = self.get_log()
if 'stderr' not in kwargs:
kwargs['stderr'] = subprocess.STDOUT
self.kwargs = kwargs
def __call__(self):
log = self.get_log()
print('{}... '.format(self.description), end='', flush=True)
log.write('$ {}\n\n'.format(self))
try:
exit_code = subprocess.call(self.command, *self.args, **self.kwargs)
if exit_code != 0:
print('failed ({}).'.format(exit_code))
return False
print('success.')
log.write('\n\n')
return True
except Exception as e:
print(e)
log.write('never ran (exception)\n\n')
return False
def __str__(self):
representation = ' '.join(map(shlex.quote, self.command))
if 'cwd' in self.kwargs:
return '(cd {} ; {})'.format(self.kwargs['cwd'], representation)
return representation
def main(argv):
already_in_project, cd_into_project = prepare_cwd()
venv, create_virtualenv, activate_venv = prepare_virtualenv()
pip_tools = backpack = funcpack = False
if venv:
pip_tools = install_pip_tools()
backpack = install_backend_packages()
funcpack = install_functest_packages()
frontpack = install_frontend_packages()
db, create_db = prepare_db()
migrate = superuser = False
if db and backpack:
migrate = run_migrations()
if migrate:
superuser = create_superuser()
main_branch = track_main()
gitflow = False
if main_branch:
gitflow = setup_gitflow()
if not all([gitflow, superuser, frontpack, funcpack, pip_tools]):
print('\nPlease read {} for information on failed commands.'.format(LOGFILE_NAME))
print('\nAlmost ready to go! Just a couple more commands to run:')
if not already_in_project: print(cd_into_project)
if not venv: print(create_virtualenv)
print(activate_venv)
if not (pip_tools and backpack and frontpack and funcpack): print(install_all_packages)
if not db: print(create_db)
if not migrate: print(run_migrations)
if not superuser: print(create_superuser)
if not main_branch: print(track_main)
if not gitflow: print(setup_gitflow)
print(yarn_start)
def prompt(variable, default_value):
return input('{} [{}]: '.format(variable, default_value)) or default_value
def prepare_cwd():
invocation_dir = op.abspath(os.getcwd())
project_root = op.dirname(op.abspath(__file__))
if invocation_dir == project_root:
return True, None
os.chdir(project_root)
relative_path = op.relpath(project_root, invocation_dir)
cd_into_project = Command('', ['cd', relative_path])
return False, cd_into_project
def prepare_virtualenv():
default_env = '.env'
env_path = prompt('virtualenv', default_env)
default_cmd = 'virtualenv {} --prompt="{}"'.format(env_path, SLUG)
env_cmd = prompt('virtualenv_command', default_cmd)
create_command = make_create_venv_command(env_cmd)
activate_command = make_activate_venv_command(env_path)
success = create_command()
if success:
adopt_virtualenv(env_path)
return success, create_command, activate_command
def make_create_venv_command(venv_cmd):
return Command('Create the virtualenv', venv_cmd)
def make_activate_venv_command(venv_path):
activate_helper = ([] if WINDOWS else ['source'])
return Command(
'',
activate_helper + [op.join(venv_path, VIRTUALENV_BINDIR, 'activate')],
)
def adopt_virtualenv(env_path):
""" Enable the virtualenv for our own subprocesses. """
# first lines are a quick imitation of a local bin/activate script
venv_abs = op.abspath(env_path)
os.environ['VIRTUAL_ENV'] = venv_abs
python_bindir = op.join(venv_abs, VIRTUALENV_BINDIR)
os.environ['PATH'] = os.pathsep.join([python_bindir, os.environ['PATH']])
os.environ.pop('PYTHONHOME', None)
# next line is a fix for https://bugs.python.org/issue22490
os.environ.pop('__PYVENV_LAUNCHER__', None)
def prepare_db():
default_cmd = 'psql'
psql_cmd = prompt('psql_command', default_cmd)
create_command = make_create_db_command(psql_cmd)
success = create_command()
return success, create_command
def make_create_db_command(psql_cmd):
# psql does not properly indicate failure; it always exits with 0.
# Fortunately, it is one of the last commands.
return Command(
'Create the database',
psql_cmd + ' -f ' + op.join('backend', 'create_db.sql'),
)
def merge_json(target, source):
for key, value in source.items():
if value == None:
del target[key]
elif key in target and isinstance(target[key], dict) and \
isinstance(source[key], dict):
merge_json(target[key], source[key])
else:
target[key] = value
return target
def modify_angular_json():
with open('web-ui/angular.json', 'r') as file:
data = json.load(file)
try:
project = 'gretel5'.replace('_', '-')
for lang in 'en:english,nl:dutch'.split(','):
[code, lang_name] = lang.split(':')
production = merge_json({}, data['projects'][project]['architect']['build']['configurations']['production'])
production['outputPath'] = f'dist/{code}'
production['i18nFile'] = f'locale/messages.{code}.xlf'
production['i18nFormat'] = 'xlf'
production['i18nLocale'] = code
production['i18nMissingTranslation'] = 'error'
data['projects'][project]['architect']['build']['configurations'][f'production-{code}'] = production
serve = merge_json({}, data['projects'][project]['architect']['serve']['configurations']['production'])
serve['browserTarget'] += f'-{code}'
data['projects'][project]['architect']['serve']['configurations'][code] = serve
data['projects'][project]['architect']['build']['options']['outputPath'] = \
data['projects'][project]['architect']['build']['configurations']['production']['outputPath'] = 'dist'
# remove e2e
del data['projects'][project]['architect']['e2e']
except:
print("Oh no! :( Maybe the format changed?")
print(json.dumps(data, indent=4))
raise
with open('web-ui/angular.json', 'w') as file:
json.dump(data, file, indent=4)
def activate_frontend():
framework = 'angular'
os.rename('package.angular.json', 'package.json')
if framework == 'backbone':
os.rename('frontend.backbone', 'frontend')
shutil.move(op.join('frontend', 'proxy.json'), 'proxy.json')
override_package_json()
elif framework == 'angular':
project_name = 'gretel5'.replace('_', '-')
Command(
'Install dependencies',
['yarn', 'install', '--ignore-scripts']
)()
Command(
'Creating project',
['yarn', 'ng', 'new', project_name, '--prefix=dh',
'--skip-git=true',
'--skip-install=true',
'--package-manager=yarn',
'--style=scss',
'--routing=true']
)()
dir_util.copy_tree('frontend.angular', project_name)
os.rename(project_name, 'frontend')
shutil.move(op.join('frontend', 'proxy.conf.json'), 'proxy.conf.json')
override_package_json()
Command(
'Install frontend dependencies using Yarn',
['yarn'],
cwd="frontend"
)()
# Remove e2e
shutil.rmtree(os.path.join('frontend', 'e2e'))
# Remove editorconfig
os.remove(os.path.join('frontend', '.editorconfig'))
modify_angular_json()
Command(
'Creating localizations',
['yarn', 'ng', 'add', '@angular/localize'],
cwd="frontend"
)()
Command(
'Creating localizations',
['yarn', 'i18n'],
cwd="frontend"
)()
for lang in 'en:english,nl:dutch'.split(','):
[code, lang_name] = lang.split(':')
shutil.copyfile('web-ui/locale/messages.xlf', f'web-ui/locale/messages.{code}.xlf')
if '4200' != '4200':
Command(
'Set frontend port',
['yarn', 'ng', 'config', "projects.gretel5.architect.serve.options.port", '4200'],
cwd="frontend"
)()
else:
print('Unknown framework angular specified!')
# remove other frameworks
for path in glob.glob("frontend.*"):
shutil.rmtree(path)
for path in glob.glob("package.*.json"):
os.remove(path)
def override_package_json():
if os.path.isfile('web-ui/package.overwrite.json'):
print('Overriding package.json')
with open('web-ui/package.overwrite.json', 'r') as file:
overwrite = json.load(file)
with open('web-ui/package.json', 'r') as file:
data = json.load(file)
with open('web-ui/package.json', 'w') as file:
merge_json(data, overwrite)
json.dump(data, file, indent=4)
os.remove('web-ui/package.overwrite.json')
install_pip_tools = Command(
'Install pip-tools',
['yarn', 'preinstall'],
)
install_backend_packages = Command(
'Install the backend requirements',
['yarn', 'install-back'],
)
install_functest_packages = Command(
'Install the functional test requirements',
['yarn', 'install-func'],
)
install_frontend_packages = Command(
'Install the frontend packages',
['yarn', 'fyarn'],
)
install_all_packages = Command('Install all packages', ['yarn'])
run_migrations = Command(
'Run the initial migrations',
['yarn', 'django', 'migrate'],
)
# Github Actions sets "CI" environment variable
if os.environ.get('CI'):
create_superuser = Command(
'Skip creating the superuser',
['yarn', 'back', ':'], # ':' for no-op
stdout=None, # share stdout and stderr with this process
stderr=None,
)
else:
create_superuser = Command(
'Create the superuser',
['yarn', 'django', 'createsuperuser'],
stdout=None, # share stdout and stderr with this process
stderr=None,
)
track_main = Command(
'Create origin-tracking main branch',
['git', 'branch', '--track', 'main', 'origin/main'],
)
setup_gitflow = Command(
'Initialize git-flow',
['git', 'flow', 'init', '-d'],
)
yarn_start = Command('', ['yarn', 'start'])
if __name__ == '__main__':
sys.exit(main(sys.argv))