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
# DB
37
mkSchemaCmd = 'CREATE SCHEMA $(1);'
38
rmSchemaCmd = 'DROP SCHEMA IF EXISTS $(1) CASCADE;'
39

    
40
##### Environment
41

    
42
export PGOPTIONS = --client-min-messages=WARNING
43

    
44
##### General targets
45

    
46
all:
47

    
48
.SUFFIXES: # turn off built-in suffix rules
49
.SECONDARY: # don't automatically delete intermediate files
50
.DELETE_ON_ERROR: # delete target if recipe fails
51

    
52
_always:
53
.PHONY: _always
54

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

    
59
%/remake: _always
60
	$(MAKE) $(@D)/clean
61
	$(MAKE) $(@D)/
62
# re-run make so that cache of existing files is reset
63

    
64
%-remake: _always
65
	rm -f $*
66
	$(MAKE) $*
67
# re-run make so that cache of existing files is reset
68

    
69
##### Installation
70

    
71
install: _always core mysql inputs/install test
72
	@$(done)
73

    
74
uninstall: _always inputs/uninstall rm_mysql rm_core ;
75

    
76
reinstall: _always uninstall install ;
77

    
78
##### Core: VegBIEN DB and dependencies
79

    
80
core: _always $(call forOs,python php postgres) db
81
	@$(done)
82

    
83
rm_core: _always rm_db ;
84

    
85
reinstall_core: _always rm_core core ;
86

    
87
##### Python
88

    
89
python-Linux: _always
90
	-sudo apt-get install python
91
	-sudo apt-get install python-dev
92
	-sudo apt-get install python-dateutil
93
	-sudo apt-get install python-parallel
94
	-sudo apt-get install python-profiler
95
	-sudo apt-get install python-pip
96
	-sudo apt-get install pymetrics
97
	-sudo pip install pp
98

    
99
python-Darwin: _always
100
	@echo $(emph)'Installing python-dateutil on Mac OS X:'$(endEmph)
101
	@echo 'Download it: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
102
	@echo 'Extract the archive'
103
	@echo 'Change to the directory of the extracted files'
104
	@echo 'Run python setup.py build'
105
	@echo 'Run sudo python setup.py install'
106
	@echo "Run python -c 'import dateutil'"
107
	@$(wait)
108

    
109
##### PHP
110

    
111
php-Linux: _always
112
	-sudo apt-get install php-http-request
113

    
114
php-Darwin: _always
115
	@echo $(emph)'Installing PHP PEAR and HTTP_Request on Mac OS X:'$(endEmph)
116
	@echo 'Download http://pear.php.net/go-pear.phar'
117
	@echo 'Change to the directory of the downloaded file'
118
	@echo 'Run php -d detect_unicode=0 go-pear.phar'
119
	@echo 'Whenever prompted, press Enter to select default options'
120
	@echo 'Add $$HOME/pear/bin to your $$PATH'
121
	@echo 'Add $(HOME)/pear/share/pear to your php.ini include_path'
122
	@echo 'If needed, set $$PHPRC to your php.ini'
123
	@echo "Run pear install HTTP_Request"
124
	@echo 'To run a php command: php -c <php.ini-path> ...'
125
	@$(wait)
126

    
127
##### PostgreSQL
128

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

    
132
apacheConf := /etc/apache2/apache2.conf
133
apacheConfPhppgadminLine := Include /etc/phppgadmin/apache.conf
134
editApacheConfForPhppgadmin =\
135
$(if $(shell grep -F "$(apacheConfPhppgadminLine)" $(apacheConf)),,\
136
$(call sudoAppend,$(apacheConf),\n$(apacheConfPhppgadminLine)))
137

    
138
nonApacheOnPort80 = $(shell sudo lsof -i :80|tail -n +2|grep -v -F apache2)
139
editApachePortsConf = echo $$'1\n,s/\\b80\\b/8080/g\nwq'|\
140
sudo ed --loose-exit-status --verbose /etc/apache2/ports.conf
141

    
142
postgres-Linux: _always
143
	-sudo apt-get install postgresql
144
	-sudo apt-get install postgresql-plpython-"$$(env minor=1 bin/pg_version)"
145
	-sudo apt-get install libpq-dev
146
	-sudo apt-get install phppgadmin
147
	$(editPhppgadminApacheConf)
148
	$(editApacheConfForPhppgadmin)
149
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
150
	-sudo apache2ctl restart
151
	-sudo pip install psycopg2
152
# run pg_version inline because it needs postgresql to be installed first
153
# ignore errors if conf files already edited
154

    
155
postgres-Darwin: _always
156
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
157
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
158
	@echo 'Open the disk image'
159
	@echo 'Run the installer in it'
160
	@$(wait)
161

    
162
postgres-: _always ; # other OSes
163

    
164
psqlOpts := --set ON_ERROR_STOP=1 --quiet
165
psqlAsAdmin := sudo -E -u postgres psql $(psqlOpts)
166
    # -E preserves env vars so PGOPTIONS is passed to psql
