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
bin := bin
12

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

    
17
# System
18
date = $(shell date +"%Y-%m-%d-%H-%M-%S")
19

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

    
26
# User interaction
27

    
28
done = echo; echo $(emph)"Finished $@"$(endEmph); echo
29

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

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

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

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

    
43
##### Environment
44

    
45
export PGOPTIONS = --client-min-messages=WARNING
46

    
47
##### General targets
48

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

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

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

    
63
%/reinstall: _always %/uninstall %/install ;
64

    
65
##### Installation
66

    
67
install: _always core mysql inputs/download inputs/install test
68
	@$(done)
69

    
70
uninstall: _always inputs/uninstall rm_mysql rm_core ;
71

    
72
reinstall: _always uninstall install ;
73

    
74
##### Core: VegBIEN DB and dependencies
75

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

    
79
rm_core: _always rm_db ;
80

    
81
reinstall_core: _always rm_core core ;
82

    
83
##### Python
84

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

    
96
python-Darwin: _always
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
postgres-Linux: _always
143
	-sudo apt-get install postgresql
144
	-sudo apt-get install postgresql-plpython-"$$(env minor=1 bin/pg_version)"
145
	-sudo apt-get install libpq-dev
146
	-sudo apt-get install phppgadmin
147
	$(editPhppgadminApacheConf)
148
	$(editApacheConfForPhppgadmin)
149
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
150
	-sudo apache2ctl restart
151
	-sudo pip install psycopg2
152
# run pg_version inline because it needs postgresql to be installed first
153
# ignore errors if conf files already edited
154

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

    
162
postgres-: _always ; # other OSes
163

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

    
171
##### VegBIEN DB
172

    
173
#### DB and bien user
174

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

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

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

    
183
db: mk_db rm_initial_public schemas/install _always ;
184

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

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

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

    
202
reinstall_db: _always rm_db db ;
203

    
204
#### Schemas
205

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

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

    
212
### public
213

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

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

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

    
228
### py_functions
229

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

    
234
### Others
235

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

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

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

    
246
##### MySQL
247

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

    
251
rm_mysql: _always rm_mysql_user ;
252

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

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

    
275
mysql-: _always # other OSes
276

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

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

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

    
289
##### Datasources
290

    
291
inputs: _always inputs/all ;
292

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

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

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

    
304
##### Testing
305

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

    
309
##### Subdir forwarding
310

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

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

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

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

    
325
Makefile: ;
326

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

    
329
#### Maps validation
330

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

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