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) (y/n)"$(endEmph) REPLY; \
30
test "$$REPLY" = y && echo t),,$(error Aborting))
31

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

    
35
##### Environment
36

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

    
39
##### General targets
40

    
41
all:
42

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

    
47
_always:
48
.PHONY: _always
49

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

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

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

    
64
##### Installation
65

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

    
69
uninstall: _always inputs/uninstall rm_mysql rm_core ;
70

    
71
reinstall: _always uninstall install ;
72

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

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

    
78
rm_core: _always rm_db ;
79

    
80
reinstall_core: _always rm_core core ;
81

    
82
##### Python
83

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

    
89
python-Darwin: _always
90
	@echo $(emph)'Installing python-dateutil on Mac OS X:'$(endEmph)
91
	@echo 'Download it: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
92
	@echo 'Extract the archive'
93
	@echo 'Change to the directory of the extracted files'
94
	@echo 'Run python setup.py build'
95
	@echo 'Run sudo python setup.py install'
96
	@echo "Run python -c 'import dateutil'"
97
	@$(wait)
98

    
99
##### PHP
100

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

    
104
php-Darwin: _always
105
	@echo $(emph)'Installing PHP PEAR and HTTP_Request on Mac OS X:'$(endEmph)
106
	@echo 'Download http://pear.php.net/go-pear.phar'
107
	@echo 'Change to the directory of the downloaded file'
108
	@echo 'Run php -d detect_unicode=0 go-pear.phar'
109
	@echo 'Whenever prompted, press Enter to select default options'
110
	@echo 'Add $$HOME/pear/bin to your $$PATH'
111
	@echo 'Add $(HOME)/pear/share/pear to your php.ini include_path'
112
	@echo 'If needed, set $$PHPRC to your php.ini'
113
	@echo "Run pear install HTTP_Request"
114
	@echo 'To run a php command: php -c <php.ini-path> ...'
115
	@$(wait)
116

    
117
##### PostgreSQL
118

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

    
122
apacheConf := /etc/apache2/apache2.conf
123
apacheConfPhppgadminLine := Include /etc/phppgadmin/apache.conf
124
editApacheConfForPhppgadmin =\
125
$(if $(shell grep -F "$(apacheConfPhppgadminLine)" $(apacheConf)),,\
126
$(call sudoAppend,$(apacheConf),\n$(apacheConfPhppgadminLine)))
127

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

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

    
141
postgres-Darwin: _always
142
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
143
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
144
	@echo 'Open the disk image'
145
	@echo 'Run the installer in it'
146
	@$(wait)
147

    
148
postgres-: _always ; # other OSes
149

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

    
156
##### VegBIEN DB
157

    
158
#### DB and bien user
159

    
160
confirmRmDb = $(call confirm,WARNING:\
161
This will delete your VegBIEN DB if it exists! Continue?)
162
rmPublicSchema := 'DROP SCHEMA IF EXISTS public CASCADE;'
163

    
164
db: mk_db schemas/install _always ;
165

    
166
mk_db: _always
167
	@$(confirmRmDb)
168
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
169
	-echo "CREATE DATABASE vegbien WITH OWNER bien TEMPLATE template0 \
170
ENCODING 'UTF8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';"|$(psqlAsAdmin)
171
	echo $(rmPublicSchema)|$(psqlAsAdmin) vegbien
172
	$(createPlpgsql)
173
# ignore errors if user or database exists
174
# drop public schema *as admin* because it starts out owned by postgres
175
createPlpgsql := echo 'CREATE PROCEDURAL LANGUAGE plpgsql;'\
176
|bin/db_dump_localize|$(psqlAsBien)
177

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

    
183
reinstall_db: _always rm_db db ;
184

    
185
#### public schema
186

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

    
193
schemas/uninstall: _always
194
	@$(confirmRmDb)
195
	echo $(rmPublicSchema)|$(psqlAsBien)
196

    
197
schemas/rotate: _always schemas/rotate-only schemas/install ;
198
schemas/rotate-only: _always
199
	echo 'ALTER SCHEMA public RENAME TO "public.$(date)";'|$(psqlAsBien)
200

    
201
empty_db: _always schemas/vegbien_empty.sql
202
	@$(confirmRmDb)
203
	$(psqlAsBien) <schemas/vegbien_empty.sql
204

    
205
##### MySQL
206

    
207
mysql: _always $(call forOs,mysql) mysql_user
208
	@$(done)
209

    
210
rm_mysql: _always rm_mysql_user ;
211

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

    
217
mysql-Darwin: _always
218
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
219
	@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
220
	@echo 'Extract the archive'
221
	@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
222
	@echo 'Run ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
223
	@echo 'Run ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
224
	@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
225
	@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
226
	@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
227
	@echo 'Change to the directory of the extracted files'
228
	@echo 'Run python setup.py clean'
229
	@echo 'Run python setup.py build'
230
	@echo 'Run sudo python setup.py install'
231
	@echo "Run python -c 'import MySQLdb'"
232
	@$(wait)
233

    
234
mysql-: _always # other OSes
235

    
236
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
237

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

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

    
248
##### Datasources
249

    
250
inputs: _always inputs/all ;
251

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

    
258
verify: _always inputs/verify
259
	@$(done)
260

    
261
test: _always inputs/test
262
	@$(done)
263

    
264
##### Testing
265

    
266
test-all: _always remake test missing_mappings
267
	@$(done)
268

    
269
##### Subdir forwarding
270

    
271
# Must come last so overridden targets are not forwarded
272

    
273
define subdirTargets
274
$(subdir): _always
275
	+$$(subMake)
276

    
277
$(subdir)%: _always
278
	+$$(subMake)
279
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
280

    
281
$(subdir)reinstall: _always $(subdir)uninstall $(subdir)install ;
282
endef
283
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
284

    
285
Makefile: ;
286

    
287
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
288

    
289
#### Maps validation
290

    
291
# Must come after Subdir forwarding to avoid infinite recursion, for some reason
292

    
293
missing_mappings: _always missing_join_mappings missing_input_mappings ;
294

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