This repository was 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
/
Copy pathbootstrap_py2.py
106 lines (92 loc) · 4.09 KB
/
bootstrap_py2.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
#!/usr/bin/env python
# bootstrap.py
# Bootstrap and setup a virtualenv with the specified requirements.txt
import os
import stat
import sys
import shutil
from subprocess import call
import argparse
from tempfile import mkstemp
from shutil import move
description = """
Set up my development environment for me!
"""
project_name = 'modelbouwdag'
parser = argparse.ArgumentParser(description=description)
parser.add_argument('target', choices=['production','staging','test','development'],
help='production/staging/development')
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.settings_%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.settings_%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.settings_%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.settings_%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} --prompt="({1}-{2}) "'.format(virtualenv,
args.project,
args.target),
shell=True)
print('\n== Set "%s.conf.settings_%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== Installing %s requirements ==\n' % args.target)
if os.name == 'posix':
call(os.path.join(virtualenv, 'bin', 'pip') + ' install --upgrade -r requirements/%s.txt' % args.target, shell=True)
elif os.name == 'nt':
call(os.path.join(virtualenv, 'Scripts', 'pip') + ' install --upgrade -r requirements\\%s.txt' % args.target, shell=True)
if __name__ == '__main__':
main()
sys.exit(0)