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
##### Subdir forwarding
62

    
63
define subdirTargets
64
$(subdir): _always
65
	+$$(subMake)
66

    
67
$(subdir)%: _always
68
	+$$(subMake)
69
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
70
endef
71
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
72

    
73
Makefile: ;
74

    
75
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
76

    
77
##### Installation
78

    
79
install: _always core mysql inputs/install test
80
	@$(done)
81

    
82
uninstall: _always inputs/uninstall rm_mysql rm_core ;
83

    
84
reinstall: _always uninstall install ;
85

    
86
##### Core: VegBIEN DB and dependencies
87

    
88
core: _always $(call forOs,python php postgres) db
89
	@$(done)
90

    
91
rm_core: _always rm_db ;
92

    
93
reinstall_core: _always rm_core core ;
94

    
95
##### Python
96

    
97
python-Linux: _always
98
	-sudo apt-get install python python-dev python-dateutil python-parallel \
99
python-profiler python-pip pymetrics
100
	-sudo pip install pp
101

    
102
python-Darwin: _always
103
	@echo $(emph)'Installing python-dateutil on Mac OS X:'$(endEmph)
104
	@echo 'Download it: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
105
	@echo 'Extract the archive'
106
	@echo 'Change to the directory of the extracted files'
107
	@echo 'Run python setup.py build'
108
	@echo 'Run sudo python setup.py install'
109
	@echo "Run python -c 'import dateutil'"
110
	@$(wait)
111

    
112
##### PHP
113

    
114
php-Linux: _always
115
	-sudo apt-get install php-http-request
116

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

    
130
##### PostgreSQL
131

    
132
editPhppgadminApacheConf = echo $$'1\n,s/\# \(allow from all\)/\1/\nwq'|\
133
sudo ed --loose-exit-status --verbose /etc/phppgadmin/apache.conf
134

    
135
apacheConf := /etc/apache2/apache2.conf
136
apacheConfPhppgadminLine := Include /etc/phppgadmin/apache.conf
137
editApacheConfForPhppgadmin =\
138
$(if $(shell grep -F "$(apacheConfPhppgadminLine)" $(apacheConf)),,\
139
$(call sudoAppend,$(apacheConf),\n$(apacheConfPhppgadminLine)))
140

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

    
145
postgres-Linux: _always
146
	-sudo apt-get install postgresql libpq-dev phppgadmin
147
	$(editPhppgadminApacheConf)
148
	$(editApacheConfForPhppgadmin)
149
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
150
	-sudo apache2ctl restart
151
	-sudo pip install psycopg2
152
# ignore errors if conf files already edited
153

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

    
161
postgres-: _always ; # other OSes
162

    
163
psqlOpts := --set ON_ERROR_STOP=1 --quiet
164
psqlAsAdmin := sudo -u postgres psql $(psqlOpts)
165
psqlAsBien := bin/psql_vegbien $(psqlOpts)
166
bienPassword := $(shell cat config/bien_password)
167

    
168
##### VegBIEN DB
169

    
170
db: schemas/vegbien.sql _always
171
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
172
	-echo "CREATE DATABASE vegbien OWNER bien ENCODING 'UTF8' \
173
TEMPLATE template1;"|$(psqlAsAdmin)
174
	$(psqlAsBien) <$<
175
# ignore errors if user or database exists
176

    
177
confirmRmDb = $(call confirm,WARNING: This will delete your VegBIEN DB!\
178
Continue?)
179

    
180
rm_db: _always
181
	@$(confirmRmDb)
182
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
183
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
184

    
185
reinstall_db: _always rm_db db ;
186

    
187
empty_db: _always schemas/vegbien_empty.sql
188
	@$(confirmRmDb)
189
	$(psqlAsBien) <schemas/vegbien_empty.sql
190

    
191
##### MySQL
192

    
193
mysql: _always $(call forOs,mysql) mysql_user
194
	@$(done)
195

    
196
rm_mysql: _always rm_mysql_user ;
197

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

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

    
220
mysql-: _always # other OSes
221

    
222
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
223

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

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

    
234
##### Datasources
235

    
236
inputs: _always inputs/all ;
237

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

    
244
verify: _always inputs/verify
245
	@$(done)
246

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

    
250
#### Maps validation
251

    
252
missing_mappings: _always missing_join_mappings missing_input_mappings ;
253

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

    
259
##### Testing
260

    
261
test-all: _always remake test missing_mappings
262
	@$(done)
(1-1/3)