Project

General

Profile

1
include lib/common.Makefile
2

    
3

    
4
##### Vars/functions
5

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

    
10
# Paths
11
root := .
12
bin := bin
13

    
14
# OS
15
os := $(shell uname)
16
forOs = $(patsubst %,%-$(filter Linux Darwin,$(os)),$(1))
17

    
18
# Terminal
19
esc := '['
20
reset := $(esc)'0m'
21
emph := $(esc)'7m '
22
endEmph := ' '$(reset)
23

    
24
# User interaction
25

    
26
done = echo; echo $(emph)"Finished $@"$(endEmph); echo
27

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

    
30
confirm = $(if $(shell read -p $(emph)"$(1)"$(endEmph)$$'$(if\
31
$(2),\n$(2))\nContinue? (y/n) ' REPLY; test "$$REPLY" = y && echo t),,\
32
$(error Aborting))
33

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

    
37
# DB
38
mkSchemaCmd = 'CREATE SCHEMA $(1);'
39
rmSchemaCmd = 'DROP SCHEMA IF EXISTS "$(1)" CASCADE;'
40

    
41
##### Environment
42

    
43
export PGOPTIONS = --client-min-messages=WARNING
44

    
45
##### General targets
46

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

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

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

    
61
%/reinstall: _always %/uninstall %/install ;
62

    
63
##### Installation
64

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

    
68
uninstall: _always inputs/uninstall rm_mysql rm_core ;
69

    
70
reinstall: _always uninstall install ;
71

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

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

    
77
rm_core: _always rm_db ;
78

    
79
reinstall_core: _always rm_core core ;
80

    
81
##### Python
82

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

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

    
107
##### PHP
108

    
109
php-Linux: _always
110
	-sudo apt-get install php-http-request
111

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

    
125
##### PostgreSQL
126

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

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

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

    
140
pgVersion = "$$(env minor=1 bin/pg_version)"
141

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

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

    
163
postgres-: _always ; # other OSes
164

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

    
172
##### VegBIEN DB
173

    
174
#### DB and bien user
175

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

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

    
182
rmPublicSchema := $(call rmSchemaCmd,public)
183

    
184
db: mk_db rm_initial_public schemas/install _always ;
185

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

    
193
rm_initial_public: _always
194
	@$(confirmRmPublicSchema)
195
	echo $(rmPublicSchema)|$(psqlAsAdminVegbien)
196
# drop public schema *as admin* because it starts out owned by postgres
197

    
198
rm_db: _always
199
	@$(confirmRmDb)
200
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
201
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
202

    
203
reinstall_db: _always rm_db db ;
204

    
205
#### Schemas
206

    
207
schemas := temp functions py_functions public
208
schemas/install: $(schemas:%=schemas/%/install) ;
209

    
210
schemasReversed := public py_functions functions temp
211
schemas/uninstall: $(schemasReversed:%=schemas/%/uninstall) ;
212

    
213
### public
214

    
215
schemas/public/install: schemas/vegbien.sql _always
216
	-echo $(call mkSchemaCmd,public)|$(psqlAsBien)
217
	<$< $(psqlAsBien)
218
# ignore errors if schema exists
219
# public schema will be owned by bien
220

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

    
225
schemas/rotate: _always schemas/rotate-only schemas/public/install ;
226
schemas/rotate-only: _always
227
	echo 'ALTER SCHEMA public RENAME TO "public.$(version)";'|$(psqlAsBien)
228

    
229
### py_functions
230

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

    
235
### Others
236

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

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

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

    
247
##### MySQL
248

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

    
252
rm_mysql: _always rm_mysql_user ;
253

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

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

    
276
mysql-: _always # other OSes
277

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

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

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

    
290
##### Datasources
291

    
292
inputs: _always inputs/all ;
293

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

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

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

    
305
##### Testing
306

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

    
310
##### Subdir forwarding
311

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

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

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

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

    
326
Makefile: ;
327

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

    
330
#### Maps validation
331

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

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