Project

General

Profile

1 784 aaronmk
##### Vars/functions
2
3 244 aaronmk
# Make
4 247 aaronmk
SHELL := /bin/bash
5 625 aaronmk
pathParts = $(shell path="$(1)"; echo "$${path%%/*}" "$${path\#*/}")
6 739 aaronmk
topDir = $(word 1,$(pathParts))
7
subPath = $(word 2,$(pathParts))
8 625 aaronmk
subMake = $(MAKE) $(call subPath,$@) --directory=$(call topDir,$@)
9 244 aaronmk
10 245 aaronmk
# OS
11 247 aaronmk
os := $(shell uname)
12 245 aaronmk
forOs = $(patsubst %,%-$(filter Linux Darwin,$(os)),$(1))
13
14 2034 aaronmk
# System
15
date = $(shell date +"%Y-%m-%d-%H-%M-%S")
16
17 241 aaronmk
# Terminal
18 247 aaronmk
esc := '['
19
reset := $(esc)'0m'
20
emph := $(esc)'7m '
21
endEmph := ' '$(reset)
22 245 aaronmk
23
# User interaction
24
25 1745 aaronmk
done = echo; echo $(emph)"Finished $@"$(endEmph); echo
26 245 aaronmk
27 247 aaronmk
wait := read -p $(emph)'Press ENTER to continue:'$(endEmph) REPLY
28 245 aaronmk
29 2090 aaronmk
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 235 aaronmk
33 335 aaronmk
# File editing
34
sudoAppend = echo $$'$(2)'|sudo tee -a $(1) >/dev/null
35
36 2091 aaronmk
# DB
37
mkSchemaCmd = 'CREATE SCHEMA $(1);'
38
rmSchemaCmd = 'DROP SCHEMA IF EXISTS $(1) CASCADE;'
39
40 784 aaronmk
##### Environment
41
42 225 aaronmk
export PGOPTIONS = --client-min-messages=WARNING
43
44 784 aaronmk
##### General targets
45 225 aaronmk
46 414 aaronmk
all:
47 228 aaronmk
48 1260 aaronmk
.SUFFIXES: # turn off built-in suffix rules
49
.SECONDARY: # don't automatically delete intermediate files
50 1354 aaronmk
.DELETE_ON_ERROR: # delete target if recipe fails
51 225 aaronmk
52 383 aaronmk
_always:
53
.PHONY: _always
54 202 aaronmk
55 1274 aaronmk
remake: _always clean
56
	$(MAKE)
57
# re-run make so that cache of existing files is reset
58
59 1288 aaronmk
%/remake: _always
60
	$(MAKE) $(@D)/clean
61
	$(MAKE) $(@D)/
62
# re-run make so that cache of existing files is reset
63
64 1813 aaronmk
%-remake: _always
65
	rm -f $*
66
	$(MAKE) $*
67
# re-run make so that cache of existing files is reset
68
69 2981 aaronmk
%/reinstall: _always %/uninstall %/install ;
70
71 784 aaronmk
##### Installation
72 241 aaronmk
73 3377 aaronmk
install: _always core mysql inputs/download inputs/install test
74 241 aaronmk
	@$(done)
75 202 aaronmk
76 418 aaronmk
uninstall: _always inputs/uninstall rm_mysql rm_core ;
77 241 aaronmk
78 418 aaronmk
reinstall: _always uninstall install ;
79 225 aaronmk
80 784 aaronmk
##### Core: VegBIEN DB and dependencies
81 202 aaronmk
82 1746 aaronmk
core: _always $(call forOs,python php postgres) db
83 241 aaronmk
	@$(done)
84 235 aaronmk
85 1746 aaronmk
rm_core: _always rm_db ;
86 241 aaronmk
87 418 aaronmk
reinstall_core: _always rm_core core ;
88 241 aaronmk
89 784 aaronmk
##### Python
90
91 383 aaronmk
python-Linux: _always
92 2618 aaronmk
	-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 3156 aaronmk
	-sudo pip install ordereddict
100 1845 aaronmk
	-sudo pip install pp
101 284 aaronmk
102 383 aaronmk
python-Darwin: _always
103 284 aaronmk
	@echo $(emph)'Installing python-dateutil on Mac OS X:'$(endEmph)
104
	@echo 'Download it: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
105
	@echo 'Extract the archive'
