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 inputs/uninstall 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 python-dev
83
	-sudo apt-get install python-dateutil
84
	-sudo apt-get install python-parallel
85
	-sudo apt-get install python-profiler
86
	-sudo apt-get install python-pip
87
	-sudo apt-get install pymetrics
88
	-sudo pip install ordereddict
89
	-sudo pip install pp
90

    
91
python-Darwin: _always
92
	@echo $(emph)'Installing Python 3.2 on Mac OS X:'$(endEmph)
93
	@echo 'Download it: http://www.python.org/ftp/python/3.2.3/python-3.2.3-macosx10.6.dmg'
94
	@echo 'Open the disk image'
95
	@echo 'Run the installer in it'
96
	@$(wait)
97
	@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
	@echo $(emph)'Installing pip on Mac OS X:'$(endEmph)
106
	@echo 'Follow the instructions at http://www.pip-installer.org/en/latest/installing.html'
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
asAdmin := sudo -E -u postgres
143
pgVersion = "$$(env minor=1 bin/pg_version)"
144

    
145
postgres-Linux: _always
146
	-sudo apt-get install postgresql
147
	-sudo apt-get install postgresql-plpython-$(pgVersion)
148
	-sudo apt-get install libpq-dev
149
	-sudo apt-get install postgresql-$(pgVersion)-postgis
150
	-sudo apt-get install phppgadmin
151
	$(editPhppgadminApacheConf)
152
	$(editApacheConfForPhppgadmin)
153
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
154
	-sudo apache2ctl restart
155
	-sudo pip install psycopg2
156
# run pg_version inline because it needs postgresql to be installed first
157
# ignore errors if conf files already edited
158

    
159
macPostgresDir := /Library/PostgreSQL/9.1
160
define macUsePostgresLib
161
sudo mv $(libDest) $(libDest).old||sudo rm -f $(libDest)
162
sudo ln -s $(macPostgresDir)/lib/$(1) $(libDest)
163

    
164
endef
165
libDest = /usr/lib/$(1)
166

    
167
macPostgresLibs := libcrypto libssl
168
macPostgresLibs := $(macPostgresLibs) $(macPostgresLibs:%=%.1.0.0)
169
macPostgresLibs := $(macPostgresLibs:%=%.dylib)
170

    
171
pg_ctl-Darwin := $(asAdmin) pg_ctl -D $(macPostgresDir)/data\
172
-l $(macPostgresDir)/postgres.log
173

    
174
postgres-Darwin: _always
175
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
176
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
177
	@echo 'Open the disk image'
178
	@echo 'Run the installer in it'
179
	@$(wait)
180
	$(foreach lib,$(macPostgresLibs),$(call macUsePostgresLib,$(lib)))
181
	$(asAdmin) cp schemas/postgresql.Mac.conf $(macPostgresDir)/data/postgresql.conf
182
	$(pg_ctl-Darwin) restart
183

    
184
postgres-: _always ; # other OSes
185

    
186
psqlOpts := --set ON_ERROR_STOP=1 --quiet
187
psqlAsAdmin := $(asAdmin) psql $(psqlOpts)
188
    # -E preserves env vars so PGOPTIONS is passed to psql
189
psqlAsAdminVegbien := $(psqlAsAdmin) vegbien
190
psqlAsBien := bin/psql_vegbien $(psqlOpts)
191
bienPassword := $(shell cat config/bien_password)
192

    
193
##### VegBIEN DB
194

    
195
#### DB and bien user
196

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

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

    
203
rmPublicSchema := $(call rmSchemaCmd,public)
204

    
205
db: mk_db rm_initial_public _always ;
206

    
207
mk_db: _always
208
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
209
	-echo "CREATE DATABASE vegbien WITH OWNER bien TEMPLATE template1 \
210
ENCODING 'UTF8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';"|$(psqlAsAdmin)
211
	-$(asAdmin) createlang plpython3u vegbien
212
# ignore errors if user/database/etc. exists
213

    
214
rm_initial_public: _always
215
	@$(confirmRmPublicSchema)
216
	echo $(rmPublicSchema)|$(psqlAsAdminVegbien)
217
# drop public schema *as admin* because it starts out owned by postgres
218

    
219
rm_db: _always
220
	@$(confirmRmDb)
