Project

General

Profile

1
##### Vars/functions
2

    
3
# Make
4
SHELL := /bin/bash
5
pathParts = $(shell path="$(1)"; echo "$${path%%/*}" "$${path\#*/}")
6
topDir = $(word 1,$(pathParts))
7
subPath = $(word 2,$(pathParts))
8
subMake = $(MAKE) $(call subPath,$@) --directory=$(call topDir,$@)
9

    
10
# OS
11
os := $(shell uname)
12
forOs = $(patsubst %,%-$(filter Linux Darwin,$(os)),$(1))
13

    
14
# System
15
date = $(shell date +"%Y-%m-%d-%H-%M-%S")
16

    
17
# Terminal
18
esc := '['
19
reset := $(esc)'0m'
20
emph := $(esc)'7m '
21
endEmph := ' '$(reset)
22

    
23
# User interaction
24

    
25
done = echo; echo $(emph)"Finished $@"$(endEmph); echo
26

    
27
wait := read -p $(emph)'Press ENTER to continue:'$(endEmph) REPLY
28

    
29
confirm = $(if $(shell read -p $(emph)"$(1)"$(endEmph)$$'$(if\
30
$(2),\n$(2))\nContinue? (y/n) ' REPLY; test "$$REPLY" = y && echo t),,\
31
$(error Aborting))
32

    
33
# File editing
34
sudoAppend = echo $$'$(2)'|sudo tee -a $(1) >/dev/null
35

    
36
# DB
37
mkSchemaCmd = 'CREATE SCHEMA $(1);'
38
rmSchemaCmd = 'DROP SCHEMA IF EXISTS $(1) CASCADE;'
39

    
40
##### Environment
41

    
42
export PGOPTIONS = --client-min-messages=WARNING
43

    
44
##### General targets
45

    
46
all:
47

    
48
.SUFFIXES: # turn off built-in suffix rules
49
.SECONDARY: # don't automatically delete intermediate files
50
.DELETE_ON_ERROR: # delete target if recipe fails
51

    
52
_always:
53
.PHONY: _always
54

    
55
remake: _always clean
56
	$(MAKE)
57
# re-run make so that cache of existing files is reset
58

    
59
%/remake: _always
60
	$(MAKE) $(@D)/clean
61
	$(MAKE) $(@D)/
62
# re-run make so that cache of existing files is reset
63

    
64
%-remake: _always
65
	rm -f $*
66
	$(MAKE) $*
67
# re-run make so that cache of existing files is reset
68

    
69
%/reinstall: _always %/uninstall %/install ;
70

    
71
##### Installation
72

    
73
install: _always core mysql inputs/install test
74
	@$(done)
75

    
76
uninstall: _always inputs/uninstall rm_mysql rm_core ;
77

    
78
reinstall: _always uninstall install ;
79

    
80
##### Core: VegBIEN DB and dependencies
81

    
82
core: _always $(call forOs,python php postgres) db
83
	@$(done)
84

    
85
rm_core: _always rm_db ;
86

    
87
reinstall_core: _always rm_core core ;
88

    
89
##### Python
90

    
91
python-Linux: _always
92
	-sudo apt-get install python
93
	-sudo apt-get install python-dev
94
	-sudo apt-get install python-dateutil
95
	-sudo apt-get install python-parallel
96
	-sudo apt-get install python-profiler
97
	-sudo apt-get install python-pip
98
	-sudo apt-get install pymetrics
99
	-sudo pip install pp
100

    
101
python-Darwin: _always
102
	@echo $(emph)'Installing python-dateutil on Mac OS X:'$(endEmph)
103
	@echo 'Download it: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
104
	@echo 'Extract the archive'
105
	@echo 'Change to the directory of the extracted files'
106
	@echo 'Run python setup.py build'
107
	@echo 'Run sudo python setup.py install'
108
	@echo "Run python -c 'import dateutil'"
109
	@$(wait)
110

    
111
##### PHP
112

    
113
php-Linux: _always
114
	-sudo apt-get install php-http-request
115

    
116
php-Darwin: _always
117
	@echo $(emph)'Installing PHP PEAR and HTTP_Request on Mac OS X:'$(endEmph)
118
	@echo 'Download http://pear.php.net/go-pear.phar'
