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 1741 aaronmk
##### Subdir forwarding
57
58 402 aaronmk
define subdirTargets
59
$(subdir): _always
60
	+$$(subMake)
61 388 aaronmk
62 402 aaronmk
$(subdir)%: _always
63
	+$$(subMake)
64 1628 aaronmk
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
65 402 aaronmk
endef
66
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
67 388 aaronmk
68 416 aaronmk
Makefile: ;
69
70 414 aaronmk
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
71
72 784 aaronmk
##### Installation
73 241 aaronmk
74 411 aaronmk
install: _always core mysql inputs/install test
75 241 aaronmk
	@$(done)
76 202 aaronmk
77 418 aaronmk
uninstall: _always inputs/uninstall rm_mysql rm_core ;
78 241 aaronmk
79 418 aaronmk
reinstall: _always uninstall install ;
80 225 aaronmk
81 784 aaronmk
##### Core: VegBIEN DB and dependencies
82 202 aaronmk
83 1746 aaronmk
core: _always $(call forOs,python php postgres) db
84 241 aaronmk
	@$(done)
85 235 aaronmk
86 1746 aaronmk
rm_core: _always rm_db ;
87 241 aaronmk
88 418 aaronmk
reinstall_core: _always rm_core core ;
89 241 aaronmk
90 784 aaronmk
##### Python
91
92 383 aaronmk
python-Linux: _always
93 1255 aaronmk
	-sudo apt-get install python python-dev python-dateutil python-profiler\
94 1375 aaronmk
python-pip pymetrics
95 284 aaronmk
96 383 aaronmk
python-Darwin: _always
97 284 aaronmk
	@echo $(emph)'Installing python-dateutil on Mac OS X:'$(endEmph)
98
	@echo 'Download it: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
99
	@echo 'Extract the archive'
100
	@echo 'Change to the directory of the extracted files'
101
	@echo 'Run python setup.py build'
102
	@echo 'Run sudo python setup.py install'
103
	@echo "Run python -c 'import dateutil'"
104
	@$(wait)
105
106 1599 aaronmk
##### PHP
107
108
php-Linux: _always
109
	-sudo apt-get install php-http-request
110
111
php-Darwin: _always
112 1600 aaronmk
	@echo $(emph)'Installing PHP PEAR and HTTP_Request on Mac OS X:'$(endEmph)
113
	@echo 'Download http://pear.php.net/go-pear.phar'
114
	@echo 'Change to the directory of the downloaded file'
115
	@echo 'Run php -d detect_unicode=0 go-pear.phar'
116
	@echo 'Whenever prompted, press Enter to select default options'
117
	@echo 'Add $$HOME/pear/bin to your $$PATH'
118
	@echo 'Add $(HOME)/pear/share/pear to your php.ini include_path'
119 1665 aaronmk
	@echo 'If needed, set $$PHPRC to your php.ini'
120 1600 aaronmk
	@echo "Run pear install HTTP_Request"
121
	@echo 'To run a php command: php -c <php.ini-path> ...'
122 1599 aaronmk
	@$(wait)
123
124 784 aaronmk
##### PostgreSQL
125
126 337 aaronmk
editPhppgadminApacheConf = echo $$'1\n,s/\# \(allow from all\)/\1/\nwq'|\
127 336 aaronmk
sudo ed --loose-exit-status --verbose /etc/phppgadmin/apache.conf
128 334 aaronmk
129
apacheConf := /etc/apache2/apache2.conf
130
apacheConfPhppgadminLine := Include /etc/phppgadmin/apache.conf
131
editApacheConfForPhppgadmin =\
132
$(if $(shell grep -F "$(apacheConfPhppgadminLine)" $(apacheConf)),,\
133
$(call sudoAppend,$(apacheConf),\n$(apacheConfPhppgadminLine)))
134
135 335 aaronmk
nonApacheOnPort80 = $(shell sudo lsof -i :80|tail -n +2|grep -v -F apache2)
136 336 aaronmk
editApachePortsConf = echo $$'1\n,s/\\b80\\b/8080/g\nwq'|\
137
sudo ed --loose-exit-status --verbose /etc/apache2/ports.conf
138 334 aaronmk
139 383 aaronmk
postgres-Linux: _always
140 329 aaronmk
	-sudo apt-get install postgresql libpq-dev phppgadmin
141 336 aaronmk
	$(editPhppgadminApacheConf)
142 334 aaronmk
	$(editApacheConfForPhppgadmin)
143 336 aaronmk
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
144 334 aaronmk
	-sudo apache2ctl restart
145 284 aaronmk
	-sudo pip install psycopg2
146 334 aaronmk
# ignore errors if conf files already edited
147 235 aaronmk
148 383 aaronmk
postgres-Darwin: _always
149 254 aaronmk
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
150
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
151
	@echo 'Open the disk image'
152
	@echo 'Run the installer in it'
153
	@$(wait)
