13
13
Set up my development environment for me!
14
14
"""
15
15
16
- project_name = ' open_inwoner'
16
+ project_name = " open_inwoner"
17
17
18
18
parser = argparse .ArgumentParser (description = description )
19
- parser .add_argument ('target' , choices = ['production' , 'staging' , 'test' , 'jenkins' , 'dev' ],
20
- help = 'production/staging/test/jenkins/dev' )
21
- parser .add_argument ('--project' , default = project_name ,
22
- help = 'Name of the project in your src directory, "%s" by default' % project_name )
23
- parser .add_argument ('--init' , action = 'store_true' ,
24
- help = 'Initialize a fresh "startproject" by pinning the requirements using pip-tools compile. '
25
- 'Automatically done if requirements/base.txt does not yet exist.' )
26
- parser .add_argument ('--env' , default = 'env' ,
27
- help = 'Directory name for virtualenv, "env" by default' )
19
+ parser .add_argument (
20
+ "target" ,
21
+ choices = ["production" , "staging" , "test" , "jenkins" , "dev" ],
22
+ help = "production/staging/test/jenkins/dev" ,
23
+ )
24
+ parser .add_argument (
25
+ "--project" ,
26
+ default = project_name ,
27
+ help = 'Name of the project in your src directory, "%s" by default' % project_name ,
28
+ )
29
+ parser .add_argument (
30
+ "--init" ,
31
+ action = "store_true" ,
32
+ help = 'Initialize a fresh "startproject" by pinning the requirements using pip-tools compile. '
33
+ "Automatically done if requirements/base.txt does not yet exist." ,
34
+ )
35
+ parser .add_argument (
36
+ "--env" , default = "env" , help = 'Directory name for virtualenv, "env" by default'
37
+ )
28
38
29
39
args = parser .parse_args ()
30
40
31
41
32
42
def replace_or_append (file_path , search_val , replace_val ):
33
43
file_handle , abs_path = mkstemp ()
34
- new_file = open (abs_path , 'w' )
35
- old_file = open (file_path , 'r' )
44
+ new_file = open (abs_path , "w" )
45
+ old_file = open (file_path )
36
46
found = False
37
47
for line in old_file :
38
48
if line .startswith (search_val ):
@@ -51,89 +61,107 @@ def replace_or_append(file_path, search_val, replace_val):
51
61
52
62
53
63
def replace_wsgi_settings (target ):
54
- path = os .path .join (' src' , project_name , ' wsgi.py' )
64
+ path = os .path .join (" src" , project_name , " wsgi.py" )
55
65
replace_or_append (
56
- path , 'os.environ.setdefault' ,
57
- 'os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.conf.%s")\n ' % (project_name , target )
66
+ path ,
67
+ "os.environ.setdefault" ,
68
+ 'os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.conf.%s")\n '
69
+ % (project_name , target ),
58
70
)
59
71
60
72
61
73
def replace_manage_settings (target ):
62
- path = os .path .join (' src' , project_name , ' manage.py' )
74
+ path = os .path .join (" src" , project_name , " manage.py" )
63
75
replace_or_append (
64
- path , ' os.environ.setdefault' ,
65
- ' os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.conf.%s")\n ' % (project_name , target )
76
+ path ,
77
+ " os.environ.setdefault" ,
78
+ ' os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.conf.%s")\n '
79
+ % (project_name , target ),
66
80
)
67
81
68
82
69
83
def append_settings_activate (project , target , env ):
70
- if os .name == 'posix' :
71
- path = '%s/bin/activate' % env
72
- replace_or_append (path , 'export DJANGO_SETTINGS_MODULE=' ,
73
- 'export DJANGO_SETTINGS_MODULE=\' %s.conf.%s\' \n ' %
74
- (project , target ))
75
- elif os .name == 'nt' :
76
- path = '%s\\ Scripts\\ activate.bat' % env
77
- replace_or_append (path , 'set DJANGO_SETTINGS_MODULE=' ,
78
- 'set DJANGO_SETTINGS_MODULE=%s.conf.%s\n ' %
79
- (project , target ))
80
- path = '%s\\ Scripts\\ deactivate.bat' % env
81
- replace_or_append (path , 'set DJANGO_SETTINGS_MODULE=' ,
82
- 'set DJANGO_SETTINGS_MODULE=\n ' )
84
+ if os .name == "posix" :
85
+ path = "%s/bin/activate" % env
86
+ replace_or_append (
87
+ path ,
88
+ "export DJANGO_SETTINGS_MODULE=" ,
89
+ "export DJANGO_SETTINGS_MODULE='%s.conf.%s'\n " % (project , target ),
90
+ )
91
+ elif os .name == "nt" :
92
+ path = "%s\\ Scripts\\ activate.bat" % env
93
+ replace_or_append (
94
+ path ,
95
+ "set DJANGO_SETTINGS_MODULE=" ,
96
+ "set DJANGO_SETTINGS_MODULE=%s.conf.%s\n " % (project , target ),
97
+ )
98
+ path = "%s\\ Scripts\\ deactivate.bat" % env
99
+ replace_or_append (
100
+ path , "set DJANGO_SETTINGS_MODULE=" , "set DJANGO_SETTINGS_MODULE=\n "
101
+ )
83
102
84
103
85
104
def pip_compile_pin_requirements (virtualenv ):
86
- print (' \n == Compiling base requirements ==\n ' )
87
- if os .name == ' posix' :
88
- pip_path = os .path .join (virtualenv , ' bin' , ' pip' )
89
- elif os .name == 'nt' :
90
- pip_path = os .path .join (virtualenv , ' Scripts' , ' pip' )
91
- cmd_tpl = '{pip } install pip-tools' . format ( pip = pip_path )
105
+ print (" \n == Compiling base requirements ==\n " )
106
+ if os .name == " posix" :
107
+ pip_path = os .path .join (virtualenv , " bin" , " pip" )
108
+ elif os .name == "nt" :
109
+ pip_path = os .path .join (virtualenv , " Scripts" , " pip" )
110
+ cmd_tpl = f" { pip_path } install pip-tools"
92
111
call (cmd_tpl , shell = True )
93
- print ('Error: Run `. env/bin/activate && ./bin/compile_dependencies.sh` to ensure you have requirements/base.txt and requirements/dev.txt' )
94
- print ('After that rerun bootstrap.py' )
112
+ print (
113
+ "Error: Run `. env/bin/activate && ./bin/compile_dependencies.sh` to ensure you have requirements/base.txt and requirements/dev.txt"
114
+ )
115
+ print ("After that rerun bootstrap.py" )
95
116
sys .exit (1 )
96
117
118
+
97
119
def main ():
98
120
virtualenv = args .env
99
- if not hasattr (sys , 'real_prefix' ):
100
- print ('\n == Creating virtual environment ==\n ' )
101
- call ('virtualenv {0} --python=python3 --prompt="({1}-{2}) "' .format (
102
- virtualenv , args .project , args .target
103
- ), shell = True )
104
- print ('\n == Set "%s.conf.%s" as default settings ==\n ' % (args .project , args .target ))
121
+ if not hasattr (sys , "real_prefix" ):
122
+ print ("\n == Creating virtual environment ==\n " )
123
+ call (
124
+ f'virtualenv { virtualenv } --python=python3 --prompt="({ args .project } -{ args .target } ) "' ,
125
+ shell = True ,
126
+ )
127
+ print (
128
+ '\n == Set "%s.conf.%s" as default settings ==\n ' % (args .project , args .target )
129
+ )
105
130
append_settings_activate (args .project , args .target , args .env )
106
131
107
- if os .name == ' posix' :
132
+ if os .name == " posix" :
108
133
# Make manage.py executable
109
- st = os .stat (' src/manage.py' )
110
- os .chmod (' src/manage.py' , st .st_mode | stat .S_IEXEC )
111
- django_admin_symlink = os .path .join (virtualenv , ' bin' , ' django' )
134
+ st = os .stat (" src/manage.py" )
135
+ os .chmod (" src/manage.py" , st .st_mode | stat .S_IEXEC )
136
+ django_admin_symlink = os .path .join (virtualenv , " bin" , " django" )
112
137
if not os .path .exists (django_admin_symlink ):
113
- os .symlink (' ../../src/manage.py' , django_admin_symlink )
114
-
115
- print (' \n == Upgrading Pip ==\n ' )
116
- if os .name == ' posix' :
117
- pip_path = os .path .join (virtualenv , ' bin' , ' pip' )
118
- elif os .name == 'nt' :
119
- pip_path = os .path .join (virtualenv , ' Scripts' , ' pip' )
120
- cmd_tpl = '{pip } install --upgrade pip' . format ( pip = pip_path )
138
+ os .symlink (" ../../src/manage.py" , django_admin_symlink )
139
+
140
+ print (" \n == Upgrading Pip ==\n " )
141
+ if os .name == " posix" :
142
+ pip_path = os .path .join (virtualenv , " bin" , " pip" )
143
+ elif os .name == "nt" :
144
+ pip_path = os .path .join (virtualenv , " Scripts" , " pip" )
145
+ cmd_tpl = f" { pip_path } install --upgrade pip"
121
146
call (cmd_tpl , shell = True )
122
147
123
- if args .init \
124
- or not os .path .exists (os .path .join ('requirements' , 'base.txt' )) \
125
- or not os .path .exists (os .path .join ('requirements' , 'dev.txt' )):
148
+ if (
149
+ args .init
150
+ or not os .path .exists (os .path .join ("requirements" , "base.txt" ))
151
+ or not os .path .exists (os .path .join ("requirements" , "dev.txt" ))
152
+ ):
126
153
pip_compile_pin_requirements (virtualenv )
127
154
128
- print (' \n == Installing %s requirements ==\n ' % args .target )
129
- if os .name == ' posix' :
130
- os .environ [' TMPDIR' ] = ' /var/tmp/'
131
- pip_path = os .path .join (virtualenv , ' bin' , ' pip' )
132
- cmd_tpl = ' {pip} install -r requirements/{target}.txt'
133
- elif os .name == 'nt' :
134
- pip_path = os .path .join (virtualenv , ' Scripts' , ' pip' )
135
- cmd_tpl = ' {pip} install -r requirements\\ {target}.txt'
155
+ print (" \n == Installing %s requirements ==\n " % args .target )
156
+ if os .name == " posix" :
157
+ os .environ [" TMPDIR" ] = " /var/tmp/"
158
+ pip_path = os .path .join (virtualenv , " bin" , " pip" )
159
+ cmd_tpl = " {pip} install -r requirements/{target}.txt"
160
+ elif os .name == "nt" :
161
+ pip_path = os .path .join (virtualenv , " Scripts" , " pip" )
162
+ cmd_tpl = " {pip} install -r requirements\\ {target}.txt"
136
163
return call (cmd_tpl .format (pip = pip_path , target = args .target ), shell = True )
137
164
138
- if __name__ == '__main__' :
165
+
166
+ if __name__ == "__main__" :
139
167
sys .exit (main ())
0 commit comments