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 418 aaronmk
uninstall: _always inputs/uninstall 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 284 aaronmk
	@echo $(emph)'Installing python-dateutil on Mac OS X:'$(endEmph)
93
	@echo 'Download it: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
94
	@echo 'Extract the archive'
95
	@echo 'Change to the directory of the extracted files'
96
	@echo 'Run python setup.py build'
97
	@echo 'Run sudo python setup.py install'
98
	@echo "Run python -c 'import dateutil'"
99
	@$(wait)
100 3156 aaronmk
	@echo $(emph)'Installing pip on Mac OS X:'$(endEmph)
101
	@echo 'Follow the instructions at http://www.pip-installer.org/en/latest/installing.html'
102
	@$(wait)
103 284 aaronmk
104 1599 aaronmk
##### PHP
105
106
php-Linux: _always
107
	-sudo apt-get install php-http-request
108
109
php-Darwin: _always
110 1600 aaronmk
	@echo $(emph)'Installing PHP PEAR and HTTP_Request on Mac OS X:'$(endEmph)
111
	@echo 'Download http://pear.php.net/go-pear.phar'
112
	@echo 'Change to the directory of the downloaded file'
113
	@echo 'Run php -d detect_unicode=0 go-pear.phar'
114
	@echo 'Whenever prompted, press Enter to select default options'
115
	@echo 'Add $$HOME/pear/bin to your $$PATH'
116
	@echo 'Add $(HOME)/pear/share/pear to your php.ini include_path'
117 1665 aaronmk
	@echo 'If needed, set $$PHPRC to your php.ini'
118 1600 aaronmk
	@echo "Run pear install HTTP_Request"
119
	@echo 'To run a php command: php -c <php.ini-path> ...'
120 1599 aaronmk
	@$(wait)
121
122 784 aaronmk
##### PostgreSQL
123
124 1970 aaronmk
editPhppgadminApacheConf = echo $$'1\n,s/deny from all/allow from all/\nwq'|\
125 336 aaronmk
sudo ed --loose-exit-status --verbose /etc/phppgadmin/apache.conf
126 334 aaronmk
127
apacheConf := /etc/apache2/apache2.conf
128
apacheConfPhppgadminLine := Include /etc/phppgadmin/apache.conf
129
editApacheConfForPhppgadmin =\
130
$(if $(shell grep -F "$(apacheConfPhppgadminLine)" $(apacheConf)),,\
131
$(call sudoAppend,$(apacheConf),\n$(apacheConfPhppgadminLine)))
132
133 335 aaronmk
nonApacheOnPort80 = $(shell sudo lsof -i :80|tail -n +2|grep -v -F apache2)
134 336 aaronmk
editApachePortsConf = echo $$'1\n,s/\\b80\\b/8080/g\nwq'|\
135
sudo ed --loose-exit-status --verbose /etc/apache2/ports.conf
136 334 aaronmk
137 4752 aaronmk
pgVersion = "$$(env minor=1 bin/pg_version)"
138
139 383 aaronmk
postgres-Linux: _always
140 2618 aaronmk
	-sudo apt-get install postgresql
141 4752 aaronmk
	-sudo apt-get install postgresql-plpython-$(pgVersion)
142 2618 aaronmk
	-sudo apt-get install libpq-dev
143 4752 aaronmk
	-sudo apt-get install postgresql-$(pgVersion)-postgis
144 2618 aaronmk
	-sudo apt-get install phppgadmin
145 336 aaronmk
	$(editPhppgadminApacheConf)
146 334 aaronmk
	$(editApacheConfForPhppgadmin)
147 336 aaronmk
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
148 334 aaronmk
	-sudo apache2ctl restart
149 284 aaronmk
	-sudo pip install psycopg2
150 2619 aaronmk
# run pg_version inline because it needs postgresql to be installed first
151 334 aaronmk
# ignore errors if conf files already edited
152 235 aaronmk
153 383 aaronmk
postgres-Darwin: _always
154 254 aaronmk
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
155
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
156
	@echo 'Open the disk image'
157
	@echo 'Run the installer in it'
158
	@$(wait)
