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