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-dateutil on Mac OS X:'$(endEmph)
93
	@echo 'Download it: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
94
	@echo 'Extract the archive'
95
	@echo 'Change to the directory of the extracted files'
96
	@echo 'Run python setup.py build'
97
	@echo 'Run sudo python setup.py install'
98
	@echo "Run python -c 'import dateutil'"
99
	@$(wait)
100
	@echo $(emph)'Installing pip on Mac OS X:'$(endEmph)
101
	@echo 'Follow the instructions at http://www.pip-installer.org/en/latest/installing.html'
102
	@$(wait)
103

    
104
##### PHP
105

    
106
php-Linux: _always
107
	-sudo apt-get install php-http-request
108

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

    
122
##### PostgreSQL
123

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

    
127
apacheConf := /etc/apache2/apache2.conf
128
apacheConfPhppgadminLine := Include /etc/phppgadmin/apache.conf
129
editApacheConfForPhppgadmin =\
130
$(if $(shell grep -F "$(apacheConfPhppgadminLine)" $(apacheConf)),,\
131
$(call sudoAppend,$(apacheConf),\n$(apacheConfPhppgadminLine)))
132

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

    
137
pgVersion = "$$(env minor=1 bin/pg_version)"
138

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

    
153
define macUsePostgresLib
154
sudo mv $(libDest) $(libDest).old||sudo rm -f $(libDest)
155
sudo ln -s /Library/PostgreSQL/9.1/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
postgres-Darwin: _always
165
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
166
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
167
	@echo 'Open the disk image'
168
	@echo 'Run the installer in it'
169
	@$(wait)
170
	$(foreach lib,$(macPostgresLibs),$(call macUsePostgresLib,$(lib)))
171

    
172
postgres-: _always ; # other OSes
173

    
174
psqlOpts := --set ON_ERROR_STOP=1 --quiet
175
psqlAsAdmin := sudo -E -u postgres psql $(psqlOpts)
176
    # -E preserves env vars so PGOPTIONS is passed to psql
177
psqlAsAdminVegbien := $(psqlAsAdmin) vegbien
178
psqlAsBien := bin/psql_vegbien $(psqlOpts)
179
bienPassword := $(shell cat config/bien_password)
180

    
181
##### VegBIEN DB
182

    
183
#### DB and bien user
184

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

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

    
191
rmPublicSchema := $(call rmSchemaCmd,public)
192

    
193
db: mk_db rm_initial_public _always ;
194

    
195
mk_db: _always
196
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
197
	-echo "CREATE DATABASE vegbien WITH OWNER bien TEMPLATE template1 \
198
ENCODING 'UTF8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';"|$(psqlAsAdmin)
199
	-bin/postgres_vegbien createlang plpythonu
200
# ignore errors if user/database/etc. exists
201

    
202
rm_initial_public: _always
203
	@$(confirmRmPublicSchema)
204
	echo $(rmPublicSchema)|$(psqlAsAdminVegbien)
205
# drop public schema *as admin* because it starts out owned by postgres
206

    
207
rm_db: _always
208
	@$(confirmRmDb)
209
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
210
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
211

    
212
reinstall_db: _always rm_db db ;
213

    
214
#### Schemas
215

    
216
schemas := temp functions py_functions public
217
schemas/install: $(schemas:%=schemas/%/install) ;
218

    
219
schemasReversed := public py_functions functions temp
220
schemas/uninstall: $(schemasReversed:%=schemas/%/uninstall) ;
221

    
222
psqlNoSearchPath := env no_search_path=1 $(psqlAsBien)
223

    
224
### public
225

    
226
schemas/public/install: schemas/vegbien.sql _always
227
	-echo $(call mkSchemaCmd,public)|$(psqlNoSearchPath)
228
	<$< $(psqlAsBien)
229
# ignore errors if schema exists
230
# public schema will be owned by bien
231
# don't include public in the search_path
232

    
233
schemas/public/uninstall: _always
234
	@$(confirmRmPublicSchema)
235
	echo $(rmPublicSchema)|$(psqlNoSearchPath)
236

    
237
schemas/rename/%: _always
238
	echo 'ALTER SCHEMA public RENAME TO "$*";'|$(psqlNoSearchPath)
239
	$(MAKE) schemas/public/install
240

    
241
schemas/rotate: _always schemas/rename/public.$(version) ;
242

    
243
### py_functions
244

    
245
schemas/py_functions/install: schemas/py_functions.sql _always
246
	-<$< env public= $(psqlAsAdminVegbien)
247
# ignore errors if schema exists
248

    
249
### Others
250

    
251
schemas/%/install: schemas/%.sql _always
252
	-<$< $(psqlNoSearchPath)
253
# ignore errors if schema exists
254

    
255
schemas/%/uninstall: _always
256
	echo $(call rmSchemaCmd,$*)|$(psqlNoSearchPath)
257

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

    
261
##### MySQL
262

    
263
mysql: _always $(call forOs,mysql) mysql_user
264
	@$(done)
265

    
266
rm_mysql: _always rm_mysql_user ;
267

    
268
mysql-Linux: _always
269
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
270
	@$(wait)
271
	-sudo apt-get install mysql-server mysql-client python-mysqldb
272

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

    
290
mysql-: _always # other OSes
291

    
292
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
293

    
294
mysql_user: _always
295
	-$($@_cmd)
296
# ignore errors if user exists
297
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
298
'$(bienPassword)';"|$(mysqlAsRoot)
299

    
300
rm_mysql_user: _always
301
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
302
# ignore errors if user exists
303

    
304
##### Datasources
305

    
306
inputs: _always inputs/all ;
307

    
308
import: _always import-msg inputs/import ;
309
import-msg: _always
310
	@echo $(emph)"To import all inputs at once:"$(endEmph) . bin/import_all
311
	@echo
312

    
313
verify: _always inputs/verify
314
	@$(done)
315

    
316
test: _always inputs/test
317
	@$(done)
318

    
319
##### Testing
320

    
321
test-all: _always remake test missing_mappings
322
	@$(done)
323

    
324
##### Subdir forwarding
325

    
326
# Must come last so overridden targets are not forwarded
327

    
328
define subdirTargets
329
$(subdir): _always
330
	+$$(subMake)
331

    
332
$(subdir)%: _always
333
	+$$(subMake)
334
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
335

    
336
$(subdir)reinstall: _always $(subdir)uninstall $(subdir)install ;
337
endef
338
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
339

    
340
Makefile: ;
341

    
342
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
343

    
344
#### Maps validation
345

    
346
termsSubdirs := $(call no/,$(call wildcard/,inputs/*/))
347

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