Project

General

Profile

1
root := .
2
include lib/common.Makefile
3

    
4

    
5
##### Vars/functions
6

    
7
# Make
8
SHELL := /bin/bash
9
subMake = $(MAKE) $(call subPath,$@) --directory=$(call topDir,$@)
10

    
11
# Paths
12
bin := bin
13

    
14
# Terminal
15
esc := '['
16
reset := $(esc)'0m'
17
emph := $(esc)'7m '
18
endEmph := ' '$(reset)
19

    
20
# User interaction
21

    
22
done = echo; echo $(emph)"Finished $@"$(endEmph); echo
23

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

    
26
confirm = $(if $(shell read -p $(emph)"$(1)"$(endEmph)$$'$(if\
27
$(2),\n$(2))\nContinue? (y/n) ' REPLY; test "$$REPLY" = y && echo t),,\
28
$(error Aborting))
29

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

    
33
# DB
34
mkSchemaCmd = 'CREATE SCHEMA $(1);'
35
rmSchemaCmd = 'DROP SCHEMA IF EXISTS "$(1)" CASCADE;'
36

    
37
##### Environment
38

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

    
41
##### General targets
42

    
43
remake: _always clean
44
	$(MAKE)
45
# re-run make so that cache of existing files is reset
46

    
47
%/remake: _always
48
	$(MAKE) $(@D)/clean
49
	$(MAKE) $(@D)/
50
# re-run make so that cache of existing files is reset
51

    
52
%-remake: _always
53
	rm -f $*
54
	$(MAKE) $*
55
# re-run make so that cache of existing files is reset
56

    
57
%/reinstall: _always %/uninstall %/install ;
58

    
59
##### Installation
60

    
61
# public must be installed *after* inputs because some views depend on inputs
62
install: _always core mysql inputs/download inputs/install schemas/install test
63
	@$(done)
64

    
65
uninstall: _always rm_mysql rm_core ;
66

    
67
reinstall: _always uninstall install ;
68

    
69
##### Core: VegBIEN DB and dependencies
70

    
71
core: _always $(call forOs,python php postgres) db
72
	@$(done)
73

    
74
rm_core: _always rm_db ;
75

    
76
reinstall_core: _always rm_core core ;
77

    
78
##### Python
79

    
80
python-Linux: _always
81
	-sudo apt-get install python
82
	-sudo apt-get install python3
83
	-sudo apt-get install python-dev
84
	-sudo apt-get install python-dateutil
85
	-sudo apt-get install python-parallel
86
	-sudo apt-get install python-profiler
87
	-sudo apt-get install python-pip
88
	-sudo apt-get install pymetrics
89
	-sudo pip install ordereddict
90
	-sudo pip install pp
91

    
92
python-Darwin: _always
93
	@echo $(emph)'Installing Python 3.2 on Mac OS X:'$(endEmph)
94
	@echo 'Download it: http://www.python.org/ftp/python/3.2.3/python-3.2.3-macosx10.6.dmg'
95
	@echo 'Open the disk image'
96
	@echo 'Run the installer in it'
97
	@$(wait)
98
	@echo $(emph)'Installing python-dateutil on Mac OS X:'$(endEmph)
99
	@echo 'Download it: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
100
	@echo 'Extract the archive'
101
	@echo 'Change to the directory of the extracted files'
102
	@echo 'Run python setup.py build'
103
	@echo 'Run sudo python setup.py install'
104
	@echo "Run python -c 'import dateutil'"
105
	@$(wait)
106
	@echo $(emph)'Installing pip on Mac OS X:'$(endEmph)
107
	@echo 'Follow the instructions at http://www.pip-installer.org/en/latest/installing.html'
108
	@$(wait)
