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
SED = sed -$(if $(filter Darwin,$(os)),E,r)
15

    
16
# Terminal
17
esc := '['
18
reset := $(esc)'0m'
19
emph := $(esc)'7m '
20
endEmph := ' '$(reset)
21

    
22
# User interaction
23

    
24
confirm = $(shell read -p $(emph)"$(1) (y/n)"$(endEmph) REPLY; \
25
test "$REPLY" = y && echo t)
26

    
27
getPassword = $(shell read -s -p $(emph)"Enter your $(1) password:"$(endEmph) \
28
REPLY; echo >&2; echo -n "$REPLY")
29

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

    
32
done = echo; echo $(emph)"Finished $@"$(endEmph); echo
33

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

    
37
##### Environment
38

    
39
export PGOPTIONS = --client-min-messages=WARNING
40

    
41
##### General targets
42

    
43
all:
44

    
45
.SUFFIXES: # turn off built-in suffix rules
46
.SECONDARY: # don't automatically delete intermediate files
47
.DELETE_ON_ERROR: # delete target if recipe fails
48

    
49
_always:
50
.PHONY: _always
51

    
52
remake: _always clean
53
	$(MAKE)
54
# re-run make so that cache of existing files is reset
55

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

    
61
define subdirTargets
62
$(subdir): _always
63
	+$$(subMake)
64

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

    
71
Makefile: ;
72

    
73
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
74

    
75
##### Installation
76

    
77
install: _always core mysql inputs/install test
78
	@$(done)
79

    
80
uninstall: _always inputs/uninstall rm_mysql rm_core ;
81

    
82
reinstall: _always uninstall install ;
83

    
84
##### Core: VegBIEN DB and dependencies
85

    
86
core: _always $(call forOs,python php postgres) postgres_user db
87
	@$(done)
88

    
89
rm_core: _always rm_db rm_postgres_user ;
90

    
91
reinstall_core: _always rm_core core ;
92

    
93
##### Python
94

    
95
python-Linux: _always
96
	-sudo apt-get install python python-dev python-dateutil python-profiler\
97
python-pip pymetrics
98

    
99
python-Darwin: _always
100
	@echo $(emph)'Installing python-dateutil on Mac OS X:'$(endEmph)
101
	@echo 'Download it: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
102
	@echo 'Extract the archive'
103
	@echo 'Change to the directory of the extracted files'
104
	@echo 'Run python setup.py build'
105
	@echo 'Run sudo python setup.py install'
106
	@echo "Run python -c 'import dateutil'"
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/\# \(allow from all\)/\1/\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 libpq-dev phppgadmin
144
	$(editPhppgadminApacheConf)
145
	$(editApacheConfForPhppgadmin)
146
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
147
	-sudo apache2ctl restart
148
	-sudo pip install psycopg2
149
# ignore errors if conf files already edited
150

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

    
158
postgres-: _always ; # other OSes
159

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

    
165
postgres_user: _always
166
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
167
# ignore errors if user exists
168

    
169
rm_postgres_user: _always
170
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
171

    
172
##### VegBIEN DB
173

    
174
db: _always schemas/vegbien.sql
175
	-$($@_create_cmd)
176
	$(psqlAsBien) <schemas/vegbien.sql
177
# ignore errors if database exists
178
db_create_cmd = echo "CREATE DATABASE vegbien OWNER bien ENCODING 'UTF8' \
179
TEMPLATE template1;"|$(psqlAsAdmin)
180

    
181
rm_db: _always
182
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
183

    
184
reinstall_db: _always rm_db db ;
185

    
186
empty_db: _always schemas/vegbien_empty.sql
187
	$(psqlAsBien) <schemas/vegbien_empty.sql
188

    
189
##### MySQL
190

    
191
mysql: _always $(call forOs,mysql) mysql_user
192
	@$(done)
193

    
194
rm_mysql: _always rm_mysql_user ;
195

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

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

    
218
mysql-: _always # other OSes
219

    
220
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
221

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

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

    
232
##### Datasources
233

    
234
inputs: _always inputs/all ;
235

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

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

    
245
test: _always inputs/test
246
	@$(done)
247

    
248
#### Maps validation
249

    
250
filter_join_warnings =\
251
$(SED) -n 's/^.*No join mapping for ([0-9A-Za-z_-]+).*$$/\1/p'\
252
|bin/ucase_first 0|sort|uniq
253

    
254
missing_joins:
255
	$(MAKE) inputs/remake 2>&1|$(filter_join_warnings)
256

    
257
##### Testing
258

    
259
test-all: _always remake test
260
	@$(done)
(1-1/3)