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 4607 aaronmk
# Paths
11
bin := bin
12
13 245 aaronmk
# OS
14 247 aaronmk
os := $(shell uname)
15 245 aaronmk
forOs = $(patsubst %,%-$(filter Linux Darwin,$(os)),$(1))
16
17 2034 aaronmk
# System
18
date = $(shell date +"%Y-%m-%d-%H-%M-%S")
19
20 241 aaronmk
# Terminal
21 247 aaronmk
esc := '['
22
reset := $(esc)'0m'
23
emph := $(esc)'7m '
24
endEmph := ' '$(reset)
25 245 aaronmk
26
# User interaction
27
28 1745 aaronmk
done = echo; echo $(emph)"Finished $@"$(endEmph); echo
29 245 aaronmk
30 247 aaronmk
wait := read -p $(emph)'Press ENTER to continue:'$(endEmph) REPLY
31 245 aaronmk
32 2090 aaronmk
confirm = $(if $(shell read -p $(emph)"$(1)"$(endEmph)$$'$(if\
33
$(2),\n$(2))\nContinue? (y/n) ' REPLY; test "$$REPLY" = y && echo t),,\
34
$(error Aborting))
35 235 aaronmk
36 335 aaronmk
# File editing
37
sudoAppend = echo $$'$(2)'|sudo tee -a $(1) >/dev/null
38
39 2091 aaronmk
# DB
40
mkSchemaCmd = 'CREATE SCHEMA $(1);'
41 3380 aaronmk
rmSchemaCmd = 'DROP SCHEMA IF EXISTS "$(1)" CASCADE;'
42 2091 aaronmk
43 784 aaronmk
##### Environment
44
45 225 aaronmk
export PGOPTIONS = --client-min-messages=WARNING
46
47 784 aaronmk
##### General targets
48 225 aaronmk
49 1274 aaronmk
remake: _always clean
50
	$(MAKE)
51
# re-run make so that cache of existing files is reset
52
53 1288 aaronmk
%/remake: _always
54
	$(MAKE) $(@D)/clean
55
	$(MAKE) $(@D)/
56
# re-run make so that cache of existing files is reset
57
58 1813 aaronmk
%-remake: _always
59
	rm -f $*
60
	$(MAKE) $*
61
# re-run make so that cache of existing files is reset
62
63 2981 aaronmk
%/reinstall: _always %/uninstall %/install ;
64
65 784 aaronmk
##### Installation
66 241 aaronmk
67 3377 aaronmk
install: _always core mysql inputs/download inputs/install test
68 241 aaronmk
	@$(done)
69 202 aaronmk
70 418 aaronmk
uninstall: _always inputs/uninstall rm_mysql rm_core ;
71 241 aaronmk
72 418 aaronmk
reinstall: _always uninstall install ;
73 225 aaronmk
74 784 aaronmk
##### Core: VegBIEN DB and dependencies
75 202 aaronmk
76 1746 aaronmk
core: _always $(call forOs,python php postgres) db
77 241 aaronmk
	@$(done)
78 235 aaronmk
79 1746 aaronmk
rm_core: _always rm_db ;
80 241 aaronmk
81 418 aaronmk
reinstall_core: _always rm_core core ;
82 241 aaronmk
83 784 aaronmk
##### Python
84
85 383 aaronmk
python-Linux: _always
86 2618 aaronmk
	-sudo apt-get install python
87
	-sudo apt-get install python-dev
88
	-sudo apt-get install python-dateutil
89
	-sudo apt-get install python-parallel
90
	-sudo apt-get install python-profiler
91
	-sudo apt-get install python-pip
92
	-sudo apt-get install pymetrics
93 3156 aaronmk
	-sudo pip install ordereddict
94 1845 aaronmk
	-sudo pip install pp
95 284 aaronmk
96 383 aaronmk
python-Darwin: _always
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 4752 aaronmk
pgVersion = "$$(env minor=1 bin/pg_version)"
143
144 383 aaronmk
postgres-Linux: _always
145 2618 aaronmk
	-sudo apt-get install postgresql
146 4752 aaronmk
	-sudo apt-get install postgresql-plpython-$(pgVersion)
147 2618 aaronmk
	-sudo apt-get install libpq-dev
148 4752 aaronmk
	-sudo apt-get install postgresql-$(pgVersion)-postgis
149 2618 aaronmk
	-sudo apt-get install phppgadmin
150 336 aaronmk
	$(editPhppgadminApacheConf)
151 334 aaronmk
	$(editApacheConfForPhppgadmin)
152 336 aaronmk
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
153 334 aaronmk
	-sudo apache2ctl restart
154 284 aaronmk
	-sudo pip install psycopg2
