Project

General

Profile

1 784 aaronmk
##### Vars/functions
2
3 244 aaronmk
# Make
4 247 aaronmk
SHELL := /bin/bash
5 625 aaronmk
pathParts = $(shell path="$(1)"; echo "$${path%%/*}" "$${path\#*/}")
6 739 aaronmk
topDir = $(word 1,$(pathParts))
7
subPath = $(word 2,$(pathParts))
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
rmSchemaCmd = 'DROP SCHEMA IF EXISTS $(1) CASCADE;'
39
40 784 aaronmk
##### Environment
41
42 225 aaronmk
export PGOPTIONS = --client-min-messages=WARNING
43
44 784 aaronmk
##### General targets
45 225 aaronmk
46 414 aaronmk
all:
47 228 aaronmk
48 1260 aaronmk
.SUFFIXES: # turn off built-in suffix rules
49
.SECONDARY: # don't automatically delete intermediate files
50 1354 aaronmk
.DELETE_ON_ERROR: # delete target if recipe fails
51 225 aaronmk
52 383 aaronmk
_always:
53
.PHONY: _always
54 202 aaronmk
55 1274 aaronmk
remake: _always clean
56
	$(MAKE)
57
# re-run make so that cache of existing files is reset
58
59 1288 aaronmk
%/remake: _always
60
	$(MAKE) $(@D)/clean
61
	$(MAKE) $(@D)/
62
# re-run make so that cache of existing files is reset
63
64 1813 aaronmk
%-remake: _always
65
	rm -f $*
66
	$(MAKE) $*
67
# re-run make so that cache of existing files is reset
68
69 784 aaronmk
##### Installation
70 241 aaronmk
71 411 aaronmk
install: _always core mysql inputs/install test
72 241 aaronmk
	@$(done)
73 202 aaronmk
74 418 aaronmk
uninstall: _always inputs/uninstall rm_mysql rm_core ;
75 241 aaronmk
76 418 aaronmk
reinstall: _always uninstall install ;
77 225 aaronmk
78 784 aaronmk
##### Core: VegBIEN DB and dependencies
79 202 aaronmk
80 1746 aaronmk
core: _always $(call forOs,python php postgres) db
81 241 aaronmk
	@$(done)
82 235 aaronmk
83 1746 aaronmk
rm_core: _always rm_db ;
84 241 aaronmk
85 418 aaronmk
reinstall_core: _always rm_core core ;
86 241 aaronmk
87 784 aaronmk
##### Python
88
89 383 aaronmk
python-Linux: _always
90 1844 aaronmk
	-sudo apt-get install python python-dev python-dateutil python-parallel \
91
python-profiler python-pip pymetrics
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
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 383 aaronmk
postgres-Linux: _always
138 329 aaronmk
	-sudo apt-get install postgresql libpq-dev phppgadmin
139 336 aaronmk
	$(editPhppgadminApacheConf)
140 334 aaronmk
	$(editApacheConfForPhppgadmin)
141 336 aaronmk
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
142 334 aaronmk
	-sudo apache2ctl restart
143 284 aaronmk
	-sudo pip install psycopg2
144 334 aaronmk
# ignore errors if conf files already edited
145 235 aaronmk
146 383 aaronmk
postgres-Darwin: _always
147 254 aaronmk
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
148
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
149
	@echo 'Open the disk image'
150
	@echo 'Run the installer in it'
151
	@$(wait)
152 235 aaronmk
153 418 aaronmk
postgres-: _always ; # other OSes
154 241 aaronmk
155 247 aaronmk
psqlOpts := --set ON_ERROR_STOP=1 --quiet
156 1980 aaronmk
psqlAsAdmin := sudo -E -u postgres psql $(psqlOpts)
157
    # -E preserves env vars so PGOPTIONS is passed to psql
158 1519 aaronmk
psqlAsBien := bin/psql_vegbien $(psqlOpts)
159 272 aaronmk
bienPassword := $(shell cat config/bien_password)
160 241 aaronmk
161 784 aaronmk
##### VegBIEN DB
162 241 aaronmk
163 1967 aaronmk
#### DB and bien user
164
165 2090 aaronmk
confirmRmDb = $(call confirm,WARNING: This will delete your entire VegBIEN\
166
DB!,This includes all public schema versions and staging tables.)
167 2089 aaronmk
168 2090 aaronmk
confirmRmPublicSchema = $(call confirm,WARNING: This will delete the current\
169
public schema of your VegBIEN DB!,To save it: make schemas/rotate)
170 2089 aaronmk
171 2091 aaronmk
rmPublicSchema := $(call rmSchemaCmd,public)
172 1967 aaronmk
173 2091 aaronmk
db: mk_db rm_initial_public schemas/functions/clear schemas/install _always ;
174 1967 aaronmk
175
mk_db: _always
176 1745 aaronmk
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
177 2037 aaronmk
	-echo "CREATE DATABASE vegbien WITH OWNER bien TEMPLATE template1 \
178 1940 aaronmk
ENCODING 'UTF8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';"|$(psqlAsAdmin)
179 2091 aaronmk
# ignore errors if user or database exists
180
181
rm_initial_public: _always
182 2089 aaronmk
	@$(confirmRmPublicSchema)
183 1980 aaronmk
	echo $(rmPublicSchema)|$(psqlAsAdmin) vegbien