106
	@echo 'Change to the directory of the extracted files'
107
	@echo 'Run python setup.py build'
108
	@echo 'Run sudo python setup.py install'
109
	@echo "Run python -c 'import dateutil'"
110
	@$(wait)
111 3156 aaronmk
	@echo $(emph)'Installing pip on Mac OS X:'$(endEmph)
112
	@echo 'Follow the instructions at http://www.pip-installer.org/en/latest/installing.html'
113
	@$(wait)
114 284 aaronmk
115 1599 aaronmk
##### PHP
116
117
php-Linux: _always
118
	-sudo apt-get install php-http-request
119
120
php-Darwin: _always
121 1600 aaronmk
	@echo $(emph)'Installing PHP PEAR and HTTP_Request on Mac OS X:'$(endEmph)
122
	@echo 'Download http://pear.php.net/go-pear.phar'
123
	@echo 'Change to the directory of the downloaded file'
124
	@echo 'Run php -d detect_unicode=0 go-pear.phar'
125
	@echo 'Whenever prompted, press Enter to select default options'
126
	@echo 'Add $$HOME/pear/bin to your $$PATH'
127
	@echo 'Add $(HOME)/pear/share/pear to your php.ini include_path'
128 1665 aaronmk
	@echo 'If needed, set $$PHPRC to your php.ini'
129 1600 aaronmk
	@echo "Run pear install HTTP_Request"
130
	@echo 'To run a php command: php -c <php.ini-path> ...'
131 1599 aaronmk
	@$(wait)
132
133 784 aaronmk
##### PostgreSQL
134
135 1970 aaronmk
editPhppgadminApacheConf = echo $$'1\n,s/deny from all/allow from all/\nwq'|\
136 336 aaronmk
sudo ed --loose-exit-status --verbose /etc/phppgadmin/apache.conf
137 334 aaronmk
138
apacheConf := /etc/apache2/apache2.conf
139
apacheConfPhppgadminLine := Include /etc/phppgadmin/apache.conf
140
editApacheConfForPhppgadmin =\
141
$(if $(shell grep -F "$(apacheConfPhppgadminLine)" $(apacheConf)),,\
142
$(call sudoAppend,$(apacheConf),\n$(apacheConfPhppgadminLine)))
143
144 335 aaronmk
nonApacheOnPort80 = $(shell sudo lsof -i :80|tail -n +2|grep -v -F apache2)
145 336 aaronmk
editApachePortsConf = echo $$'1\n,s/\\b80\\b/8080/g\nwq'|\
146
sudo ed --loose-exit-status --verbose /etc/apache2/ports.conf
147 334 aaronmk
148 383 aaronmk
postgres-Linux: _always
149 2618 aaronmk
	-sudo apt-get install postgresql
150 2619 aaronmk
	-sudo apt-get install postgresql-plpython-"$$(env minor=1 bin/pg_version)"
151 2618 aaronmk
	-sudo apt-get install libpq-dev
152
	-sudo apt-get install phppgadmin
153 336 aaronmk
	$(editPhppgadminApacheConf)
154 334 aaronmk
	$(editApacheConfForPhppgadmin)
155 336 aaronmk
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
156 334 aaronmk
	-sudo apache2ctl restart
157 284 aaronmk
	-sudo pip install psycopg2
158 2619 aaronmk
# run pg_version inline because it needs postgresql to be installed first
159 334 aaronmk
# ignore errors if conf files already edited
160 235 aaronmk
161 383 aaronmk
postgres-Darwin: _always
162 254 aaronmk
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
163
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
164
	@echo 'Open the disk image'
165
	@echo 'Run the installer in it'
166
	@$(wait)
167 235 aaronmk
168 418 aaronmk
postgres-: _always ; # other OSes
169 241 aaronmk
170 247 aaronmk
psqlOpts := --set ON_ERROR_STOP=1 --quiet
171 1980 aaronmk
psqlAsAdmin := sudo -E -u postgres psql $(psqlOpts)
172
    # -E preserves env vars so PGOPTIONS is passed to psql
