Project

General

Profile

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