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
install: _always core mysql inputs/download inputs/install test
62
	@$(done)
63

    
64
uninstall: _always inputs/uninstall rm_mysql rm_core ;
65

    
66
reinstall: _always uninstall install ;
67

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

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

    
73
rm_core: _always rm_db ;
74

    
75
reinstall_core: _always rm_core core ;
76

    
77
##### Python
78

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

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

    
103
##### PHP
104

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

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

    
121
##### PostgreSQL
122

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

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

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

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

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

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

    
159
postgres-: _always ; # other OSes
160

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

    
168
##### VegBIEN DB
169

    
170
#### DB and bien user
171

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

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

    
178
rmPublicSchema := $(call rmSchemaCmd,public)
179

    
180
db: mk_db rm_initial_public inputs/.TNRS/install schemas/install _always ;
181

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

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

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

    
199
reinstall_db: _always rm_db db ;
200

    
201
#### Schemas
202

    
203
schemas := temp functions py_functions public
204
schemas/install: $(schemas:%=schemas/%/install) ;
205

    
206
schemasReversed := public py_functions functions temp
207
schemas/uninstall: $(schemasReversed:%=schemas/%/uninstall) ;
208

    
209
psqlNoSearchPath := env no_search_path=1 $(psqlAsBien)
210

    
211
### public
212

    
213
schemas/public/install: schemas/vegbien.sql _always
214
	-echo $(call mkSchemaCmd,public)|$(psqlNoSearchPath)
215
	<$< $(psqlAsBien)
216
# ignore errors if schema exists
217
# public schema will be owned by bien
218
# don't include public in the search_path
219

    
220
schemas/public/uninstall: _always
221
	@$(confirmRmPublicSchema)
222
	echo $(rmPublicSchema)|$(psqlNoSearchPath)
223

    
224
schemas/rename/%: _always
225
	echo 'ALTER SCHEMA public RENAME TO "$*";'|$(psqlNoSearchPath)
226
	$(MAKE) schemas/public/install
227

    
228
schemas/rotate: _always schemas/rename/public.$(version) ;
229

    
230
### py_functions
231

    
232
schemas/py_functions/install: schemas/py_functions.sql _always
233
	-<$< env public= $(psqlAsAdminVegbien)
234
# ignore errors if schema exists
235

    
236
### Others
237

    
238
schemas/%/install: schemas/%.sql _always
239
	-<$< $(psqlNoSearchPath)
240
# ignore errors if schema exists
241

    
242
schemas/%/uninstall: _always
243
	echo $(call rmSchemaCmd,$*)|$(psqlNoSearchPath)
244

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

    
248
##### MySQL
249

    
250
mysql: _always $(call forOs,mysql) mysql_user
251
	@$(done)
252

    
253
rm_mysql: _always rm_mysql_user ;
254

    
255
mysql-Linux: _always
256
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
257
	@$(wait)
258
	-sudo apt-get install mysql-server mysql-client python-mysqldb
259

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

    
277
mysql-: _always # other OSes
278

    
279
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
280

    
281
mysql_user: _always
282
	-$($@_cmd)
283
# ignore errors if user exists
284
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
285
'$(bienPassword)';"|$(mysqlAsRoot)
286

    
287
rm_mysql_user: _always
288
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
289
# ignore errors if user exists
290

    
291
##### Datasources
292

    
293
inputs: _always inputs/all ;
294

    
295
import: _always import-msg inputs/import ;
296
import-msg: _always
297
	@echo $(emph)"To import all inputs at once:"$(endEmph) . bin/import_all
298
	@echo
299

    
300
verify: _always inputs/verify
301
	@$(done)
302

    
303
test: _always inputs/test
304
	@$(done)
305

    
306
##### Testing
307

    
308
test-all: _always remake test missing_mappings
309
	@$(done)
310

    
311
##### Subdir forwarding
312

    
313
# Must come last so overridden targets are not forwarded
314

    
315
define subdirTargets
316
$(subdir): _always
317
	+$$(subMake)
318

    
319
$(subdir)%: _always
320
	+$$(subMake)
321
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
322

    
323
$(subdir)reinstall: _always $(subdir)uninstall $(subdir)install ;
324
endef
325
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
326

    
327
Makefile: ;
328

    
329
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
330

    
331
#### Maps validation
332

    
333
termsSubdirs := $(call no/,$(call wildcard/,inputs/*/))
334

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