173 2624 aaronmk
psqlAsAdminVegbien := $(psqlAsAdmin) vegbien
174 1519 aaronmk
psqlAsBien := bin/psql_vegbien $(psqlOpts)
175 272 aaronmk
bienPassword := $(shell cat config/bien_password)
176 241 aaronmk
177 784 aaronmk
##### VegBIEN DB
178 241 aaronmk
179 1967 aaronmk
#### DB and bien user
180
181 2090 aaronmk
confirmRmDb = $(call confirm,WARNING: This will delete your entire VegBIEN\
182 3374 aaronmk
DB!,This includes all archived imports and staging tables.)
183 2089 aaronmk
184 2633 aaronmk
confirmRmPublicSchema = $(call confirm,WARNING: This will delete the current\
185
public schema of your VegBIEN DB!,To save it: make schemas/rotate)
186 2089 aaronmk
187 2091 aaronmk
rmPublicSchema := $(call rmSchemaCmd,public)
188 1967 aaronmk
189 2631 aaronmk
db: mk_db rm_initial_public schemas/install _always ;
190 1967 aaronmk
191
mk_db: _always
192 1745 aaronmk
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
193 2037 aaronmk
	-echo "CREATE DATABASE vegbien WITH OWNER bien TEMPLATE template1 \
194 1940 aaronmk
ENCODING 'UTF8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';"|$(psqlAsAdmin)
195 2627 aaronmk
	-bin/postgres_vegbien createlang plpythonu
196
# ignore errors if user/database/etc. exists
197 2091 aaronmk
198
rm_initial_public: _always
199 2089 aaronmk
	@$(confirmRmPublicSchema)
200 2624 aaronmk
	echo $(rmPublicSchema)|$(psqlAsAdminVegbien)
201 1967 aaronmk
# drop public schema *as admin* because it starts out owned by postgres
202 241 aaronmk
203 383 aaronmk
rm_db: _always
204 1748 aaronmk
	@$(confirmRmDb)
205
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
206
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
207 241 aaronmk
208 418 aaronmk
reinstall_db: _always rm_db db ;
209 241 aaronmk
210 2631 aaronmk
#### Schemas
211 1967 aaronmk
212 2980 aaronmk
schemas := temp functions py_functions public
213 2631 aaronmk
schemas/install: $(schemas:%=schemas/%/install) ;
214
215 2980 aaronmk
schemasReversed := public py_functions functions temp
216 2631 aaronmk
schemas/uninstall: $(schemasReversed:%=schemas/%/uninstall) ;
217
218
### public
219
220
schemas/public/install: schemas/vegbien.sql _always
221 2091 aaronmk
	-echo $(call mkSchemaCmd,public)|$(psqlAsBien)
222 2036 aaronmk
	<$< $(psqlAsBien)
223 1967 aaronmk
# ignore errors if schema exists
224
# public schema will be owned by bien
225
226 2631 aaronmk
schemas/public/uninstall: _always
227 2089 aaronmk
	@$(confirmRmPublicSchema)
228 1967 aaronmk
	echo $(rmPublicSchema)|$(psqlAsBien)
229
230 2901 aaronmk
schemas/rotate: _always schemas/rotate-only schemas/public/install ;
231 2034 aaronmk
schemas/rotate-only: _always
232
	echo 'ALTER SCHEMA public RENAME TO "public.$(date)";'|$(psqlAsBien)
233
234 2631 aaronmk
### py_functions
235 2091 aaronmk
236 2631 aaronmk
schemas/py_functions/install: schemas/py_functions.sql _always
237 3376 aaronmk
	-<$< $(psqlAsAdminVegbien)
238
# ignore errors if schema exists
239 2631 aaronmk
240
### Others
241
242
schemas/%/install: schemas/%.sql _always
243 3376 aaronmk
	-<$< $(psqlAsBien)
244
# ignore errors if schema exists
245 2091 aaronmk
246 2631 aaronmk
schemas/%/uninstall: _always
247
	echo $(call rmSchemaCmd,$*)|$(psqlAsBien)
248 2624 aaronmk
249 3365 aaronmk
# Note: This can't be used for the current (unrotated) public schema because
250
# pg_dump doesn't back up the CREATE SCHEMA statement for it, assuming falsely
251
# that public already exists because it's in template1.
252 3371 aaronmk
schemas/%.backup:
253 3379 aaronmk
	"time" env data=1 bin/pg_dump_vegbien $* >$@