154 235 aaronmk
155 418 aaronmk
postgres-: _always ; # other OSes
156 241 aaronmk
157 247 aaronmk
psqlOpts := --set ON_ERROR_STOP=1 --quiet
158 249 aaronmk
psqlAsAdmin := sudo -u postgres psql $(psqlOpts)
159 1519 aaronmk
psqlAsBien := bin/psql_vegbien $(psqlOpts)
160 272 aaronmk
bienPassword := $(shell cat config/bien_password)
161 241 aaronmk
162 784 aaronmk
##### VegBIEN DB
163 241 aaronmk
164 1745 aaronmk
db: schemas/vegbien.sql _always
165
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
166
	-echo "CREATE DATABASE vegbien OWNER bien ENCODING 'UTF8' \
167 381 aaronmk
TEMPLATE template1;"|$(psqlAsAdmin)
168 1745 aaronmk
	$(psqlAsBien) <$<
169
# ignore errors if user or database exists
170 241 aaronmk
171 1748 aaronmk
confirmRmDb = $(call confirm,WARNING: This will delete your VegBIEN DB!\
172
Continue?)
173 1745 aaronmk
174 383 aaronmk
rm_db: _always
175 1748 aaronmk
	@$(confirmRmDb)
176
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
177
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
178 241 aaronmk
179 418 aaronmk
reinstall_db: _always rm_db db ;
180 241 aaronmk
181 389 aaronmk
empty_db: _always schemas/vegbien_empty.sql
182 1748 aaronmk
	@$(confirmRmDb)
183 389 aaronmk
	$(psqlAsBien) <schemas/vegbien_empty.sql
184 241 aaronmk
185 784 aaronmk
##### MySQL
186 241 aaronmk
187 383 aaronmk
mysql: _always $(call forOs,mysql) mysql_user
188 241 aaronmk
	@$(done)
189
190 418 aaronmk
rm_mysql: _always rm_mysql_user ;
191 241 aaronmk
192 383 aaronmk
mysql-Linux: _always
193 244 aaronmk
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
194 238 aaronmk
	@$(wait)
195 237 aaronmk
	-sudo apt-get install mysql-server mysql-client python-mysqldb
196 235 aaronmk
197 383 aaronmk
mysql-Darwin: _always
198 244 aaronmk
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
199 235 aaronmk
	@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
200
	@echo 'Extract the archive'
201
	@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
202 284 aaronmk
	@echo 'Run ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
203
	@echo 'Run ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
204 235 aaronmk
	@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
205
	@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
206
	@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
207
	@echo 'Change to the directory of the extracted files'
208 284 aaronmk
	@echo 'Run python setup.py clean'
209
	@echo 'Run python setup.py build'
210 235 aaronmk
	@echo 'Run sudo python setup.py install'
211
	@echo "Run python -c 'import MySQLdb'"
212 238 aaronmk
	@$(wait)
213 235 aaronmk
214 383 aaronmk
mysql-: _always # other OSes
215 235 aaronmk
216 247 aaronmk
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
217 235 aaronmk
218 383 aaronmk
mysql_user: _always
219 241 aaronmk
	-$($@_cmd)
220 364 aaronmk
# ignore errors if user exists
221 244 aaronmk
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
222 245 aaronmk
'$(bienPassword)';"|$(mysqlAsRoot)
223 202 aaronmk
224 383 aaronmk
rm_mysql_user: _always
225 245 aaronmk
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
226 364 aaronmk
# ignore errors if user exists
227 202 aaronmk
228 784 aaronmk
##### Datasources
229 202 aaronmk
230 418 aaronmk
inputs: _always inputs/all ;
231 245 aaronmk
232 1542 aaronmk
import: _always import-msg inputs/import ;
233
import-msg: _always
234 1550 aaronmk
	@echo $(emph)"To import all inputs at once:"$(endEmph) . bin/import_all
235 1542 aaronmk
	@echo
236
	@$(wait)
237 250 aaronmk
238 1458 aaronmk
verify: _always inputs/verify
239 1241 aaronmk
	@$(done)
240 386 aaronmk
241 1458 aaronmk
test: _always inputs/test
242 397 aaronmk
	@$(done)
243 1458 aaronmk
244 1519 aaronmk
#### Maps validation
245
246 1741 aaronmk
missing_mappings: _always missing_join_mappings missing_input_mappings ;
247 1519 aaronmk
248 1741 aaronmk
missing_%_mappings: _always # stem is one of join|input
249 1742 aaronmk
	@echo $(emph)"Missing $* mappings:"$(endEmph)
250
	@+$(MAKE) inputs/$@|grep -v -e ' Missing '\
251
$(if $(filter join,$*), -e '^make '|sort|uniq)
252 1519 aaronmk
253 1458 aaronmk
##### Testing
254
255 1743 aaronmk
test-all: _always remake test missing_mappings
256 1458 aaronmk
	@$(done)