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