-
-
Notifications
You must be signed in to change notification settings - Fork 183
/
post_gen_project.py
112 lines (83 loc) · 3.13 KB
/
post_gen_project.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
from __future__ import print_function
import shlex
import sys
import os
import shutil
import subprocess
install_deps = '{{cookiecutter.install_dependencies}}'
project_shortname = '{{cookiecutter.project_shortname}}'
use_async = '{{cookiecutter.use_async}}'
is_windows = sys.platform == 'win32'
if is_windows:
python_executable = os.path.join('venv', 'Scripts', 'python')
else:
python_executable = os.path.join('venv', 'bin', 'python')
def _execute_command(cmd):
line = shlex.split(cmd, posix=not is_windows)
print('Executing: {}'.format(cmd))
# call instead of Popen to get realtime output
status = subprocess.call(line, shell=is_windows)
if status != 0:
print('post_gen_project command failed: {}'.format(cmd),
file=sys.stderr)
sys.exit(status)
return status
# Remove the cookiecutter_templates directory since it only contains
# files that are conditionally included.
template_dir = os.path.join(os.getcwd(), 'cookiecutter_templates')
shutil.rmtree(template_dir)
print("\n\n\nuse_async")
print(use_async)
# If it doesn't use async, we can remove the fragments and lazyloader.js
if use_async != "True":
print('use_async is set to False, your component will not be lazy loaded and fragments will not be created.')
shutil.rmtree(os.path.join(os.getcwd(), 'src', 'lib', 'fragments'))
os.remove(os.path.join(os.getcwd(), 'src', 'lib', 'LazyLoader.js'))
if install_deps != 'True':
print('`install_dependencies` is false!!', file=sys.stderr)
print('Please create a venv in your project root'
' and install the dependencies in requirements.txt',
file=sys.stderr)
sys.exit(0)
# Create a virtual env
if sys.version.split(' ')[0] > '3.2':
venv = '{} -m venv venv'.format(sys.executable)
else:
venv = 'virtualenv venv'
# noinspection PyBroadException
try:
_execute_command(venv)
except BaseException:
print(
'''
venv creation failed.
Make sure you have installed virtualenv on Python 2.
''',
file=sys.stderr
)
raise
print('\n\nInstalling dependencies\n', file=sys.stderr)
# Install Python requirements.
_execute_command(
r'{} -m pip install -r requirements.txt'.format(python_executable)
)
# Install node_modules
_execute_command('npm install --ignore-scripts')
# Run the first build
print('Building initial bundles...')
_execute_command('npm run build:js')
# Activating the venv and running the command
# doesn't work on Linux with subprocess.
# The command needs to run in the venv we just created to use the dash cmd.
# But it also needs shell to be true for the command to work.
# And shell doesn't work with `npm run` nor `. venv/bin/activate`
# The command works in a terminal.
_execute_command("{} -m dash.development.component_generator"
" ./src/lib/components"
" {{cookiecutter.project_shortname}}"
" -p package-info.json"
" --jl-prefix '{{ cookiecutter.jl_prefix }}'"
" --r-prefix '{{ cookiecutter.r_prefix }}'"
.format(python_executable))
print('\n{} ready!\n'.format(project_shortname))
sys.exit(0)