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

    
58
define subdirTargets
59
$(subdir): _always
60
	+$$(subMake)
61

    
62
$(subdir)%: _always
63
	+$$(subMake)
64
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
65
endef
66
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
67

    
68
Makefile: ;
69

    
70
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
71

    
72
##### Installation
73

    
74
install: _always core mysql inputs/install test
75
	@$(done)
76

    
77
uninstall: _always inputs/uninstall rm_mysql rm_core ;
78

    
79
reinstall: _always uninstall install ;
80

    
81
##### Core: VegBIEN DB and dependencies
82

    
83
core: _always $(call forOs,python php postgres) db
84
	@$(done)
85

    
86
rm_core: _always rm_db ;
87

    
88
reinstall_core: _always rm_core core ;
89

    
90
##### Python
91

    
92
python-Linux: _always
93
	-sudo apt-get install python python-dev python-dateutil python-profiler\
94
python-pip pymetrics
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

    
106
##### PHP
107

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

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

    
124
##### PostgreSQL
125

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

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

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

    
139
postgres-Linux: _always
140
	-sudo apt-get install postgresql libpq-dev phppgadmin
141
	$(editPhppgadminApacheConf)
142
	$(editApacheConfForPhppgadmin)
143
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
144
	-sudo apache2ctl restart
145
	-sudo pip install psycopg2
146
# ignore errors if conf files already edited
147

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

    
155
postgres-: _always ; # other OSes
156

    
157
psqlOpts := --set ON_ERROR_STOP=1 --quiet
158
psqlAsAdmin := sudo -u postgres psql $(psqlOpts)
159
psqlAsBien := bin/psql_vegbien $(psqlOpts)
160
bienPassword := $(shell cat config/bien_password)
161

    
162
##### VegBIEN DB
163

    
164
db: schemas/vegbien.sql _always
165
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
166
	-echo "CREATE DATABASE vegbien OWNER bien ENCODING 'UTF8' \
167
TEMPLATE template1;"|$(psqlAsAdmin)
168
	$(psqlAsBien) <$<
169
# ignore errors if user or database exists
170

    
171
confirmRmDb = $(call confirm,WARNING: This will delete your VegBIEN DB!\
172
Continue?)
173

    
174
rm_db: _always
175
	@$(confirmRmDb)
176
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
177
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
178

    
179
reinstall_db: _always rm_db db ;
180

    
181
empty_db: _always schemas/vegbien_empty.sql
182
	@$(confirmRmDb)
183
	$(psqlAsBien) <schemas/vegbien_empty.sql
184

    
185
##### MySQL
186

    
187
mysql: _always $(call forOs,mysql) mysql_user
188
	@$(done)
189

    
190
rm_mysql: _always rm_mysql_user ;
191

    
192
mysql-Linux: _always
193
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
194
	@$(wait)
195
	-sudo apt-get install mysql-server mysql-client python-mysqldb
196

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

    
214
mysql-: _always # other OSes
215

    
216
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
217

    
218
mysql_user: _always
219
	-$($@_cmd)
220
# ignore errors if user exists
221
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
222
'$(bienPassword)';"|$(mysqlAsRoot)
223

    
224
rm_mysql_user: _always
225
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
226
# ignore errors if user exists
227

    
228
##### Datasources
229

    
230
inputs: _always inputs/all ;
231

    
232
import: _always import-msg inputs/import ;
233
import-msg: _always
234
	@echo $(emph)"To import all inputs at once:"$(endEmph) . bin/import_all
235
	@echo
236
	@$(wait)
237

    
238
verify: _always inputs/verify
239
	@$(done)
240

    
241
test: _always inputs/test
242
	@$(done)
243

    
244
#### Maps validation
245

    
246
missing_mappings: _always missing_join_mappings missing_input_mappings ;
247

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

    
253
##### Testing
254

    
255
test-all: _always remake test missing_mappings
256
	@$(done)
(1-1/3)