109

    
110
##### PHP
111

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

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

    
128
##### PostgreSQL
129

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

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

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

    
143
asAdmin := sudo -E -u postgres
144
pgVersion = "$$(env minor=1 bin/pg_version)"
145

    
146
linuxPostgresDir := /etc/postgresql/9.1/main
147
define pg_ctl-Linux
148
$(asAdmin) /etc/init.d/postgresql $(1)
149
sleep 2
150
endef
151

    
152
macPostgresDir := /Library/PostgreSQL/9.1
153
define macUsePostgresLib
154
sudo mv $(libDest) $(libDest).old||sudo rm -f $(libDest)
155
sudo ln -s $(macPostgresDir)/lib/$(1) $(libDest)
156

    
157
endef
158
libDest = /usr/lib/$(1)
159

    
160
macPostgresLibs := libcrypto libssl
161
macPostgresLibs := $(macPostgresLibs) $(macPostgresLibs:%=%.1.0.0)
162
macPostgresLibs := $(macPostgresLibs:%=%.dylib)
163

    
164
define pg_ctl-Darwin
165
$(asAdmin) pg_ctl -D $(macPostgresDir)/data -l $(macPostgresDir)/postgres.log $(1)
166
sleep 2
167
endef
168

    
169
define postgresReload-Linux
170
$(asAdmin) cp --preserve=timestamps schemas/postgresql.conf $(linuxPostgresDir)/postgresql.conf
171
$(asAdmin) cp --preserve=timestamps schemas/pg_hba.conf $(linuxPostgresDir)/pg_hba.conf
172
$(call pg_ctl-Linux,restart)
173

    
174
endef
175

    
176
define postgresReload-Darwin
177
$(asAdmin) cp schemas/postgresql.Mac.conf $(macPostgresDir)/data/postgresql.conf
178
$(asAdmin) cp schemas/pg_hba.Mac.conf $(macPostgresDir)/data/pg_hba.conf
179
$(call pg_ctl-Darwin,restart)
180

    
181
endef
182

    
183
postgres_reload: _always
184
	$($(call forOs,postgresReload))
185

    
186
postgres-Linux: _always
187
	-sudo apt-get install postgresql
188
	-sudo apt-get install postgresql-plpython3-$(pgVersion)
189
	-sudo apt-get install libpq-dev
190
	-sudo apt-get install postgresql-$(pgVersion)-postgis
191
	-sudo apt-get install phppgadmin
192
	$(editPhppgadminApacheConf)
193
	$(editApacheConfForPhppgadmin)
194
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
195
	-sudo apache2ctl restart
196
	-sudo pip install psycopg2
197
	$(postgresReload-Linux)
198
# run pg_version inline because it needs postgresql to be installed first
199
# ignore errors if conf files already edited
200

    
201
postgres-Darwin: _always
202
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
203
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
204
	@echo 'Open the disk image'
205
	@echo 'Run the installer in it'
206
	@$(wait)
207
	$(foreach lib,$(macPostgresLibs),$(call macUsePostgresLib,$(lib)))
208
	$(postgresReload-Darwin)
209

    
210
postgres-: _always ; # other OSes
211

    
212
psqlOpts := --set ON_ERROR_STOP=1 --quiet
213
psqlAsAdmin := $(asAdmin) psql $(psqlOpts)
214
    # -E preserves env vars so PGOPTIONS is passed to psql
215
psqlAsAdminVegbien := $(psqlAsAdmin) vegbien
216
psqlAsBien := bin/psql_vegbien $(psqlOpts)
217
bienPassword := $(shell cat config/bien_password)
218
bienReadPassword := $(shell cat config/bien_read_password)
219

    
220
##### VegBIEN DB
221

    
222
#### DB and users
223

    
224
confirmRmDb = $(call confirm,WARNING: This will delete your entire VegBIEN\
225
DB!,This includes all archived imports and staging tables.)
226

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

    
230
rmPublicSchema := $(call rmSchemaCmd,public)
231

    
232
db: mk_db rm_initial_public _always ;
233

    
234
mk_db: _always
235
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
236
	-echo "CREATE USER bien_read PASSWORD '$(bienReadPassword)' IN ROLE bien \
237
NOINHERIT;"|$(psqlAsAdmin)
238
	-echo "CREATE DATABASE vegbien WITH OWNER bien TEMPLATE template1 \
239
ENCODING 'UTF8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';"|$(psqlAsAdmin)
240
	-echo "CREATE EXTENSION hstore SCHEMA pg_catalog;"|$(psqlAsAdminVegbien)
241
	-$(asAdmin) createlang plpython3u vegbien
242
# ignore errors if user/database/etc. exists
243

    
244
rm_initial_public: _always
245
	@$(confirmRmPublicSchema)
246
	echo $(rmPublicSchema)|$(psqlAsAdminVegbien)
247
# drop public schema *as admin* because it starts out owned by postgres
248

    
249
rm_db: _always
250
	@$(confirmRmDb)
251
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
252
	echo "DROP USER IF EXISTS bien_read;"|$(psqlAsAdmin)
