Project

General

Profile

1
##### Vars/functions
2

    
3
# Make
4
SHELL := /bin/bash
5
pathParts = $(shell path="$(1)"; echo "$${path%%/*}" "$${path\#*/}")
6
topDir = $(word 1,$(pathParts))
7
subPath = $(word 2,$(pathParts))
8
subMake = $(MAKE) $(call subPath,$@) --directory=$(call topDir,$@)
9

    
10
# OS
11
os := $(shell uname)
12
forOs = $(patsubst %,%-$(filter Linux Darwin,$(os)),$(1))
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) (y/n)"$(endEmph) REPLY; \
27
test "$$REPLY" = y && echo t),,$(error Aborting))
28

    
29
# File editing
30
sudoAppend = echo $$'$(2)'|sudo tee -a $(1) >/dev/null
31

    
32
##### Environment
33

    
34
export PGOPTIONS = --client-min-messages=WARNING
35

    
36
##### General targets
37

    
38
all:
39

    
40
.SUFFIXES: # turn off built-in suffix rules
41
.SECONDARY: # don't automatically delete intermediate files
42
.DELETE_ON_ERROR: # delete target if recipe fails
43

    
44
_always:
45
.PHONY: _always
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
##### Installation
62

    
63
install: _always core mysql inputs/install test
64
	@$(done)
65

    
66
uninstall: _always inputs/uninstall rm_mysql rm_core ;
67

    
68
reinstall: _always uninstall install ;
69

    
70
##### Core: VegBIEN DB and dependencies
71

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

    
75
rm_core: _always rm_db ;
76

    
77
reinstall_core: _always rm_core core ;
78

    
79
##### Python
80

    
81
python-Linux: _always
82
	-sudo apt-get install python python-dev python-dateutil python-parallel \
83
python-profiler python-pip pymetrics
84
	-sudo pip install pp
85

    
86
python-Darwin: _always
87
	@echo $(emph)'Installing python-dateutil on Mac OS X:'$(endEmph)
88
	@echo 'Download it: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
89
	@echo 'Extract the archive'
90
	@echo 'Change to the directory of the extracted files'
91
	@echo 'Run python setup.py build'
92
	@echo 'Run sudo python setup.py install'
93
	@echo "Run python -c 'import dateutil'"
94
	@$(wait)
95

    
96
##### PHP
97

    
98
php-Linux: _always
99
	-sudo apt-get install php-http-request
100

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

    
114
##### PostgreSQL
115

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

    
119
apacheConf := /etc/apache2/apache2.conf
120
apacheConfPhppgadminLine := Include /etc/phppgadmin/apache.conf
121
editApacheConfForPhppgadmin =\
122
$(if $(shell grep -F "$(apacheConfPhppgadminLine)" $(apacheConf)),,\
123
$(call sudoAppend,$(apacheConf),\n$(apacheConfPhppgadminLine)))
124

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

    
129
postgres-Linux: _always
130
	-sudo apt-get install postgresql libpq-dev phppgadmin
131
	$(editPhppgadminApacheConf)
132
	$(editApacheConfForPhppgadmin)
133
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
134
	-sudo apache2ctl restart
135
	-sudo pip install psycopg2
136
# ignore errors if conf files already edited
137

    
138
postgres-Darwin: _always
139
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
140
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
141
	@echo 'Open the disk image'
142
	@echo 'Run the installer in it'
143
	@$(wait)
144

    
145
postgres-: _always ; # other OSes
146

    
147
psqlOpts := --set ON_ERROR_STOP=1 --quiet
148
psqlAsAdmin := sudo -u postgres psql $(psqlOpts)
149
psqlAsBien := bin/psql_vegbien $(psqlOpts)
150
bienPassword := $(shell cat config/bien_password)
151

    
152
##### VegBIEN DB
153

    
154
#### DB and bien user
155

    
156
confirmRmDb = $(call confirm,WARNING:\
157
This will delete your VegBIEN DB if it exists! Continue?)
158
rmPublicSchema := 'DROP SCHEMA IF EXISTS public CASCADE;'
159

    
160
db: mk_db schemas/install _always ;
161

    
162
mk_db: _always
163
	@$(confirmRmDb)
