Project

General

Profile

1 244 aaronmk
# Make
2 247 aaronmk
SHELL := /bin/bash
3 244 aaronmk
4 245 aaronmk
# OS
5 247 aaronmk
os := $(shell uname)
6 245 aaronmk
forOs = $(patsubst %,%-$(filter Linux Darwin,$(os)),$(1))
7
8 241 aaronmk
# Terminal
9 247 aaronmk
esc := '['
10
reset := $(esc)'0m'
11
emph := $(esc)'7m '
12
endEmph := ' '$(reset)
13 245 aaronmk
14
# User interaction
15
16
confirm = $(shell read -p $(emph)"$(1) (y/n)"$(endEmph) REPLY; \
17
test "$REPLY" = y && echo t)
18
19
getPassword = $(shell read -s -p $(emph)"Enter your $(1) password:"$(endEmph) \
20
REPLY; echo >&2; echo -n "$REPLY")
21
22 247 aaronmk
wait := read -p $(emph)'Press ENTER to continue:'$(endEmph) REPLY
23 245 aaronmk
24 247 aaronmk
done := echo; echo $(emph)"Finished $@"$(endEmph); echo
25 235 aaronmk
26 241 aaronmk
# Env
27 225 aaronmk
export PGOPTIONS = --client-min-messages=WARNING
28
29 241 aaronmk
#####
30 225 aaronmk
31 235 aaronmk
all: _not_file install
32 228 aaronmk
33 225 aaronmk
.SUFFIXES:
34
35 235 aaronmk
_not_file:
36
.PHONY: _not_file
37 202 aaronmk
38 241 aaronmk
%/: _not_file
39 247 aaronmk
	-$(if $(wildcard $@/Makefile*),$(MAKE) --directory=$@ ,)
40
# ignore errors in sub-makes
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 245 aaronmk
uninstall: _not_file 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 241 aaronmk
core: _not_file $(call forOs,postgres) postgres_user db
54
	@$(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 235 aaronmk
postgres-Linux: _not_file
61 237 aaronmk
	-sudo apt-get install python-dev libpq-dev
62
	-sudo apt-get install python-pip && sudo pip install psycopg2
63 235 aaronmk
64 241 aaronmk
postgres-Darwin: _not_file # TODO: implement this
65 235 aaronmk
66 241 aaronmk
postgres-: _not_file # other OSes
67
68 247 aaronmk
psqlOpts := --set ON_ERROR_STOP=1 --quiet
69
asPostgresAdmin:== sudo -u postgres
70
psqlAsAdmin := $(asPostgresAdmin) psql $(psqlOpts)
71
psqlAsBien := ./util/psql_vegbien $(psqlOpts)
72
bienPassword := $(shell cat util/bien_password)
73 241 aaronmk
74
postgres_user: _not_file
75 244 aaronmk
	@echo $(emph)"At sudo password prompt, enter *your* password"$(endEmph)
76 241 aaronmk
	@sudo -v
77 244 aaronmk
	@echo $(emph)"At \"Enter password for new role:\", enter $(bienPassword)"$(endEmph)
78 241 aaronmk
	-$($@_cmd)
79
# ignore errors about user existing
80 244 aaronmk
postgres_user_cmd = $(asPostgresAdmin) createuser --no-superuser \
81 243 aaronmk
--no-createdb --no-createrole --pwprompt "bien"
82 241 aaronmk
83
rm_postgres_user: _not_file
84
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
85
86
#####
87
88
db: _not_file
89
	$(psqlAsAdmin) <../mappings/schemas/vegbien.sql
90
91
rm_db: _not_file
92
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
93
94
reinstall_db: _not_file rm_db db
95
96
empty_db: _not_file
97 246 aaronmk
	$(psqlAsBien) <../mappings/schemas/vegbien_empty.sql
98 241 aaronmk
99
#####
100
101 245 aaronmk
mysql: _not_file $(call forOs,mysql) mysql_user
102 241 aaronmk
	@$(done)
103
104 245 aaronmk
rm_mysql: _not_file rm_mysql_user
105 241 aaronmk
106 235 aaronmk
mysql-Linux: _not_file
107 244 aaronmk
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
108 238 aaronmk
	@$(wait)
109 237 aaronmk
	-sudo apt-get install mysql-server mysql-client python-mysqldb
110 235 aaronmk
111
mysql-Darwin: _not_file
112 244 aaronmk
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
113 235 aaronmk
	@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
114
	@echo 'Extract the archive'
115
	@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
116
	@echo 'Run sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
117
	@echo 'Run sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
118
	@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
119
	@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
120
	@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
121
	@echo 'Change to the directory of the extracted files'
122
	@echo 'Run sudo python setup.py clean'
123
	@echo 'Run sudo python setup.py build'
124
	@echo 'Run sudo python setup.py install'
125
	@echo "Run python -c 'import MySQLdb'"
126 238 aaronmk
	@$(wait)
127 235 aaronmk
128 241 aaronmk
mysql-: _not_file # other OSes
129 235 aaronmk
130 247 aaronmk
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
131 235 aaronmk
132 241 aaronmk
mysql_user: _not_file
133
	-$($@_cmd)
134
# ignore errors about user existing
135 244 aaronmk
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
136 245 aaronmk
'$(bienPassword)';"|$(mysqlAsRoot)
137 202 aaronmk
138 241 aaronmk
rm_mysql_user: _not_file
139 245 aaronmk
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
140 241 aaronmk
# ignore errors about user not existing
141 202 aaronmk
142 241 aaronmk
#####
143 202 aaronmk
144 246 aaronmk
inputs: _not_file $(wildcard ../../inputs/*/)
145 245 aaronmk
146
#####
147
148 241 aaronmk
test: _not_file test-map
149
	@$(done)
150 202 aaronmk
151 235 aaronmk
test-%: _not_file
152 176 aaronmk
	./test/$(*F)