Project

General

Profile

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