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

    
46
_always:
47
.PHONY: _always
48

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

    
53
define subdirTargets
54
$(subdir): _always
55
	+$$(subMake)
56

    
57
$(subdir)%: _always
58
	+$$(subMake)
59

    
60
$(subdir)remake: _always $(subdir)clean
61
	$(MAKE) $(subdir)
62
# re-run make so that cache of existing files is reset
63
endef
64
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
65

    
66
Makefile: ;
67

    
68
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
69

    
70
##### Installation
71

    
72
install: _always core mysql inputs/install test
73
	@$(done)
74

    
75
uninstall: _always inputs/uninstall rm_mysql rm_core ;
76

    
77
reinstall: _always uninstall install ;
78

    
79
##### Core: VegBIEN DB and dependencies
80

    
81
core: _always $(call forOs,python postgres) postgres_user db
82
	@$(done)
83

    
84
rm_core: _always rm_db rm_postgres_user ;
85

    
86
reinstall_core: _always rm_core core ;
87

    
88
##### Python
89

    
90
python-Linux: _always
91
	-sudo apt-get install python python-dev python-dateutil python-profiler\
92
python-pip
93

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

    
104
##### PostgreSQL
105

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

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

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

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

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

    
135
postgres-: _always ; # other OSes
136

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

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

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

    
149
##### VegBIEN DB
150

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

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

    
161
reinstall_db: _always rm_db db ;
162

    
163
empty_db: _always schemas/vegbien_empty.sql
164
	$(psqlAsBien) <schemas/vegbien_empty.sql
165

    
166
##### MySQL
167

    
168
mysql: _always $(call forOs,mysql) mysql_user
169
	@$(done)
170

    
171
rm_mysql: _always rm_mysql_user ;
172

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

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

    
195
mysql-: _always # other OSes
196

    
197
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
198

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

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

    
209
##### Datasources
210

    
211
inputs: _always inputs/all ;
212

    
213
import: _always inputs/import ;
214

    
215
verify: _always inputs/verify ;
216
	@$(done)
217

    
218
test: _always inputs/test ;
219
	@$(done)
(1-1/3)