119
	@echo 'Change to the directory of the downloaded file'
120
	@echo 'Run php -d detect_unicode=0 go-pear.phar'
121
	@echo 'Whenever prompted, press Enter to select default options'
122
	@echo 'Add $$HOME/pear/bin to your $$PATH'
123
	@echo 'Add $(HOME)/pear/share/pear to your php.ini include_path'
124
	@echo 'If needed, set $$PHPRC to your php.ini'
125
	@echo "Run pear install HTTP_Request"
126
	@echo 'To run a php command: php -c <php.ini-path> ...'
127
	@$(wait)
128

    
129
##### PostgreSQL
130

    
131
editPhppgadminApacheConf = echo $$'1\n,s/deny from all/allow from all/\nwq'|\
132
sudo ed --loose-exit-status --verbose /etc/phppgadmin/apache.conf
133

    
134
apacheConf := /etc/apache2/apache2.conf
135
apacheConfPhppgadminLine := Include /etc/phppgadmin/apache.conf
136
editApacheConfForPhppgadmin =\
137
$(if $(shell grep -F "$(apacheConfPhppgadminLine)" $(apacheConf)),,\
138
$(call sudoAppend,$(apacheConf),\n$(apacheConfPhppgadminLine)))
139

    
140
nonApacheOnPort80 = $(shell sudo lsof -i :80|tail -n +2|grep -v -F apache2)
141
editApachePortsConf = echo $$'1\n,s/\\b80\\b/8080/g\nwq'|\
142
sudo ed --loose-exit-status --verbose /etc/apache2/ports.conf
143

    
144
postgres-Linux: _always
145
	-sudo apt-get install postgresql
146
	-sudo apt-get install postgresql-plpython-"$$(env minor=1 bin/pg_version)"
147
	-sudo apt-get install libpq-dev
148
	-sudo apt-get install phppgadmin
149
	$(editPhppgadminApacheConf)
150
	$(editApacheConfForPhppgadmin)
151
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
152
	-sudo apache2ctl restart
153
	-sudo pip install psycopg2
154
# run pg_version inline because it needs postgresql to be installed first
155
# ignore errors if conf files already edited
156

    
157
postgres-Darwin: _always
158
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
159
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
160
	@echo 'Open the disk image'
161
	@echo 'Run the installer in it'
162
	@$(wait)
163

    
164
postgres-: _always ; # other OSes
165

    
166
psqlOpts := --set ON_ERROR_STOP=1 --quiet
167
psqlAsAdmin := sudo -E -u postgres psql $(psqlOpts)
168
    # -E preserves env vars so PGOPTIONS is passed to psql
169
psqlAsAdminVegbien := $(psqlAsAdmin) vegbien
170
psqlAsBien := bin/psql_vegbien $(psqlOpts)
171
bienPassword := $(shell cat config/bien_password)
172

    
173
##### VegBIEN DB
174

    
175
#### DB and bien user
176

    
177
confirmRmDb = $(call confirm,WARNING: This will delete your entire VegBIEN\
178
DB!,This includes all public schema versions and staging tables.)
179

    
180
confirmRmPublicSchema = $(call confirm,WARNING: This will delete the current\
181
public schema of your VegBIEN DB!,To save it: make schemas/rotate)
182

    
183
rmPublicSchema := $(call rmSchemaCmd,public)
184

    
185
db: mk_db rm_initial_public schemas/install _always ;
186

    
187
mk_db: _always
188
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
189
	-echo "CREATE DATABASE vegbien WITH OWNER bien TEMPLATE template1 \
190
ENCODING 'UTF8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';"|$(psqlAsAdmin)
191
	-bin/postgres_vegbien createlang plpythonu
192
# ignore errors if user/database/etc. exists
193

    
194
rm_initial_public: _always
195
	@$(confirmRmPublicSchema)
196
	echo $(rmPublicSchema)|$(psqlAsAdminVegbien)
197
# drop public schema *as admin* because it starts out owned by postgres
198

    
199
rm_db: _always
200
	@$(confirmRmDb)
