Project

General

Profile

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