167
psqlAsBien := bin/psql_vegbien $(psqlOpts)
168
bienPassword := $(shell cat config/bien_password)
169

    
170
##### VegBIEN DB
171

    
172
#### DB and bien user
173

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

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

    
180
rmPublicSchema := $(call rmSchemaCmd,public)
181

    
182
db: mk_db rm_initial_public schemas/functions/reset schemas/install _always ;
183

    
184
mk_db: _always
185
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
186
	-echo "CREATE DATABASE vegbien WITH OWNER bien TEMPLATE template1 \
187
ENCODING 'UTF8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';"|$(psqlAsAdmin)
188
# ignore errors if user or database exists
189

    
190
rm_initial_public: _always
191
	@$(confirmRmPublicSchema)
192
	echo $(rmPublicSchema)|$(psqlAsAdmin) vegbien
193
# drop public schema *as admin* because it starts out owned by postgres
194

    
195
rm_db: _always
196
	@$(confirmRmDb)
197
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
198
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
199

    
200
reinstall_db: _always rm_db db ;
201

    
202
#### public schema
203

    
204
schemas/install: schemas/vegbien.sql _always
205
	-echo $(call mkSchemaCmd,public)|$(psqlAsBien)
206
	<$< $(psqlAsBien)
207
# ignore errors if schema exists
208
# public schema will be owned by bien
209

    
210
schemas/uninstall: _always
211
	@$(confirmRmPublicSchema)
212
	echo $(rmPublicSchema)|$(psqlAsBien)
213

    
214
schemas/rotate: _always schemas/rotate-only schemas/install ;
215
schemas/rotate-only: _always
216
	echo 'ALTER SCHEMA public RENAME TO "public.$(date)";'|$(psqlAsBien)
217

    
218
empty_db: _always schemas/vegbien_empty.sql
219
	@$(confirmRmPublicSchema)
220
	$(psqlAsBien) <schemas/vegbien_empty.sql
221

    
222
#### functions schema
223

    
224
confirmRmFunctions = $(call confirm,WARNING:\
225
This will delete the functions schema of your VegBIEN DB!)
226

    
227
schemas/functions/reset: schemas/functions.sql _always
228
	@$(confirmRmFunctions)
229
	echo $(call rmSchemaCmd,functions)|$(psqlAsBien)
230
	<$< $(psqlAsBien)
231

    
232
##### MySQL
233

    
234
mysql: _always $(call forOs,mysql) mysql_user
235
	@$(done)
236

    
237
rm_mysql: _always rm_mysql_user ;
238

    
239
mysql-Linux: _always
240
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
241
	@$(wait)
242
	-sudo apt-get install mysql-server mysql-client python-mysqldb
243

    
244
mysql-Darwin: _always
245
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
246
	@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
247
	@echo 'Extract the archive'
248
	@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
249
	@echo 'Run ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
250
	@echo 'Run ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
251
	@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
252
	@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
253
	@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
254
	@echo 'Change to the directory of the extracted files'
255
	@echo 'Run python setup.py clean'
256
	@echo 'Run python setup.py build'
257
	@echo 'Run sudo python setup.py install'
258
	@echo "Run python -c 'import MySQLdb'"
259
	@$(wait)
260

    
261
mysql-: _always # other OSes
262

    
263
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
264

    
265
mysql_user: _always
266
	-$($@_cmd)
267
# ignore errors if user exists
268
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
269
'$(bienPassword)';"|$(mysqlAsRoot)
270

    
271
rm_mysql_user: _always
272
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
273
# ignore errors if user exists
274

    
275
##### Datasources
276

    
277
inputs: _always inputs/all ;
278

    
279
import: _always import-msg inputs/import ;
280
import-msg: _always
281
	@echo $(emph)"To import all inputs at once:"$(endEmph) . bin/import_all
282
	@echo
283
	@$(wait)
284

    
285
verify: _always inputs/verify
286
	@$(done)
287

    
288
test: _always inputs/test
289
	@$(done)
290

    
291
##### Testing
292

    
293
test-all: _always remake test missing_mappings
294
	@$(done)
295

    
296
##### Subdir forwarding
297

    
298
# Must come last so overridden targets are not forwarded
299

    
300
define subdirTargets
301
$(subdir): _always
302
	+$$(subMake)
303

    
304
$(subdir)%: _always
305
	+$$(subMake)
306
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
307

    
308
$(subdir)reinstall: _always $(subdir)uninstall $(subdir)install ;
309
endef
310
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
311

    
312
Makefile: ;
313

    
314
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
315

    
316
#### Maps validation
317

    
318
# Must come after Subdir forwarding to avoid infinite recursion, for some reason
319

    
320
missing_mappings: _always missing_join_mappings missing_input_mappings ;
321

    
322
missing_%_mappings: _always # stem is one of join|input
323
	@echo $(emph)"Missing $* mappings:"$(endEmph)
324
	@+$(MAKE) inputs/$@|grep -v -e ' Missing '\
325
$(if $(filter join,$*), -e '^make '|sort|uniq)
(1-1/3)