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
confirm = $(shell read -p $(emph)"$(1) (y/n)"$(endEmph) REPLY; \
23
test "$REPLY" = y && echo t)
24

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

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

    
30
done = echo; echo $(emph)"Finished $@"$(endEmph); echo
31

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

    
35
##### Environment
36

    
37
export PGOPTIONS = --client-min-messages=WARNING
38

    
39
##### General targets
40

    
41
all:
42

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

    
47
_always:
48
.PHONY: _always
49

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

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

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

    
63
$(subdir)%: _always
64
	+$$(subMake)
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 postgres) postgres_user db
84
	@$(done)
85

    
86
rm_core: _always rm_db rm_postgres_user ;
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
##### PostgreSQL
107

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

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

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

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

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

    
137
postgres-: _always ; # other OSes
138

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

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

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

    
151
##### VegBIEN DB
152

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

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

    
163
reinstall_db: _always rm_db db ;
164

    
165
empty_db: _always schemas/vegbien_empty.sql
166
	$(psqlAsBien) <schemas/vegbien_empty.sql
167

    
168
##### MySQL
169

    
170
mysql: _always $(call forOs,mysql) mysql_user
171
	@$(done)
172

    
173
rm_mysql: _always rm_mysql_user ;
174

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

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

    
197
mysql-: _always # other OSes
198

    
199
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
200

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

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

    
211
##### Datasources
212

    
213
inputs: _always inputs/all ;
214

    
215
import: _always inputs/import ;
216

    
217
verify: _always inputs/verify
218
	@$(done)
219

    
220
test: _always inputs/test
221
	@$(done)
222

    
223
##### Testing
224

    
225
test-all: _always remake test
226
	@$(done)
(1-1/3)