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:
44

    
45
_always:
46
.PHONY: _always
47

    
48
define subdirTargets
49
$(subdir): _always
50
	+$$(subMake)
51

    
52
$(subdir)%: _always
53
	+$$(subMake)
54
endef
55
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
56

    
57
Makefile: ;
58

    
59
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
60

    
61
##### Installation
62

    
63
install: _always core mysql inputs/install test
64
	@$(done)
65

    
66
uninstall: _always inputs/uninstall rm_mysql rm_core ;
67

    
68
reinstall: _always uninstall install ;
69

    
70
##### Core: VegBIEN DB and dependencies
71

    
72
core: _always $(call forOs,python postgres) postgres_user db
73
	@$(done)
74

    
75
rm_core: _always rm_db rm_postgres_user ;
76

    
77
reinstall_core: _always rm_core core ;
78

    
79
##### Python
80

    
81
python-Linux: _always
82
	-sudo apt-get install python python-dev python-dateutil python-profiler\
83
python-pip
84

    
85
python-Darwin: _always
86
	@echo $(emph)'Installing python-dateutil on Mac OS X:'$(endEmph)
87
	@echo 'Download it: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
88
	@echo 'Extract the archive'
89
	@echo 'Change to the directory of the extracted files'
90
	@echo 'Run python setup.py build'
91
	@echo 'Run sudo python setup.py install'
92
	@echo "Run python -c 'import dateutil'"
93
	@$(wait)
94

    
95
##### PostgreSQL
96

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

    
100
apacheConf := /etc/apache2/apache2.conf
101
apacheConfPhppgadminLine := Include /etc/phppgadmin/apache.conf
102
editApacheConfForPhppgadmin =\
103
$(if $(shell grep -F "$(apacheConfPhppgadminLine)" $(apacheConf)),,\
104
$(call sudoAppend,$(apacheConf),\n$(apacheConfPhppgadminLine)))
105

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

    
110
postgres-Linux: _always
111
	-sudo apt-get install postgresql libpq-dev phppgadmin
112
	$(editPhppgadminApacheConf)
113
	$(editApacheConfForPhppgadmin)
114
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
115
	-sudo apache2ctl restart
116
	-sudo pip install psycopg2
117
# ignore errors if conf files already edited
118

    
119
postgres-Darwin: _always
120
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
121
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
122
	@echo 'Open the disk image'
123
	@echo 'Run the installer in it'
124
	@$(wait)
125

    
126
postgres-: _always ; # other OSes
127

    
128
psqlOpts := --set ON_ERROR_STOP=1 --quiet
129
psqlAsAdmin := sudo -u postgres psql $(psqlOpts)
130
psqlAsBien := ./bin/psql_vegbien $(psqlOpts)
131
bienPassword := $(shell cat config/bien_password)
132

    
133
postgres_user: _always
134
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
135
# ignore errors if user exists
136

    
137
rm_postgres_user: _always
138
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
139

    
140
##### VegBIEN DB
141

    
142
db: _always schemas/vegbien.sql
143
	-$($@_create_cmd)
144
	$(psqlAsBien) <schemas/vegbien.sql
145
# ignore errors if database exists
146
db_create_cmd = echo "CREATE DATABASE vegbien OWNER bien ENCODING 'UTF8' \
147
TEMPLATE template1;"|$(psqlAsAdmin)
148

    
149
rm_db: _always
150
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
151

    
152
reinstall_db: _always rm_db db ;
153

    
154
empty_db: _always schemas/vegbien_empty.sql
155
	$(psqlAsBien) <schemas/vegbien_empty.sql
156

    
157
##### MySQL
158

    
159
mysql: _always $(call forOs,mysql) mysql_user
160
	@$(done)
161

    
162
rm_mysql: _always rm_mysql_user ;
163

    
164
mysql-Linux: _always
165
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
166
	@$(wait)
167
	-sudo apt-get install mysql-server mysql-client python-mysqldb
168

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

    
186
mysql-: _always # other OSes
187

    
188
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
189

    
190
mysql_user: _always
191
	-$($@_cmd)
192
# ignore errors if user exists
193
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
194
'$(bienPassword)';"|$(mysqlAsRoot)
195

    
196
rm_mysql_user: _always
197
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
198
# ignore errors if user exists
199

    
200
##### Datasources
201

    
202
inputs: _always inputs/all ;
203

    
204
import: _always inputs/import ;
205

    
206
verify: _always inputs/verify ;
207
	@$(done)
208

    
209
test: _always inputs/test ;
210
	@$(done)
(1-1/3)