This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.py
125 lines (105 loc) · 4.29 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
#!/usr/bin/env python
# bootstrap.py
# Bootstrap and setup a virtualenv with the specified requirements.txt
import argparse
import os
import stat
import sys
from shutil import move
from subprocess import call
from tempfile import mkstemp
description = """
Set up my development environment for me!
"""
project_name = 'modelbouwdag'
parser = argparse.ArgumentParser(description=description)
parser.add_argument('target', choices=['production', 'staging', 'test', 'dev'],
help='production/staging/dev')
parser.add_argument('--project', default=project_name,
help='Name of the project in your src directory, "%s" by default' % project_name)
parser.add_argument('--env', default='env',
help='Directory name for virtualenv, "env" by default')
args = parser.parse_args()
def replace_or_append(file_path, search_val, replace_val):
file_handle, abs_path = mkstemp()
new_file = open(abs_path, 'w')
old_file = open(file_path, 'r')
found = False
for line in old_file:
if line.startswith(search_val):
new_file.write(replace_val)
found = True
else:
new_file.write(line)
if not found:
new_file.write("\n" + replace_val)
new_file.close()
os.close(file_handle)
old_file.close()
os.remove(file_path)
move(abs_path, file_path)
os.chmod(file_path, 436)
def replace_wsgi_settings(target):
path = os.path.join('src', project_name, 'wsgi.py')
replace_or_append(
path, 'os.environ.setdefault',
'os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.conf.%s")\n' % (project_name, target)
)
def replace_manage_settings(target):
path = os.path.join('src', project_name, 'manage.py')
replace_or_append(
path, ' os.environ.setdefault',
' os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.conf.%s")\n' % (project_name, target)
)
def append_settings_activate(project, target, env):
if os.name == 'posix':
path = '%s/bin/activate' % env
replace_or_append(path, 'export DJANGO_SETTINGS_MODULE=',
'export DJANGO_SETTINGS_MODULE=\'%s.conf.%s\'\n' %
(project, target))
elif os.name == 'nt':
path = '%s\\Scripts\\activate.bat' % env
replace_or_append(path, 'set DJANGO_SETTINGS_MODULE=',
'set DJANGO_SETTINGS_MODULE=%s.conf.%s\n' %
(project, target))
path = '%s\\Scripts\\deactivate.bat' % env
replace_or_append(path, 'set DJANGO_SETTINGS_MODULE=',
'set DJANGO_SETTINGS_MODULE=\n')
def main():
virtualenv = args.env
if not hasattr(sys, 'real_prefix'):
print('\n== Creating virtual environment ==\n')
call('virtualenv {0} --python=python3 --prompt="({1}-{2}) "'.format(
virtualenv, args.project, args.target
), shell=True)
print('\n== Set "%s.conf.%s" as default settings ==\n' % (args.project, args.target))
append_settings_activate(args.project, args.target, args.env)
if os.name == 'posix':
# Make manage.py executable
st = os.stat('src/manage.py')
os.chmod('src/manage.py', st.st_mode | stat.S_IEXEC)
django_admin_symlink = os.path.join(virtualenv, 'bin', 'django')
if not os.path.exists(django_admin_symlink):
os.symlink('../../src/manage.py', django_admin_symlink)
# Disabled: we have a separate wsgi script per target for now
# replace_wsgi_settings(args.target)
print('\n== Compiling base requirements ==\n')
if os.name == 'posix':
bin_dir = 'bin'
elif os.name == 'nt':
bin_dir = 'Scripts'
pip_compile = os.path.join(virtualenv, bin_dir, 'pip-compile')
os.chdir('requirements')
call('{0} main.in'.format(pip_compile), shell=True)
os.chdir('..')
print('\n== Installing %s requirements ==\n' % args.target)
if os.name == 'posix':
pip_path = os.path.join(virtualenv, 'bin', 'pip')
cmd_tpl = '{pip} install -r requirements/{target}.txt'
elif os.name == 'nt':
pip_path = os.path.join(virtualenv, 'Scripts', 'pip')
cmd_tpl = '{pip} install -r requirements\\{target}.txt'
call(cmd_tpl.format(pip=pip_path, target=args.target), shell=True)
if __name__ == '__main__':
main()
sys.exit(0)