159 235 aaronmk
160 418 aaronmk
postgres-: _always ; # other OSes
161 241 aaronmk
162 247 aaronmk
psqlOpts := --set ON_ERROR_STOP=1 --quiet
163 1980 aaronmk
psqlAsAdmin := sudo -E -u postgres psql $(psqlOpts)
164
    # -E preserves env vars so PGOPTIONS is passed to psql
165 2624 aaronmk
psqlAsAdminVegbien := $(psqlAsAdmin) vegbien
166 1519 aaronmk
psqlAsBien := bin/psql_vegbien $(psqlOpts)
167 272 aaronmk
bienPassword := $(shell cat config/bien_password)
168 241 aaronmk
169 784 aaronmk
##### VegBIEN DB
170 241 aaronmk
171 1967 aaronmk
#### DB and bien user
172
173 2090 aaronmk
confirmRmDb = $(call confirm,WARNING: This will delete your entire VegBIEN\
174 3374 aaronmk
DB!,This includes all archived imports and staging tables.)
175 2089 aaronmk
176 2633 aaronmk
confirmRmPublicSchema = $(call confirm,WARNING: This will delete the current\
177
public schema of your VegBIEN DB!,To save it: make schemas/rotate)
178 2089 aaronmk
179 2091 aaronmk
rmPublicSchema := $(call rmSchemaCmd,public)
180 1967 aaronmk
181 6102 aaronmk
db: mk_db rm_initial_public _always ;
182 1967 aaronmk
183
mk_db: _always
184 1745 aaronmk
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
185 2037 aaronmk
	-echo "CREATE DATABASE vegbien WITH OWNER bien TEMPLATE template1 \
186 1940 aaronmk
ENCODING 'UTF8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';"|$(psqlAsAdmin)
187 2627 aaronmk
	-bin/postgres_vegbien createlang plpythonu
188
# ignore errors if user/database/etc. exists
189 2091 aaronmk
190
rm_initial_public: _always
191 2089 aaronmk
	@$(confirmRmPublicSchema)
192 2624 aaronmk
	echo $(rmPublicSchema)|$(psqlAsAdminVegbien)
193 1967 aaronmk
# drop public schema *as admin* because it starts out owned by postgres
194 241 aaronmk
195 383 aaronmk
rm_db: _always
196 1748 aaronmk
	@$(confirmRmDb)
197
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
198
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
199 241 aaronmk
200 418 aaronmk
reinstall_db: _always rm_db db ;
201 241 aaronmk
202 2631 aaronmk
#### Schemas
203 1967 aaronmk
204 2980 aaronmk
schemas := temp functions py_functions public
205 2631 aaronmk
schemas/install: $(schemas:%=schemas/%/install) ;
206
207 2980 aaronmk
schemasReversed := public py_functions functions temp
208 2631 aaronmk
schemas/uninstall: $(schemasReversed:%=schemas/%/uninstall) ;
209
210 6098 aaronmk
psqlNoSearchPath := env no_search_path=1 $(psqlAsBien)
211
212 2631 aaronmk
### public
213
214
schemas/public/install: schemas/vegbien.sql _always
215 6098 aaronmk
	-echo $(call mkSchemaCmd,public)|$(psqlNoSearchPath)
216 2036 aaronmk
	<$< $(psqlAsBien)
217 1967 aaronmk
# ignore errors if schema exists
218
# public schema will be owned by bien
219 6088 aaronmk
# don't include public in the search_path
220 1967 aaronmk
221 2631 aaronmk
schemas/public/uninstall: _always
222 2089 aaronmk
	@$(confirmRmPublicSchema)
223 6098 aaronmk
	echo $(rmPublicSchema)|$(psqlNoSearchPath)
224 1967 aaronmk
225 5678 aaronmk
schemas/rename/%: _always
226 6098 aaronmk
	echo 'ALTER SCHEMA public RENAME TO "$*";'|$(psqlNoSearchPath)
227 5678 aaronmk
	$(MAKE) schemas/public/install
228
229 5679 aaronmk
schemas/rotate: _always schemas/rename/public.$(version) ;
230 2034 aaronmk
231 2631 aaronmk
### py_functions
232 2091 aaronmk
233 2631 aaronmk
schemas/py_functions/install: schemas/py_functions.sql _always
234 6098 aaronmk
	-<$< env public= $(psqlAsAdminVegbien)
