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 = $(shell read -p $(emph)"$(1) (y/n)"$(endEmph) REPLY; \
27
test "$$REPLY" = y && echo t)
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
define rmDb
172
echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
173
echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
174
endef
175

    
176
rm_db: _always
177
	$(if $(call confirm,WARNING: This will delete your VegBIEN DB! Continue?),\
178
$(rmDb),$(error Aborting))
179
# otherwise, abort in case target was reinstall_db to prevent installing
180

    
181
reinstall_db: _always rm_db db ;
182

    
183
empty_db: _always schemas/vegbien_empty.sql
184
	$(psqlAsBien) <schemas/vegbien_empty.sql
185

    
186
##### MySQL
187

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

    
191
rm_mysql: _always rm_mysql_user ;
192

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

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

    
215
mysql-: _always # other OSes
216

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

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

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

    
229
##### Datasources
230

    
231
inputs: _always inputs/all ;
232

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

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

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

    
245
#### Maps validation
246

    
247
missing_mappings: _always missing_join_mappings missing_input_mappings ;
248

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

    
254
##### Testing
255

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