Project

General

Profile

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