164
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
165
	-echo "CREATE DATABASE vegbien WITH OWNER bien TEMPLATE template0 \
166
ENCODING 'UTF8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';"|$(psqlAsAdmin)
167
	echo $(rmPublicSchema)|env $(psqlAsAdmin) vegbien
168
# ignore errors if user or database exists
169
# drop public schema *as admin* because it starts out owned by postgres
170

    
171
rm_db: _always
172
	@$(confirmRmDb)
173
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
174
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
175

    
176
reinstall_db: _always rm_db db ;
177

    
178
#### public schema
179

    
180
schemas/install: schemas/vegbien.sql _always
181
	-echo "CREATE SCHEMA public;"|$(psqlAsBien)
182
	<$< bin/db_dump_localize|$(psqlAsBien)
183
# ignore errors if schema exists
184
# public schema will be owned by bien
185

    
186
schemas/uninstall: _always
187
	@$(confirmRmDb)
188
	echo $(rmPublicSchema)|$(psqlAsBien)
189

    
190
empty_db: _always schemas/vegbien_empty.sql
191
	@$(confirmRmDb)
192
	$(psqlAsBien) <schemas/vegbien_empty.sql
193

    
194
##### MySQL
195

    
196
mysql: _always $(call forOs,mysql) mysql_user
197
	@$(done)
198

    
199
rm_mysql: _always rm_mysql_user ;
200

    
201
mysql-Linux: _always
202
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
203
	@$(wait)
204
	-sudo apt-get install mysql-server mysql-client python-mysqldb
205

    
206
mysql-Darwin: _always
207
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
208
	@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
209
	@echo 'Extract the archive'
210
	@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
211
	@echo 'Run ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
212
	@echo 'Run ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
213
	@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
214
	@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
215
	@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
216
	@echo 'Change to the directory of the extracted files'
217
	@echo 'Run python setup.py clean'
218
	@echo 'Run python setup.py build'
219
	@echo 'Run sudo python setup.py install'
220
	@echo "Run python -c 'import MySQLdb'"
221
	@$(wait)
222

    
223
mysql-: _always # other OSes
224

    
225
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
226

    
227
mysql_user: _always
228
	-$($@_cmd)
229
# ignore errors if user exists
230
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
231
'$(bienPassword)';"|$(mysqlAsRoot)
232

    
233
rm_mysql_user: _always
234
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
235
# ignore errors if user exists
236

    
237
##### Datasources
238

    
239
inputs: _always inputs/all ;
240

    
241
import: _always import-msg inputs/import ;
242
import-msg: _always
243
	@echo $(emph)"To import all inputs at once:"$(endEmph) . bin/import_all
244
	@echo
245
	@$(wait)
246

    
247
verify: _always inputs/verify
248
	@$(done)
249

    
250
test: _always inputs/test
251
	@$(done)
252

    
253
#### Maps validation
254

    
255
missing_mappings: _always missing_join_mappings missing_input_mappings ;
256

    
257
missing_%_mappings: _always # stem is one of join|input
258
	@echo $(emph)"Missing $* mappings:"$(endEmph)
259
	@+$(MAKE) inputs/$@|grep -v -e ' Missing '\
260
$(if $(filter join,$*), -e '^make '|sort|uniq)
261

    
262
##### Testing
263

    
264
test-all: _always remake test missing_mappings
265
	@$(done)
266

    
267
##### Subdir forwarding
268

    
269
# Must come last so overridden targets are not forwarded
270

    
271
define subdirTargets
272
$(subdir): _always
273
	+$$(subMake)
274

    
275
$(subdir)%: _always
276
	+$$(subMake)
277
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
278

    
279
$(subdir)reinstall: _always $(subdir)uninstall $(subdir)install ;
280
endef
281
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
282

    
283
Makefile: ;
284

    
285
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
(1-1/3)