Project

General

Profile

1
##### Vars/functions
2

    
3
# Make
4
SHELL := /bin/bash
5
pathParts = $(shell path="$(1)"; echo "$${path%%/*}" "$${path\#*/}")
6
topDir = $(word 1,$(pathParts))
7
subPath = $(word 2,$(pathParts))
8
subMake = $(MAKE) $(call subPath,$@) --directory=$(call topDir,$@)
9

    
10
# OS
11
os := $(shell uname)
12
forOs = $(patsubst %,%-$(filter Linux Darwin,$(os)),$(1))
13

    
14
# System
15
date = $(shell date +"%Y-%m-%d-%H-%M-%S")
16

    
17
# Terminal
18
esc := '['
19
reset := $(esc)'0m'
20
emph := $(esc)'7m '
21
endEmph := ' '$(reset)
22

    
23
# User interaction
24

    
25
done = echo; echo $(emph)"Finished $@"$(endEmph); echo
26

    
27
wait := read -p $(emph)'Press ENTER to continue:'$(endEmph) REPLY
28

    
29
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

    
33
# File editing
34
sudoAppend = echo $$'$(2)'|sudo tee -a $(1) >/dev/null
35

    
36
##### Environment
37

    
38
export PGOPTIONS = --client-min-messages=WARNING
39

    
40
##### General targets
41

    
42
all:
43

    
44
.SUFFIXES: # turn off built-in suffix rules
45
.SECONDARY: # don't automatically delete intermediate files
46
.DELETE_ON_ERROR: # delete target if recipe fails
47

    
48
_always:
49
.PHONY: _always
50

    
51
remake: _always clean
52
	$(MAKE)
53
# re-run make so that cache of existing files is reset
54

    
55
%/remake: _always
56
	$(MAKE) $(@D)/clean
57
	$(MAKE) $(@D)/
58
# re-run make so that cache of existing files is reset
59

    
60
%-remake: _always
61
	rm -f $*
62
	$(MAKE) $*
63
# re-run make so that cache of existing files is reset
64

    
65
##### Installation
66

    
67
install: _always core mysql inputs/install test
68
	@$(done)
69

    
70
uninstall: _always inputs/uninstall rm_mysql rm_core ;
71

    
72
reinstall: _always uninstall install ;
73

    
74
##### Core: VegBIEN DB and dependencies
75

    
76
core: _always $(call forOs,python php postgres) db
77
	@$(done)
78

    
79
rm_core: _always rm_db ;
80

    
81
reinstall_core: _always rm_core core ;
82

    
83
##### Python
84

    
85
python-Linux: _always
86
	-sudo apt-get install python python-dev python-dateutil python-parallel \
87
python-profiler python-pip pymetrics
88
	-sudo pip install pp
89

    
90
python-Darwin: _always
91
	@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
##### PHP
101

    
102
php-Linux: _always
103
	-sudo apt-get install php-http-request
104

    
105
php-Darwin: _always
106
	@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
	@echo 'If needed, set $$PHPRC to your php.ini'
114
	@echo "Run pear install HTTP_Request"
115
	@echo 'To run a php command: php -c <php.ini-path> ...'
116
	@$(wait)
117

    
118
##### PostgreSQL
119

    
120
editPhppgadminApacheConf = echo $$'1\n,s/deny from all/allow from all/\nwq'|\
121
sudo ed --loose-exit-status --verbose /etc/phppgadmin/apache.conf
122

    
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
nonApacheOnPort80 = $(shell sudo lsof -i :80|tail -n +2|grep -v -F apache2)
130
editApachePortsConf = echo $$'1\n,s/\\b80\\b/8080/g\nwq'|\
131
sudo ed --loose-exit-status --verbose /etc/apache2/ports.conf
132

    
133
postgres-Linux: _always
134
	-sudo apt-get install postgresql libpq-dev phppgadmin
135
	$(editPhppgadminApacheConf)
136
	$(editApacheConfForPhppgadmin)
137
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
138
	-sudo apache2ctl restart