221
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
222
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
223

    
224
reinstall_db: _always rm_db db ;
225

    
226
#### Schemas
227

    
228
schemas := temp functions py_functions public
229
schemas/install: $(schemas:%=schemas/%/install) ;
230

    
231
schemasReversed := public py_functions functions temp
232
schemas/uninstall: $(schemasReversed:%=schemas/%/uninstall) ;
233

    
234
psqlNoSearchPath := env no_search_path=1 $(psqlAsBien)
235

    
236
### public
237

    
238
schemas/public/install: schemas/vegbien.sql _always
239
	-echo $(call mkSchemaCmd,public)|$(psqlNoSearchPath)
240
	<$< $(psqlNoSearchPath)
241
# ignore errors if schema exists
242
# public schema will be owned by bien
243
# don't include public in the search_path
244

    
245
schemas/public/uninstall: _always
246
	@$(confirmRmPublicSchema)
247
	echo $(rmPublicSchema)|$(psqlNoSearchPath)
248

    
249
schemas/rename/%: _always
250
	echo 'ALTER SCHEMA public RENAME TO "$*";'|$(psqlNoSearchPath)
251
	$(MAKE) schemas/public/install
252

    
253
schemas/rotate: _always schemas/rename/public.$(version) ;
254

    
255
### py_functions
256

    
257
schemas/py_functions/install: schemas/py_functions.sql _always
258
	-<$< env public= $(psqlAsAdminVegbien)
259
# ignore errors if schema exists
260

    
261
### Others
262

    
263
schemas/%/install: schemas/%.sql _always
264
	-<$< $(psqlNoSearchPath)
265
# ignore errors if schema exists
266

    
267
schemas/%/uninstall: _always
268
	echo $(call rmSchemaCmd,$*)|$(psqlNoSearchPath)
269

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

    
273
##### MySQL
274

    
275
mysql: _always $(call forOs,mysql) mysql_user
276
	@$(done)
277

    
278
rm_mysql: _always rm_mysql_user ;
279

    
280
mysql-Linux: _always
281
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
282
	@$(wait)
283
	-sudo apt-get install mysql-server mysql-client python-mysqldb
284

    
285
mysql-Darwin: _always
286
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
287
	@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
288
	@echo 'Extract the archive'
289
	@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
290
	@echo 'Run ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
291
	@echo 'Run ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
292
	@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
293
	@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
294
	@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
295
	@echo 'Change to the directory of the extracted files'
296
	@echo 'Run python setup.py clean'
297
	@echo 'Run python setup.py build'
298
	@echo 'Run sudo python setup.py install'
299
	@echo "Run python -c 'import MySQLdb'"
300
	@$(wait)
301

    
302
mysql-: _always # other OSes
303

    
304
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
305

    
306
mysql_user: _always
307
	-$($@_cmd)
308
# ignore errors if user exists
309
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
310
'$(bienPassword)';"|$(mysqlAsRoot)
311

    
312
rm_mysql_user: _always
313
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
314
# ignore errors if user exists
315

    
316
##### Datasources
317

    
318
inputs: _always inputs/all ;
319

    
320
inputs/install: _always
321
	echo '. bin/reinstall_all; wait'|bash -s
322

    
323
import: _always import-msg inputs/import ;
324
import-msg: _always
325
	@echo $(emph)"To import all inputs at once:"$(endEmph) . bin/import_all
326
	@echo
327

    
328
verify: _always inputs/verify
329
	@$(done)
330

    
331
test: _always inputs/test
332
	@$(done)
333

    
334
##### Testing
335

    
336
test-all: _always remake test missing_mappings
337
	@$(done)
338

    
339
##### Subdir forwarding
340

    
341
# Must come last so overridden targets are not forwarded
342

    
343
define subdirTargets
344
$(subdir): _always
345
	+$$(subMake)
346

    
347
$(subdir)%: _always
348
	+$$(subMake)
349
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
350

    
351
$(subdir)reinstall: _always $(subdir)uninstall $(subdir)install ;
352
endef
353
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
354

    
355
Makefile: ;
356

    
357
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
358

    
359
#### Maps validation
360

    
361
termsSubdirs := $(call no/,$(call wildcard/,inputs/*/))
362

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