Project

General

Profile

1 244 aaronmk
# Make
2 247 aaronmk
SHELL := /bin/bash
3 625 aaronmk
pathParts = $(shell path="$(1)"; echo "$${path%%/*}" "$${path\#*/}")
4 739 aaronmk
topDir = $(word 1,$(pathParts))
5
subPath = $(word 2,$(pathParts))
6 625 aaronmk
subMake = $(MAKE) $(call subPath,$@) --directory=$(call topDir,$@)
7 244 aaronmk
8 245 aaronmk
# OS
9 247 aaronmk
os := $(shell uname)
10 245 aaronmk
forOs = $(patsubst %,%-$(filter Linux Darwin,$(os)),$(1))
11
12 241 aaronmk
# Terminal
13 247 aaronmk
esc := '['
14
reset := $(esc)'0m'
15
emph := $(esc)'7m '
16
endEmph := ' '$(reset)
17 245 aaronmk
18
# User interaction
19
20
confirm = $(shell read -p $(emph)"$(1) (y/n)"$(endEmph) REPLY; \
21
test "$REPLY" = y && echo t)
22
23
getPassword = $(shell read -s -p $(emph)"Enter your $(1) password:"$(endEmph) \
24
REPLY; echo >&2; echo -n "$REPLY")
25
26 247 aaronmk
wait := read -p $(emph)'Press ENTER to continue:'$(endEmph) REPLY
27 245 aaronmk
28 249 aaronmk
done = echo; echo $(emph)"Finished $@"$(endEmph); echo
29 235 aaronmk
30 335 aaronmk
# File editing
31
sudoAppend = echo $$'$(2)'|sudo tee -a $(1) >/dev/null
32
33 241 aaronmk
# Env
34 225 aaronmk
export PGOPTIONS = --client-min-messages=WARNING
35
36 241 aaronmk
#####
37 225 aaronmk
38 414 aaronmk
all:
39 228 aaronmk
40 225 aaronmk
.SUFFIXES:
41
42 383 aaronmk
_always:
43
.PHONY: _always
44 202 aaronmk
45 402 aaronmk
define subdirTargets
46
$(subdir): _always
47
	+$$(subMake)
48 388 aaronmk
49 402 aaronmk
$(subdir)%: _always
50
	+$$(subMake)
51
endef
52
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
53 388 aaronmk
54 416 aaronmk
Makefile: ;
55
56 414 aaronmk
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
57
58 241 aaronmk
#####
59
60 411 aaronmk
install: _always core mysql inputs/install test
61 241 aaronmk
	@$(done)
62 202 aaronmk
63 418 aaronmk
uninstall: _always inputs/uninstall rm_mysql rm_core ;
64 241 aaronmk
65 418 aaronmk
reinstall: _always uninstall install ;
66 225 aaronmk
67 241 aaronmk
#####
68 202 aaronmk
69 383 aaronmk
core: _always $(call forOs,python postgres) postgres_user db
70 241 aaronmk
	@$(done)
71 235 aaronmk
72 418 aaronmk
rm_core: _always rm_db rm_postgres_user ;
73 241 aaronmk
74 418 aaronmk
reinstall_core: _always rm_core core ;
75 241 aaronmk
76 383 aaronmk
python-Linux: _always
77 334 aaronmk
	-sudo apt-get install python python-dev python-dateutil python-pip
78 284 aaronmk
79 383 aaronmk
python-Darwin: _always
80 284 aaronmk
	@echo $(emph)'Installing python-dateutil on Mac OS X:'$(endEmph)
81
	@echo 'Download it: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
82
	@echo 'Extract the archive'
83
	@echo 'Change to the directory of the extracted files'
84
	@echo 'Run python setup.py build'
85
	@echo 'Run sudo python setup.py install'
86
	@echo "Run python -c 'import dateutil'"
87
	@$(wait)
88
89 337 aaronmk
editPhppgadminApacheConf = echo $$'1\n,s/\# \(allow from all\)/\1/\nwq'|\
90 336 aaronmk
sudo ed --loose-exit-status --verbose /etc/phppgadmin/apache.conf
91 334 aaronmk
92
apacheConf := /etc/apache2/apache2.conf
93
apacheConfPhppgadminLine := Include /etc/phppgadmin/apache.conf
94
editApacheConfForPhppgadmin =\
95
$(if $(shell grep -F "$(apacheConfPhppgadminLine)" $(apacheConf)),,\
96
$(call sudoAppend,$(apacheConf),\n$(apacheConfPhppgadminLine)))
97
98 335 aaronmk
nonApacheOnPort80 = $(shell sudo lsof -i :80|tail -n +2|grep -v -F apache2)
99 336 aaronmk
editApachePortsConf = echo $$'1\n,s/\\b80\\b/8080/g\nwq'|\
100
sudo ed --loose-exit-status --verbose /etc/apache2/ports.conf
101 334 aaronmk
102 383 aaronmk
postgres-Linux: _always
103 329 aaronmk
	-sudo apt-get install postgresql libpq-dev phppgadmin