184 1967 aaronmk
# drop public schema *as admin* because it starts out owned by postgres
185 241 aaronmk
186 383 aaronmk
rm_db: _always
187 1748 aaronmk
	@$(confirmRmDb)
188
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
189
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
190 241 aaronmk
191 418 aaronmk
reinstall_db: _always rm_db db ;
192 241 aaronmk
193 1967 aaronmk
#### public schema
194
195
schemas/install: schemas/vegbien.sql _always
196 2091 aaronmk
	-echo $(call mkSchemaCmd,public)|$(psqlAsBien)
197 2036 aaronmk
	<$< $(psqlAsBien)
198 1967 aaronmk
# ignore errors if schema exists
199
# public schema will be owned by bien
200
201
schemas/uninstall: _always
202 2089 aaronmk
	@$(confirmRmPublicSchema)
203 1967 aaronmk
	echo $(rmPublicSchema)|$(psqlAsBien)
204
205 2034 aaronmk
schemas/rotate: _always schemas/rotate-only schemas/install ;
206
schemas/rotate-only: _always
207
	echo 'ALTER SCHEMA public RENAME TO "public.$(date)";'|$(psqlAsBien)
208
209 389 aaronmk
empty_db: _always schemas/vegbien_empty.sql
210 2089 aaronmk
	@$(confirmRmPublicSchema)
211 389 aaronmk
	$(psqlAsBien) <schemas/vegbien_empty.sql
212 241 aaronmk
213 2091 aaronmk
#### functions schema
214
215
confirmRmFunctions = $(call confirm,WARNING:\
216
This will delete the functions schema of your VegBIEN DB!)
217
218
schemas/functions/clear: _always
219
	@$(confirmRmFunctions)
220
	echo $(call rmSchemaCmd,functions)|$(psqlAsBien)
221
	echo $(call mkSchemaCmd,functions)|$(psqlAsBien)
222
223 784 aaronmk
##### MySQL
224 241 aaronmk
225 383 aaronmk
mysql: _always $(call forOs,mysql) mysql_user
226 241 aaronmk
	@$(done)
227
228 418 aaronmk
rm_mysql: _always rm_mysql_user ;
229 241 aaronmk
230 383 aaronmk
mysql-Linux: _always
231 244 aaronmk
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
232 238 aaronmk
	@$(wait)
233 237 aaronmk
	-sudo apt-get install mysql-server mysql-client python-mysqldb
234 235 aaronmk
235 383 aaronmk
mysql-Darwin: _always
236 244 aaronmk
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
237 235 aaronmk
	@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
238
	@echo 'Extract the archive'
239
	@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
240 284 aaronmk
	@echo 'Run ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
241
	@echo 'Run ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
242 235 aaronmk
	@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
243
	@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
244
	@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
245
	@echo 'Change to the directory of the extracted files'
246 284 aaronmk
	@echo 'Run python setup.py clean'
247
	@echo 'Run python setup.py build'
248 235 aaronmk
	@echo 'Run sudo python setup.py install'
249
	@echo "Run python -c 'import MySQLdb'"
250 238 aaronmk
	@$(wait)
251 235 aaronmk
252 383 aaronmk
mysql-: _always # other OSes
253 235 aaronmk
254 247 aaronmk
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
255 235 aaronmk
256 383 aaronmk
mysql_user: _always
257 241 aaronmk
	-$($@_cmd)
258 364 aaronmk
# ignore errors if user exists
259 244 aaronmk
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
260 245 aaronmk
'$(bienPassword)';"|$(mysqlAsRoot)
261 202 aaronmk
262 383 aaronmk
rm_mysql_user: _always
263 245 aaronmk
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
264 364 aaronmk
# ignore errors if user exists
265 202 aaronmk
266 784 aaronmk
##### Datasources
267 202 aaronmk
268 418 aaronmk
inputs: _always inputs/all ;
269 245 aaronmk
270 1542 aaronmk
import: _always import-msg inputs/import ;
271
import-msg: _always
272 1550 aaronmk
	@echo $(emph)"To import all inputs at once:"$(endEmph) . bin/import_all
273 1542 aaronmk
	@echo
274
	@$(wait)
275 250 aaronmk
276 1458 aaronmk
verify: _always inputs/verify
277 1241 aaronmk
	@$(done)
278 386 aaronmk
279 1458 aaronmk
test: _always inputs/test
280 397 aaronmk
	@$(done)
281 1458 aaronmk
282
##### Testing
283
284 1743 aaronmk
test-all: _always remake test missing_mappings
285 1458 aaronmk
	@$(done)
286 1967 aaronmk
287
##### Subdir forwarding
288
289
# Must come last so overridden targets are not forwarded
290
291
define subdirTargets
292
$(subdir): _always
293
	+$$(subMake)
294
295
$(subdir)%: _always
296
	+$$(subMake)
297
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
298
299
$(subdir)reinstall: _always $(subdir)uninstall $(subdir)install ;
300
endef
301
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
302
303
Makefile: ;
304
305
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
306 1999 aaronmk
307
#### Maps validation
308
309
# Must come after Subdir forwarding to avoid infinite recursion, for some reason
310
311
missing_mappings: _always missing_join_mappings missing_input_mappings ;
312
313
missing_%_mappings: _always # stem is one of join|input
314
	@echo $(emph)"Missing $* mappings:"$(endEmph)
315
	@+$(MAKE) inputs/$@|grep -v -e ' Missing '\
316
$(if $(filter join,$*), -e '^make '|sort|uniq)