201
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
202
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
203

    
204
reinstall_db: _always rm_db db ;
205

    
206
#### Schemas
207

    
208
schemas := temp functions py_functions public
209
schemas/install: $(schemas:%=schemas/%/install) ;
210

    
211
schemasReversed := public py_functions functions temp
212
schemas/uninstall: $(schemasReversed:%=schemas/%/uninstall) ;
213

    
214
### public
215

    
216
schemas/public/install: schemas/vegbien.sql _always
217
	-echo $(call mkSchemaCmd,public)|$(psqlAsBien)
218
	<$< $(psqlAsBien)
219
# ignore errors if schema exists
220
# public schema will be owned by bien
221

    
222
schemas/public/uninstall: _always
223
	@$(confirmRmPublicSchema)
224
	echo $(rmPublicSchema)|$(psqlAsBien)
225

    
226
schemas/rotate: _always schemas/rotate-only schemas/public/install ;
227
schemas/rotate-only: _always
228
	echo 'ALTER SCHEMA public RENAME TO "public.$(date)";'|$(psqlAsBien)
229

    
230
### py_functions
231

    
232
schemas/py_functions/install: schemas/py_functions.sql _always
233
	<$< $(psqlAsAdminVegbien)
234

    
235
### Others
236

    
237
schemas/%/install: schemas/%.sql _always
238
	<$< $(psqlAsBien)
239

    
240
schemas/%/uninstall: _always
241
	echo $(call rmSchemaCmd,$*)|$(psqlAsBien)
242

    
243
# Needed on Ubuntu 12.04 (also other Linuxes?) because %/reinstall is ignored.
244
schemas/temp/reinstall: _always schemas/temp/uninstall schemas/temp/install ;
245

    
246
##### MySQL
247

    
248
mysql: _always $(call forOs,mysql) mysql_user
249
	@$(done)
250

    
251
rm_mysql: _always rm_mysql_user ;
252

    
253
mysql-Linux: _always
254
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
255
	@$(wait)
256
	-sudo apt-get install mysql-server mysql-client python-mysqldb
257

    
258
mysql-Darwin: _always
259
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
260
	@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
261
	@echo 'Extract the archive'
262
	@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
263
	@echo 'Run ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
264
	@echo 'Run ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
265
	@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
266
	@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
267
	@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
268
	@echo 'Change to the directory of the extracted files'
269
	@echo 'Run python setup.py clean'
270
	@echo 'Run python setup.py build'
271
	@echo 'Run sudo python setup.py install'
272
	@echo "Run python -c 'import MySQLdb'"
273
	@$(wait)
274

    
275
mysql-: _always # other OSes
276

    
277
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
278

    
279
mysql_user: _always
280
	-$($@_cmd)
281
# ignore errors if user exists
282
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
283
'$(bienPassword)';"|$(mysqlAsRoot)
284

    
285
rm_mysql_user: _always
286
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
287
# ignore errors if user exists
288

    
289
##### Datasources
290

    
291
inputs: _always inputs/all ;
292

    
293
import: _always import-msg inputs/import ;
294
import-msg: _always
295
	@echo $(emph)"To import all inputs at once:"$(endEmph) . bin/import_all
296
	@echo
297
	@$(wait)
298

    
299
verify: _always inputs/verify
300
	@$(done)
301

    
302
test: _always inputs/test
303
	@$(done)
304

    
305
##### Testing
306

    
307
test-all: _always remake test missing_mappings
308
	@$(done)
309

    
310
##### Subdir forwarding
311

    
312
# Must come last so overridden targets are not forwarded
313

    
314
define subdirTargets
315
$(subdir): _always
316
	+$$(subMake)
317

    
318
$(subdir)%: _always
319
	+$$(subMake)
320
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
321

    
322
$(subdir)reinstall: _always $(subdir)uninstall $(subdir)install ;
323
endef
324
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
325

    
326
Makefile: ;
327

    
328
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
329

    
330
#### Maps validation
331

    
332
# Must come after Subdir forwarding to avoid infinite recursion, for some reason
333

    
334
missing_mappings: _always missing_join_mappings missing_input_mappings ;
335

    
336
missing_%_mappings: _always # stem is one of join|input
337
	@echo $(emph)"Missing $* mappings:"$(endEmph)
338
	@+$(MAKE) inputs/$@|grep -v -e ' Missing '\
339
$(if $(filter join,$*), -e '^make '|sort|uniq)
(1-1/3)