155 2619 aaronmk
# run pg_version inline because it needs postgresql to be installed first
156 334 aaronmk
# ignore errors if conf files already edited
157 235 aaronmk
158 383 aaronmk
postgres-Darwin: _always
159 254 aaronmk
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
160
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
161
	@echo 'Open the disk image'
162
	@echo 'Run the installer in it'
163
	@$(wait)
164 235 aaronmk
165 418 aaronmk
postgres-: _always ; # other OSes
166 241 aaronmk
167 247 aaronmk
psqlOpts := --set ON_ERROR_STOP=1 --quiet
168 1980 aaronmk
psqlAsAdmin := sudo -E -u postgres psql $(psqlOpts)
169
    # -E preserves env vars so PGOPTIONS is passed to psql
170 2624 aaronmk
psqlAsAdminVegbien := $(psqlAsAdmin) vegbien
171 1519 aaronmk
psqlAsBien := bin/psql_vegbien $(psqlOpts)
172 272 aaronmk
bienPassword := $(shell cat config/bien_password)
173 241 aaronmk
174 784 aaronmk
##### VegBIEN DB
175 241 aaronmk
176 1967 aaronmk
#### DB and bien user
177
178 2090 aaronmk
confirmRmDb = $(call confirm,WARNING: This will delete your entire VegBIEN\
179 3374 aaronmk
DB!,This includes all archived imports and staging tables.)
180 2089 aaronmk
181 2633 aaronmk
confirmRmPublicSchema = $(call confirm,WARNING: This will delete the current\
182
public schema of your VegBIEN DB!,To save it: make schemas/rotate)
183 2089 aaronmk
184 2091 aaronmk
rmPublicSchema := $(call rmSchemaCmd,public)
185 1967 aaronmk
186 2631 aaronmk
db: mk_db rm_initial_public schemas/install _always ;
187 1967 aaronmk
188
mk_db: _always
189 1745 aaronmk
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
190 2037 aaronmk
	-echo "CREATE DATABASE vegbien WITH OWNER bien TEMPLATE template1 \
191 1940 aaronmk
ENCODING 'UTF8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';"|$(psqlAsAdmin)
192 2627 aaronmk
	-bin/postgres_vegbien createlang plpythonu
193
# ignore errors if user/database/etc. exists
194 2091 aaronmk
195
rm_initial_public: _always
196 2089 aaronmk
	@$(confirmRmPublicSchema)
197 2624 aaronmk
	echo $(rmPublicSchema)|$(psqlAsAdminVegbien)
198 1967 aaronmk
# drop public schema *as admin* because it starts out owned by postgres
199 241 aaronmk
200 383 aaronmk
rm_db: _always
201 1748 aaronmk
	@$(confirmRmDb)
202
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
203
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
204 241 aaronmk
205 418 aaronmk
reinstall_db: _always rm_db db ;
206 241 aaronmk
207 2631 aaronmk
#### Schemas
208 1967 aaronmk
209 2980 aaronmk
schemas := temp functions py_functions public
210 2631 aaronmk
schemas/install: $(schemas:%=schemas/%/install) ;
211
212 2980 aaronmk
schemasReversed := public py_functions functions temp
213 2631 aaronmk
schemas/uninstall: $(schemasReversed:%=schemas/%/uninstall) ;
214
215
### public
216
217
schemas/public/install: schemas/vegbien.sql _always
218 2091 aaronmk
	-echo $(call mkSchemaCmd,public)|$(psqlAsBien)
219 2036 aaronmk
	<$< $(psqlAsBien)
220 1967 aaronmk
# ignore errors if schema exists
221
# public schema will be owned by bien
222
223 2631 aaronmk
schemas/public/uninstall: _always
224 2089 aaronmk
	@$(confirmRmPublicSchema)
225 1967 aaronmk
	echo $(rmPublicSchema)|$(psqlAsBien)
226
227 2901 aaronmk
schemas/rotate: _always schemas/rotate-only schemas/public/install ;
228 2034 aaronmk
schemas/rotate-only: _always
229
	echo 'ALTER SCHEMA public RENAME TO "public.$(date)";'|$(psqlAsBien)
230
231 2631 aaronmk
### py_functions
232 2091 aaronmk
233 2631 aaronmk
schemas/py_functions/install: schemas/py_functions.sql _always
234 3376 aaronmk
	-<$< $(psqlAsAdminVegbien)
235
# ignore errors if schema exists
236 2631 aaronmk
237
### Others
238
239
schemas/%/install: schemas/%.sql _always
240 3376 aaronmk
	-<$< $(psqlAsBien)
241
# ignore errors if schema exists
242 2091 aaronmk
243 3393 aaronmk
schemas/%/uninstall: _always
244 2631 aaronmk
	echo $(call rmSchemaCmd,$*)|$(psqlAsBien)
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