-
Notifications
You must be signed in to change notification settings - Fork 153
/
dev.py
394 lines (310 loc) · 11.2 KB
/
dev.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# -*- coding: utf-8 -*-
"""
usage: dev.py [-h] (-v | -d | -c | -run-vagrant | -run-docker | -no-container)
[--with-docs] [--with-npm-settings]
optional arguments:
-h, --help show this help message and exit
-v, --vagrant Install Vagrant development environment (requires
Vagrant).
-d, --docker Install Docker development environment (requires
Docker).
-c, --clean Clean up the development files (only the ones that have
been automatically created).
-run-vagrant Run vagrant development environment (runs --vagrant if
the files don't already exist). Vagrant must be
installed on your machine.
-run-docker Run vagrant development environment (runs --vagrant if
the files don't already exist). Docker must already be
installed on your machine, and Docker Daemon must be up
and running.
-no-container Run vagrant development environment (runs --vagrant if
the files don't already exist).
--with-docs Install documentation development environment.
--with-npm-settings Install npm installation environment for front-end
(TypeScript) development (requires node.js and npm).
Copyright (c) 2017, Django MarkdownX - Adi, Pouria Hadjibagheri.
"""
# Python's internal libraries:
from __future__ import unicode_literals
from os.path import join as join_path, dirname, abspath, exists, splitext
from os import remove, chmod, stat
from stat import S_IEXEC
from sys import executable as python_path
from sys import exit as sys_exit
# Third party libraries:
try:
from pip._internal import main as pip_main
except ImportError: # pip < 10
from pip import main as pip_main
BASE_DIR = dirname(abspath(__file__))
DEV_XML_FILE = 'dev.xml'
XML_FILE_ABSOLUTE_PATH = join_path(BASE_DIR, DEV_XML_FILE)
pypi_packages = {
'mkdocs',
'pymdown-extensions',
}
bash_commands = {
'vagrant': (
'cd {path}'.format(path=BASE_DIR),
'vagrant up',
'vagrant ssh -c "/home/vagrant/.virtualenvs/django-markdownx/bin/python -u '
'/srv/django-markdownx/manage.py runserver 0.0.0.0:8000"'
),
'docs': (
'mkdocs build',
),
'docker': (
'docker-compose build',
'docker-compose up -d'
)
}
def quiz(question, options, max_attempts=3):
from collections import Counter
count = Counter([item[0] for item in options])
if max(count.values()) > 1:
raise ValueError(
'Multiple options start with '
'character "{}".'.format(max(count, lambda x: count[x]))
)
current_attempt = 0
opts = tuple(key[0] for key in options)
opts_str = str.join(
', ',
('[{}]{}'.format(key, options[index][1:]) for index, key in enumerate(opts))
)
while current_attempt < max_attempts:
try:
response = input(
'> {question}\n'
' {opts} (ctrl+c to cancel): '.format(question=question, opts=opts_str)
)
if response.strip() in opts:
return options[opts.index(response.strip())]
print('\nInvalid response.')
current_attempt += 1
except KeyboardInterrupt:
print('\nOperation cancelled by the user. Exited with code 0.')
sys_exit(0)
else:
print(
'\nFailed {} attempts. Operation cancelled, '
'exited with code 1.'.format(max_attempts)
)
sys_exit(1)
def yes_no_quiz(question, max_attempts=3):
response = quiz(
question=question,
options=('Yes', 'No'),
max_attempts=max_attempts
)
return response == 'Yes'
def replace_contents_or_not(path):
replace_response = 'override and update the default'
replace_or_override = dict(
question='Contents of the existing "{}" and default values don\'t match. '
'Would you like to...\n'.format(path),
options=(
'override and update the default',
'replace changes with the default'
)
)
return quiz(**replace_or_override) == replace_response
def from_terminal(subject):
from subprocess import run as run_bash
from shlex import split as shlex_split
for command in bash_commands[subject]:
print('> EXECUTING:', command)
run_bash(shlex_split(command), timeout=None, check=True)
return True
def create_files(name):
from xml.etree.ElementTree import parse
from xml.sax.saxutils import escape, unescape
contents_xml = parse(XML_FILE_ABSOLUTE_PATH)
root = contents_xml.getroot()
for file in root:
if name in file.attrib['for'].split(';'):
relative_path = [node.text for node in file.iterfind('path')]
absolute_path = join_path(BASE_DIR, *relative_path)
display_path = join_path('markdownx', *relative_path)
template_contents = file.find('contents').text
if exists(absolute_path):
with open(absolute_path, mode='r') as data_file:
file_io = data_file.read()
contents_identical = template_contents.strip() == escape(file_io.strip())
if not contents_identical and replace_contents_or_not(display_path):
file.find('contents').text = escape(file_io)
elif not contents_identical:
with open(absolute_path, mode='w') as file_io:
file_io.write(unescape(template_contents) + '\n')
print('> REPLACED with default:', display_path)
else:
with open(absolute_path, mode='w') as target_file:
target_file.write(unescape(template_contents))
print('> CREATED:', display_path)
if splitext(absolute_path)[1] == '.sh' or absolute_path.endswith('manage.py'):
st = stat(absolute_path)
chmod(absolute_path, st.st_mode | S_IEXEC)
contents_xml.write(
file_or_filename=XML_FILE_ABSOLUTE_PATH,
xml_declaration=True,
encoding='UTF-8',
method='xml'
)
return True
def delete(abs_path, disp_path):
try:
remove(abs_path)
except IOError as error:
print('Failed to delete "{}"\n'.format(disp_path), error)
print('> REMOVED:', disp_path)
return True
def clean():
response = yes_no_quiz(
question="Are you sure you want to clean up "
"the developers' environment?"
)
if not response:
print('Exited at user\'s request with code 0.')
sys_exit(0)
from xml.etree.ElementTree import parse
from xml.sax.saxutils import escape
contents_xml = parse(XML_FILE_ABSOLUTE_PATH)
root = contents_xml.getroot()
for file in root:
relative_path = [node.text for node in file.iterfind('path')]
absolute_path = join_path(BASE_DIR, *relative_path)
display_path = join_path('markdownx', *relative_path)
if not exists(absolute_path):
continue
with open(absolute_path, mode='r') as data_file:
file_content = data_file.read()
if file.find('contents').text.strip() == escape(file_content.strip()):
delete(absolute_path, display_path)
continue
if replace_contents_or_not(display_path):
file.find('contents').text = escape(file_content)
print('> UPDATED in default setting:', display_path)
delete(absolute_path, display_path)
continue
delete(absolute_path, display_path)
delete(join_path(BASE_DIR, 'db.sqlite3'), 'db.sqlite3')
contents_xml.write(
file_or_filename=XML_FILE_ABSOLUTE_PATH,
xml_declaration=True,
encoding='UTF-8',
method='xml'
)
return True
def docs():
subject = 'docs'
create_files(subject)
pip_install(*pypi_packages)
return from_terminal(subject)
def vagrant():
subject = 'vagrant'
return create_files(subject)
def run_vagrant():
subject = 'vagrant'
vagrant()
return from_terminal(subject)
def docker():
subject = 'docker'
return create_files(subject)
def run_docker():
subject = 'docker'
docker()
return from_terminal(subject)
def npm():
subject = 'npm'
return create_files(subject)
def no_container():
subject = 'no-container'
return create_files(subject)
def pip_install(*packages):
for package in packages:
pip_main(['install', package])
return True
def main():
import argparse
from datetime import datetime
parser = argparse.ArgumentParser(
description='Welcome to Django MarkdownX Developers\' Environment.',
epilog=(
"Copyright (c) {}, Django MarkdownX - "
"Adi, Pouria Hadjibagheri.".format(
datetime.now().strftime('%Y')
)
)
)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
'-v',
'--vagrant',
action='store_const',
const=vagrant,
dest='run',
help='Install Vagrant development environment (requires Vagrant).'
)
group.add_argument(
'-d',
'--docker',
action='store_const',
dest='run',
const=docker,
help='Install Docker development environment (requires Docker).'
)
group.add_argument(
'-c',
'--clean',
action='store_const',
const=clean,
dest='run',
help='Clean up the development files (only the ones that '
'have been automatically created).'
)
group.add_argument(
'-run-vagrant',
action='store_const',
dest='run',
const=run_vagrant,
help='Run vagrant development environment '
'(runs --vagrant if the files don\'t already exist). '
'Vagrant must be installed on your machine.'
)
group.add_argument(
'-run-docker',
action='store_const',
dest='run',
const=run_docker,
help='Run docker development environment '
'(runs --docker if the files don\'t already exist). '
'Docker must already be installed on your machine, and '
'Docker Daemon must be up and running.'
)
group.add_argument(
'-no-container',
action='store_const',
dest='run',
const=no_container,
help='Create development files without a container-based '
'development environment (creates "manage.py" and "runtests.py").'
)
parser.add_argument(
'--with-docs',
action='store_const',
const=docs,
dest='run',
help='Install documentation development environment.'
)
parser.add_argument(
'--with-npm-settings',
action='store_const',
const=npm,
dest='run',
help='Install npm installation environment for front-end '
'(TypeScript) development (requires node.js and npm).'
)
parser.parse_args().run()
return parser
if __name__ == '__main__':
main()