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