-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.py
214 lines (192 loc) · 5.92 KB
/
build.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
from pybuilder.core import use_plugin, task, init
import subprocess
import sys
import os
import json
import builtins
use_plugin('python.core')
use_plugin('python.frosted')
config = {}
dir_path = os.path.dirname(os.path.realpath(__file__))
__spot = ''
def check_db_exists():
@init
def initialize(project):
__spot = project.get_property(t)
# check if ufree database exists
print('Checking DB for required tables...')
if not config['database']['password']:
print('Password not found. Using default')
arg_array = [
'psql',
'-U',
config['database']['user'],
os.environ['DB_NAME']
]
else:
arg_array = [
'psql',
'-U',
config['database']['user'],
os.environ['DB_NAME'],
'-W',
config['database']['password']
]
print('Using arguments:', arg_array)
db_exist = subprocess.Popen(
arg_array,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
out = db_exist.stdout.read()
print('Start script output:', out)
print('here')
# check output for 'does not exist...' string
check_string = str.encode('FATAL: database \"' + os.environ['DB_NAME'] + '\" does not exist')
if check_string in out:
# initialize cluster if found
print('Database not found. creating...')
create_db = subprocess.Popen(
['createdb', '-U', config['database']['user'], os.environ['DB_NAME']],
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
out, err = create_db.communicate()
print(out, err)
print('Check complete')
def check_db_cluster():
# get status of the /database directory
db_status = subprocess.Popen(
['pg_ctl', 'status', '-D', './database'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
out, err = db_status.communicate()
print(out, err)
# check output for 'is not database...' message
check_string = b'is not a database cluster'
if check_string in out or check_string in err:
# initialize cluster if found
print('Database cluster not found. Initializing...')
create_db = subprocess.Popen(
['pg_ctl', '-D', './database', 'initdb'],
stdout=subprocess.PIPE
)
print(create_db.communicate())
if create_db.returncode > 0:
sys.exit()
def start_db_server():
print('Starting database...')
check_db_cluster()
port_arg = '-p ' + str(config['database']['port'])
print('using port: ' + str(config['database']['port']))
start_db = subprocess.Popen(
[
'pg_ctl',
'-o',
'"' + port_arg + '"',
'-D',
'./database',
'-l',
'./db-logfile',
'start'
],
shell=True,
stdin=subprocess.PIPE
)
print('Start-up message:', start_db.communicate())
if start_db.returncode > 0:
print('return code > 0. exiting.')
sys.exit()
def stop_db():
proc = subprocess.Popen(
['pg_ctl', 'stop'],
stdin=subprocess.PIPE
)
print(proc.communicate())
if proc.returncode > 0:
sys.exit()
def build_client():
prefix = os.path.join(dir_path, 'client')
print(os.environ['PATH'])
proc = subprocess.Popen(
['npm', '--prefix', prefix, 'run', 'build'],
shell=True,
stdout=subprocess.PIPE
)
print(proc.communicate())
if proc.returncode > 0:
sys.exit()
def start_app_server():
print('starting the application server...')
main_path = os.path.join(dir_path, 'main.py')
print('starting ' + main_path)
os.system('python ' + main_path)
subprocess.call(
['python', main_path],
shell=True,
stdin=subprocess.PIPE
)
print(proc.communicate())
if proc.returncode > 0:
sys.exit()
def load_config():
global config
if os.environ['ENV'] != 'production':
print('Loading dev configuration')
with open('build_config_dev.json') as json_data_file:
config = json.load(json_data_file)
else:
print('Loading production configuration')
with open('build_config_prod.json') as json_data_file:
config = json.load(json_data_file)
# populate environment vars
os.environ['TEST_DB_FAIL'] = 'False'
os.environ['DAOS_PACKAGE'] = config['daoPackage']
os.environ['FILTERS_PACKAGE'] = config['filterPackage']
os.environ['SERVICES_PACKAGE'] = config['daoPackage']
os.environ['VALIDATORS_PACKAGE'] = config['validatorPackage']
os.environ['PSYCOPG2_PACKAGE'] = config['psycopg2Package']
os.environ['DB_NAME'] = config['database']['name']
@task(description='pyb -P t="eventDao_test.py"')
def spot():
os.environ['ENV'] = 'test'
load_config()
proc = subprocess.Popen(
['nosetests', '-v', '--tests=test\\{0}'.format(__spot)],
shell=True,
stdout=subprocess.PIPE
)
print(proc.communicate())
@task(description='Uses Nose to run all unit tests')
def test():
os.environ['ENV'] = 'test'
os.environ['TEST_DB_FAIL'] = 'False'
load_config()
#os.environ['DB_NAME'] = 'ufree_test'
proc = subprocess.Popen(
['nosetests', '-v'],
shell=True,
stdout=subprocess.PIPE
)
print(proc.communicate())
@task(description='Compiles client-side code')
def build():
print('Building client side code...')
build_client()
@task(description='Starts the database and app server in production mode')
def start():
os.environ['ENV'] = 'production'
load_config()
start_db_server()
check_db_exists()
start_app_server()
@task(description='Starts the database and app server in development mode')
def start_dev():
os.environ['ENV'] = 'development'
load_config()
start_db_server()
check_db_exists()
start_app_server()