253
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
254

    
255
reinstall_db: _always rm_db db ;
256

    
257
#### Schemas
258

    
259
schemas := temp functions py_functions public
260
schemas/install: $(schemas:%=schemas/%/install) ;
261

    
262
schemasReversed := public py_functions functions temp
263
schemas/uninstall: $(schemasReversed:%=schemas/%/uninstall) ;
264

    
265
psqlNoSearchPath := env no_search_path=1 $(psqlAsBien)
266

    
267
### public
268

    
269
schemas/public/install: schemas/vegbien.sql _always
270
	-echo $(call mkSchemaCmd,public)|$(psqlNoSearchPath)
271
	<$< $(psqlNoSearchPath)
272
# ignore errors if schema exists
273
# public schema will be owned by bien
274
# don't include public in the search_path
275

    
276
schemas/public/uninstall: _always
277
	@$(confirmRmPublicSchema)
278
	echo $(rmPublicSchema)|$(psqlNoSearchPath)
279

    
280
schemas/rename/%: _always
281
	echo 'ALTER SCHEMA public RENAME TO "$*";'|$(psqlNoSearchPath)
282
	$(MAKE) schemas/public/install
283

    
284
schemas/rotate: _always schemas/rename/public.$(version) ;
285

    
286
### py_functions
287

    
288
schemas/py_functions/install: schemas/py_functions.sql _always
289
	-<$< env public= $(psqlAsAdminVegbien)
290
# ignore errors if schema exists
291

    
292
### Others
293

    
294
schemas/%/install: schemas/%.sql _always
295
	-<$< $(psqlNoSearchPath)
296
# ignore errors if schema exists
297

    
298
schemas/%/uninstall: _always
299
	echo $(call rmSchemaCmd,$*)|$(psqlNoSearchPath)
300

    
301
# Needed on Ubuntu 12.04 (also other Linuxes?) because %/reinstall is ignored.
302
schemas/temp/reinstall: _always schemas/temp/uninstall schemas/temp/install ;
303

    
304
##### MySQL
305

    
306
mysql: _always $(call forOs,mysql) mysql_user
307
	@$(done)
308

    
309
rm_mysql: _always rm_mysql_user ;
310

    
311
mysql-Linux: _always
312
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
313
	@$(wait)
314
	-sudo apt-get install mysql-server mysql-client python-mysqldb
315

    
316
mysql-Darwin: _always
317
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
318
	@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
319
	@echo 'Extract the archive'
320
	@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
321
	@echo 'Run ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
322
	@echo 'Run ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
323
	@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
324
	@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
325
	@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
326
	@echo 'Change to the directory of the extracted files'
327
	@echo 'Run python setup.py clean'
328
	@echo 'Run python setup.py build'
329
	@echo 'Run sudo python setup.py install'
330
	@echo "Run python -c 'import MySQLdb'"
331
	@$(wait)
332

    
333
mysql-: _always # other OSes
334

    
335
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
336

    
337
mysql_user: _always
338
	-$($@_cmd)
339
# ignore errors if user exists
340
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
341
'$(bienPassword)';"|$(mysqlAsRoot)
342

    
343
rm_mysql_user: _always
344
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
345
# ignore errors if user exists
346

    
347
##### Datasources
348

    
349
inputs: _always inputs/all ;
350

    
351
inputs/install: _always
352
	echo '. bin/reinstall_all; wait'|bash -s
353

    
354
import: _always import-msg inputs/import ;
355
import-msg: _always
356
	@echo $(emph)"To import all inputs at once:"$(endEmph) . bin/import_all
357
	@echo
358

    
359
verify: _always inputs/verify
360
	@$(done)
361

    
362
test: _always inputs/test
363
	@$(done)
364

    
365
##### Testing
366

    
367
test-all: _always remake test missing_mappings
368
	@$(done)
369

    
370
##### Subdir forwarding
371

    
372
# Must come last so overridden targets are not forwarded
373

    
374
define subdirTargets
375
$(subdir): _always
376
	+$$(subMake)
377

    
378
$(subdir)%: _always
379
	+$$(subMake)
380
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
381

    
382
$(subdir)reinstall: _always $(subdir)uninstall $(subdir)install ;
383
endef
384
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
385

    
386
Makefile: ;
387

    
388
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
389

    
390
#### Maps validation
391

    
392
termsSubdirs := $(call no/,$(call wildcard/,inputs/*/))
393

    
394
include lib/mappings.Makefile
(1-1/5)