139
	-sudo pip install psycopg2
140
# ignore errors if conf files already edited
141

    
142
postgres-Darwin: _always
143
	@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

    
149
postgres-: _always ; # other OSes
150

    
151
psqlOpts := --set ON_ERROR_STOP=1 --quiet
152
psqlAsAdmin := sudo -E -u postgres psql $(psqlOpts)
153
    # -E preserves env vars so PGOPTIONS is passed to psql
154
psqlAsBien := bin/psql_vegbien $(psqlOpts)
155
bienPassword := $(shell cat config/bien_password)
156

    
157
##### VegBIEN DB
158

    
159
#### DB and bien user
160

    
161
confirmRmDb = $(call confirm,WARNING: This will delete your entire VegBIEN\
162
DB!,This includes all public schema versions and staging tables.)
163

    
164
confirmRmPublicSchema = $(call confirm,WARNING: This will delete the current\
165
public schema of your VegBIEN DB!,To save it: make schemas/rotate)
166

    
167
rmPublicSchema := 'DROP SCHEMA IF EXISTS public CASCADE;'
168

    
169
db: mk_db schemas/install _always ;
170

    
171
mk_db: _always
172
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
173
	-echo "CREATE DATABASE vegbien WITH OWNER bien TEMPLATE template1 \
174
ENCODING 'UTF8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';"|$(psqlAsAdmin)
175
	@$(confirmRmPublicSchema)
176
	echo $(rmPublicSchema)|$(psqlAsAdmin) vegbien
177
# ignore errors if user or database exists
178
# drop public schema *as admin* because it starts out owned by postgres
179

    
180
rm_db: _always
181
	@$(confirmRmDb)
182
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
183
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
184

    
185
reinstall_db: _always rm_db db ;
186

    
187
#### public schema
188

    
189
schemas/install: schemas/vegbien.sql _always
190
	-echo "CREATE SCHEMA public;"|$(psqlAsBien)
191
	<$< $(psqlAsBien)
192
# ignore errors if schema exists
193
# public schema will be owned by bien
194

    
195
schemas/uninstall: _always
196
	@$(confirmRmPublicSchema)
197
	echo $(rmPublicSchema)|$(psqlAsBien)
198

    
199
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
empty_db: _always schemas/vegbien_empty.sql
204
	@$(confirmRmPublicSchema)
205
	$(psqlAsBien) <schemas/vegbien_empty.sql
206

    
207
##### MySQL
208

    
209
mysql: _always $(call forOs,mysql) mysql_user
210
	@$(done)
211

    
212
rm_mysql: _always rm_mysql_user ;
213

    
214
mysql-Linux: _always
215
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
216
	@$(wait)
217
	-sudo apt-get install mysql-server mysql-client python-mysqldb
218

    
219
mysql-Darwin: _always
220
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
221
	@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
	@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
	@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
	@echo 'Run python setup.py clean'
231
	@echo 'Run python setup.py build'
232
	@echo 'Run sudo python setup.py install'
233
	@echo "Run python -c 'import MySQLdb'"
234
	@$(wait)
235

    
236
mysql-: _always # other OSes
237

    
238
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
239

    
240
mysql_user: _always
241
	-$($@_cmd)
242
# ignore errors if user exists
243
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
244
'$(bienPassword)';"|$(mysqlAsRoot)
245

    
246
rm_mysql_user: _always
247
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
248
# ignore errors if user exists
249

    
250
##### Datasources
251

    
252
inputs: _always inputs/all ;
253

    
254
import: _always import-msg inputs/import ;
255
import-msg: _always
256
	@echo $(emph)"To import all inputs at once:"$(endEmph) . bin/import_all
257
	@echo
258
	@$(wait)
259

    
260
verify: _always inputs/verify
261
	@$(done)
262

    
263
test: _always inputs/test
264
	@$(done)
265

    
266
##### Testing
267

    
268
test-all: _always remake test missing_mappings
269
	@$(done)
270

    
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

    
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)
(1-1/3)