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
define subdirTargets
50
$(subdir): _always
51
	+$$(subMake)
52

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

    
58
Makefile: ;
59

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

    
62
##### Installation
63

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

    
67
uninstall: _always inputs/uninstall rm_mysql rm_core ;
68

    
69
reinstall: _always uninstall install ;
70

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

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

    
76
rm_core: _always rm_db rm_postgres_user ;
77

    
78
reinstall_core: _always rm_core core ;
79

    
80
##### Python
81

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

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

    
96
##### PostgreSQL
97

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

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

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

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

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

    
127
postgres-: _always ; # other OSes
128

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

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

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

    
141
##### VegBIEN DB
142

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

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

    
153
reinstall_db: _always rm_db db ;
154

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

    
158
##### MySQL
159

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

    
163
rm_mysql: _always rm_mysql_user ;
164

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

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

    
187
mysql-: _always # other OSes
188

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

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

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

    
201
##### Datasources
202

    
203
inputs: _always inputs/all ;
204

    
205
import: _always inputs/import ;
206

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

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