235 3376 aaronmk
# ignore errors if schema exists
236 2631 aaronmk
237
### Others
238
239
schemas/%/install: schemas/%.sql _always
240 6098 aaronmk
	-<$< $(psqlNoSearchPath)
241 3376 aaronmk
# ignore errors if schema exists
242 2091 aaronmk
243 3393 aaronmk
schemas/%/uninstall: _always
244 6098 aaronmk
	echo $(call rmSchemaCmd,$*)|$(psqlNoSearchPath)
245 2624 aaronmk
246 2983 aaronmk
# Needed on Ubuntu 12.04 (also other Linuxes?) because %/reinstall is ignored.
247
schemas/temp/reinstall: _always schemas/temp/uninstall schemas/temp/install ;
248
249 784 aaronmk
##### MySQL
250 241 aaronmk
251 383 aaronmk
mysql: _always $(call forOs,mysql) mysql_user
252 241 aaronmk
	@$(done)
253
254 418 aaronmk
rm_mysql: _always rm_mysql_user ;
255 241 aaronmk
256 383 aaronmk
mysql-Linux: _always
257 244 aaronmk
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
258 238 aaronmk
	@$(wait)
259 237 aaronmk
	-sudo apt-get install mysql-server mysql-client python-mysqldb
260 235 aaronmk
261 383 aaronmk
mysql-Darwin: _always
262 244 aaronmk
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
263 235 aaronmk
	@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
264
	@echo 'Extract the archive'
265
	@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
266 284 aaronmk
	@echo 'Run ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
267
	@echo 'Run ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
268 235 aaronmk
	@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
269
	@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
270
	@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
271
	@echo 'Change to the directory of the extracted files'
272 284 aaronmk
	@echo 'Run python setup.py clean'
273
	@echo 'Run python setup.py build'
274 235 aaronmk
	@echo 'Run sudo python setup.py install'
275
	@echo "Run python -c 'import MySQLdb'"
276 238 aaronmk
	@$(wait)
277 235 aaronmk
278 383 aaronmk
mysql-: _always # other OSes
279 235 aaronmk
280 247 aaronmk
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
281 235 aaronmk
282 383 aaronmk
mysql_user: _always
283 241 aaronmk
	-$($@_cmd)
284 364 aaronmk
# ignore errors if user exists
285 244 aaronmk
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
286 245 aaronmk
'$(bienPassword)';"|$(mysqlAsRoot)
287 202 aaronmk
288 383 aaronmk
rm_mysql_user: _always
289 245 aaronmk
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
290 364 aaronmk
# ignore errors if user exists
291 202 aaronmk
292 784 aaronmk
##### Datasources
293 202 aaronmk
294 418 aaronmk
inputs: _always inputs/all ;
295 245 aaronmk
296 1542 aaronmk
import: _always import-msg inputs/import ;
297
import-msg: _always
298 1550 aaronmk
	@echo $(emph)"To import all inputs at once:"$(endEmph) . bin/import_all
299 1542 aaronmk
	@echo
300 250 aaronmk
301 1458 aaronmk
verify: _always inputs/verify
302 1241 aaronmk
	@$(done)
303 386 aaronmk
304 1458 aaronmk
test: _always inputs/test
305 397 aaronmk
	@$(done)
306 1458 aaronmk
307
##### Testing
308
309 1743 aaronmk
test-all: _always remake test missing_mappings
310 1458 aaronmk
	@$(done)
311 1967 aaronmk
312
##### Subdir forwarding
313
314
# Must come last so overridden targets are not forwarded
315
316
define subdirTargets
317
$(subdir): _always
318
	+$$(subMake)
319
320
$(subdir)%: _always
321
	+$$(subMake)
322
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
323
324
$(subdir)reinstall: _always $(subdir)uninstall $(subdir)install ;
325
endef
326
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
327
328
Makefile: ;
329
330
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
331 1999 aaronmk
332
#### Maps validation
333
334 4607 aaronmk
termsSubdirs := $(call no/,$(call wildcard/,inputs/*/))
335
336 3764 aaronmk
include lib/mappings.Makefile