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
endef
68
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
69

    
70
Makefile: ;
71

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

    
74
##### Installation
75

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

    
79
uninstall: _always inputs/uninstall rm_mysql rm_core ;
80

    
81
reinstall: _always uninstall install ;
82

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

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

    
88
rm_core: _always rm_db rm_postgres_user ;
89

    
90
reinstall_core: _always rm_core core ;
91

    
92
##### Python
93

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

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

    
108
##### PostgreSQL
109

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

    
113
apacheConf := /etc/apache2/apache2.conf
114
apacheConfPhppgadminLine := Include /etc/phppgadmin/apache.conf
115
editApacheConfForPhppgadmin =\
116
$(if $(shell grep -F "$(apacheConfPhppgadminLine)" $(apacheConf)),,\
117
$(call sudoAppend,$(apacheConf),\n$(apacheConfPhppgadminLine)))
118

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

    
123
postgres-Linux: _always
124
	-sudo apt-get install postgresql libpq-dev phppgadmin
125
	$(editPhppgadminApacheConf)
126
	$(editApacheConfForPhppgadmin)
127
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
128
	-sudo apache2ctl restart
129
	-sudo pip install psycopg2
130
# ignore errors if conf files already edited
131

    
132
postgres-Darwin: _always
133
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
134
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
135
	@echo 'Open the disk image'
136
	@echo 'Run the installer in it'
137
	@$(wait)
138

    
139
postgres-: _always ; # other OSes
140

    
141
psqlOpts := --set ON_ERROR_STOP=1 --quiet
142
psqlAsAdmin := sudo -u postgres psql $(psqlOpts)
143
psqlAsBien := bin/psql_vegbien $(psqlOpts)
144
bienPassword := $(shell cat config/bien_password)
145

    
146
postgres_user: _always
147
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
148
# ignore errors if user exists
149

    
150
rm_postgres_user: _always
151
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
152

    
153
##### VegBIEN DB
154

    
155
db: _always schemas/vegbien.sql
156
	-$($@_create_cmd)
157
	$(psqlAsBien) <schemas/vegbien.sql
158
# ignore errors if database exists
159
db_create_cmd = echo "CREATE DATABASE vegbien OWNER bien ENCODING 'UTF8' \
160
TEMPLATE template1;"|$(psqlAsAdmin)
161

    
162
rm_db: _always
163
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
164

    
165
reinstall_db: _always rm_db db ;
166

    
167
empty_db: _always schemas/vegbien_empty.sql
168
	$(psqlAsBien) <schemas/vegbien_empty.sql
169

    
170
##### MySQL
171

    
172
mysql: _always $(call forOs,mysql) mysql_user
173
	@$(done)
174

    
175
rm_mysql: _always rm_mysql_user ;
176

    
177
mysql-Linux: _always
178
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
179
	@$(wait)
180
	-sudo apt-get install mysql-server mysql-client python-mysqldb
181

    
182
mysql-Darwin: _always
183
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
184
	@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
185
	@echo 'Extract the archive'
186
	@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
187
	@echo 'Run ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
188
	@echo 'Run ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
189
	@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
190
	@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
191
	@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
192
	@echo 'Change to the directory of the extracted files'
193
	@echo 'Run python setup.py clean'
194
	@echo 'Run python setup.py build'
195
	@echo 'Run sudo python setup.py install'
196
	@echo "Run python -c 'import MySQLdb'"
197
	@$(wait)
198

    
199
mysql-: _always # other OSes
200

    
201
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
202

    
203
mysql_user: _always
204
	-$($@_cmd)
205
# ignore errors if user exists
206
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
207
'$(bienPassword)';"|$(mysqlAsRoot)
208

    
209
rm_mysql_user: _always
210
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
211
# ignore errors if user exists
212

    
213
##### Datasources
214

    
215
inputs: _always inputs/all ;
216

    
217
import: _always inputs/import ;
218

    
219
verify: _always inputs/verify
220
	@$(done)
221

    
222
test: _always inputs/test
223
	@$(done)
224

    
225
#### Maps validation
226

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

    
231
missing_joins:
232
	$(MAKE) inputs/remake 2>&1|$(filter_join_warnings)
233

    
234
##### Testing
235

    
236
test-all: _always remake test
237
	@$(done)
(1-1/3)