104 336 aaronmk
	$(editPhppgadminApacheConf)
105 334 aaronmk
	$(editApacheConfForPhppgadmin)
106 336 aaronmk
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
107 334 aaronmk
	-sudo apache2ctl restart
108 284 aaronmk
	-sudo pip install psycopg2
109 334 aaronmk
# ignore errors if conf files already edited
110 235 aaronmk
111 383 aaronmk
postgres-Darwin: _always
112 254 aaronmk
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
113
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
114
	@echo 'Open the disk image'
115
	@echo 'Run the installer in it'
116
	@$(wait)
117 235 aaronmk
118 418 aaronmk
postgres-: _always ; # other OSes
119 241 aaronmk
120 247 aaronmk
psqlOpts := --set ON_ERROR_STOP=1 --quiet
121 249 aaronmk
psqlAsAdmin := sudo -u postgres psql $(psqlOpts)
122 274 aaronmk
psqlAsBien := ./bin/psql_vegbien $(psqlOpts)
123 272 aaronmk
bienPassword := $(shell cat config/bien_password)
124 241 aaronmk
125 383 aaronmk
postgres_user: _always
126 249 aaronmk
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
127 364 aaronmk
# ignore errors if user exists
128 241 aaronmk
129 383 aaronmk
rm_postgres_user: _always
130 241 aaronmk
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
131
132
#####
133
134 389 aaronmk
db: _always schemas/vegbien.sql
135 383 aaronmk
	-$($@_create_cmd)
136 389 aaronmk
	$(psqlAsBien) <schemas/vegbien.sql
137 364 aaronmk
# ignore errors if database exists
138 383 aaronmk
db_create_cmd = echo "CREATE DATABASE vegbien OWNER bien ENCODING 'UTF8' \
139 381 aaronmk
TEMPLATE template1;"|$(psqlAsAdmin)
140 241 aaronmk
141 383 aaronmk
rm_db: _always
142 241 aaronmk
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
143
144 418 aaronmk
reinstall_db: _always rm_db db ;
145 241 aaronmk
146 389 aaronmk
empty_db: _always schemas/vegbien_empty.sql
147
	$(psqlAsBien) <schemas/vegbien_empty.sql
148 241 aaronmk
149
#####
150
151 383 aaronmk
mysql: _always $(call forOs,mysql) mysql_user
152 241 aaronmk
	@$(done)
153
154 418 aaronmk
rm_mysql: _always rm_mysql_user ;
155 241 aaronmk
156 383 aaronmk
mysql-Linux: _always
157 244 aaronmk
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
158 238 aaronmk
	@$(wait)
159 237 aaronmk
	-sudo apt-get install mysql-server mysql-client python-mysqldb
160 235 aaronmk
161 383 aaronmk
mysql-Darwin: _always
162 244 aaronmk
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
163 235 aaronmk
	@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
164
	@echo 'Extract the archive'
165
	@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
166 284 aaronmk
	@echo 'Run ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
167
	@echo 'Run ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
168 235 aaronmk
	@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
169
	@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
170
	@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
171
	@echo 'Change to the directory of the extracted files'
172 284 aaronmk
	@echo 'Run python setup.py clean'
173
	@echo 'Run python setup.py build'
174 235 aaronmk
	@echo 'Run sudo python setup.py install'
175
	@echo "Run python -c 'import MySQLdb'"
176 238 aaronmk
	@$(wait)
177 235 aaronmk
178 383 aaronmk
mysql-: _always # other OSes
179 235 aaronmk
180 247 aaronmk
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
181 235 aaronmk
182 383 aaronmk
mysql_user: _always
183 241 aaronmk
	-$($@_cmd)
184 364 aaronmk
# ignore errors if user exists
185 244 aaronmk
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
186 245 aaronmk
'$(bienPassword)';"|$(mysqlAsRoot)
187 202 aaronmk
188 383 aaronmk
rm_mysql_user: _always
189 245 aaronmk
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
190 364 aaronmk
# ignore errors if user exists
191 202 aaronmk
192 241 aaronmk
#####
193 202 aaronmk
194 418 aaronmk
inputs: _always inputs/all ;
195 245 aaronmk
196 418 aaronmk
import: _always inputs/import ;
197 250 aaronmk
198 418 aaronmk
verify: _always inputs/verify ;
199 386 aaronmk
200 418 aaronmk
test2: _always inputs/test ;
201 397 aaronmk
	@$(done)
202 375 aaronmk
203 245 aaronmk
#####
204
205 383 aaronmk
test: _always test-map
206 241 aaronmk
	@$(done)
207 202 aaronmk
208 383 aaronmk
test-%: _always
209 176 aaronmk
	./test/$(*F)