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