254 3362 aaronmk
255 3373 aaronmk
restoreBien = "time" bin/postgres_vegbien pg_restore --exit-on-error --verbose\
256
--dbname=vegbien
257
# pg_restore doesn't support PGDATABASE env var
258
259
schemas/%.backup/restore: schemas/%.backup _always
260
	$(restoreBien) $< &>$<.log
261
262 3249 aaronmk
schemas/%/rm_indexes: _always
263
	bin/pg_dump_vegbien $*|bin/mk_rm_indexes|$(psqlAsBien) --echo-all
264
265 2983 aaronmk
# Needed on Ubuntu 12.04 (also other Linuxes?) because %/reinstall is ignored.
266
schemas/temp/reinstall: _always schemas/temp/uninstall schemas/temp/install ;
267
268 784 aaronmk
##### MySQL
269 241 aaronmk
270 383 aaronmk
mysql: _always $(call forOs,mysql) mysql_user
271 241 aaronmk
	@$(done)
272
273 418 aaronmk
rm_mysql: _always rm_mysql_user ;
274 241 aaronmk
275 383 aaronmk
mysql-Linux: _always
276 244 aaronmk
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
277 238 aaronmk
	@$(wait)
278 237 aaronmk
	-sudo apt-get install mysql-server mysql-client python-mysqldb
279 235 aaronmk
280 383 aaronmk
mysql-Darwin: _always
281 244 aaronmk
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
282 235 aaronmk
	@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
283
	@echo 'Extract the archive'
284
	@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
285 284 aaronmk
	@echo 'Run ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
286
	@echo 'Run ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
287 235 aaronmk
	@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
288
	@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
289
	@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
290
	@echo 'Change to the directory of the extracted files'
291 284 aaronmk
	@echo 'Run python setup.py clean'
292
	@echo 'Run python setup.py build'
293 235 aaronmk
	@echo 'Run sudo python setup.py install'
294
	@echo "Run python -c 'import MySQLdb'"
295 238 aaronmk
	@$(wait)
296 235 aaronmk
297 383 aaronmk
mysql-: _always # other OSes
298 235 aaronmk
299 247 aaronmk
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
300 235 aaronmk
301 383 aaronmk
mysql_user: _always
302 241 aaronmk
	-$($@_cmd)
303 364 aaronmk
# ignore errors if user exists
304 244 aaronmk
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
305 245 aaronmk
'$(bienPassword)';"|$(mysqlAsRoot)
306 202 aaronmk
307 383 aaronmk
rm_mysql_user: _always
308 245 aaronmk
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
309 364 aaronmk
# ignore errors if user exists
310 202 aaronmk
311 784 aaronmk
##### Datasources
312 202 aaronmk
313 418 aaronmk
inputs: _always inputs/all ;
314 245 aaronmk
315 1542 aaronmk
import: _always import-msg inputs/import ;
316
import-msg: _always
317 1550 aaronmk
	@echo $(emph)"To import all inputs at once:"$(endEmph) . bin/import_all
318 1542 aaronmk
	@echo
319
	@$(wait)
320 250 aaronmk
321 1458 aaronmk
verify: _always inputs/verify
322 1241 aaronmk
	@$(done)
323 386 aaronmk
324 1458 aaronmk
test: _always inputs/test
325 397 aaronmk
	@$(done)
326 1458 aaronmk
327
##### Testing
328
329 1743 aaronmk
test-all: _always remake test missing_mappings
330 1458 aaronmk
	@$(done)
331 1967 aaronmk
332
##### Subdir forwarding
333
334
# Must come last so overridden targets are not forwarded
335
336
define subdirTargets
337
$(subdir): _always
338
	+$$(subMake)
339
340
$(subdir)%: _always
341
	+$$(subMake)
342
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
343
344
$(subdir)reinstall: _always $(subdir)uninstall $(subdir)install ;
345
endef
346
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
347
348
Makefile: ;
349
350
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
351 1999 aaronmk
352
#### Maps validation
353
354
# Must come after Subdir forwarding to avoid infinite recursion, for some reason
355
356
missing_mappings: _always missing_join_mappings missing_input_mappings ;
357
358
missing_%_mappings: _always # stem is one of join|input
359
	@echo $(emph)"Missing $* mappings:"$(endEmph)
360
	@+$(MAKE) inputs/$@|grep -v -e ' Missing '\
361
$(if $(filter join,$*), -e '^make '|sort|uniq)