Project

General

Profile

1
# Make
2
SHELL = /bin/bash
3

    
4
# OS
5
os = $(shell uname)
6
forOs = $(patsubst %,%-$(filter Linux Darwin,$(os)),$(1))
7

    
8
# Terminal
9
esc = '['
10
reset = $(esc)'0m'
11
emph = $(esc)'7m '
12
endEmph = ' '$(reset)
13

    
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
wait = read -p $(emph)'Press ENTER to continue:'$(endEmph) REPLY
23

    
24
done = echo; echo $(emph)"Finished $@"$(endEmph); echo
25

    
26
# Env
27
export PGOPTIONS = --client-min-messages=WARNING
28

    
29
#####
30

    
31
all: _not_file install
32

    
33
.SUFFIXES:
34

    
35
_not_file:
36
.PHONY: _not_file
37

    
38
%/: _not_file
39
	$(if $(wildcard $@/Makefile*),$(MAKE) --directory=$@,)
40

    
41
#####
42

    
43
install: _not_file core mysql inputs test
44
	@$(done)
45

    
46
uninstall: _not_file rm_mysql rm_core
47

    
48
reinstall: _not_file uninstall install
49

    
50
#####
51

    
52
core: _not_file $(call forOs,postgres) postgres_user db
53
	@$(done)
54

    
55
rm_core: _not_file rm_db rm_postgres_user
56

    
57
reinstall_core: _not_file rm_core core
58

    
59
postgres-Linux: _not_file
60
	-sudo apt-get install python-dev libpq-dev
61
	-sudo apt-get install python-pip && sudo pip install psycopg2
62

    
63
postgres-Darwin: _not_file # TODO: implement this
64

    
65
postgres-: _not_file # other OSes
66

    
67
psqlOpts = --set ON_ERROR_STOP=1 --quiet
68
asPostgresAdmin = sudo -u postgres
69
psqlAsAdmin = $(asPostgresAdmin) psql $(psqlOpts)
70
psqlAsBien = ./util/psql_vegbien $(psqlOpts)
71
bienPassword = $(shell cat util/bien_password)
72

    
73
postgres_user: _not_file
74
	@echo $(emph)"At sudo password prompt, enter *your* password"$(endEmph)
75
	@sudo -v
76
	@echo $(emph)"At \"Enter password for new role:\", enter $(bienPassword)"$(endEmph)
77
	-$($@_cmd)
78
# ignore errors about user existing
79
postgres_user_cmd = $(asPostgresAdmin) createuser --no-superuser \
80
--no-createdb --no-createrole --pwprompt "bien"
81

    
82
rm_postgres_user: _not_file
83
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
84

    
85
#####
86

    
87
db: _not_file
88
	$(psqlAsAdmin) <../mappings/schemas/vegbien.sql
89

    
90
rm_db: _not_file
91
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
92

    
93
reinstall_db: _not_file rm_db db
94

    
95
empty_db: _not_file
96
	$(psqlAsBien) <../mappings/schemas/vegbien_empty.sql
97

    
98
#####
99

    
100
mysql: _not_file $(call forOs,mysql) mysql_user
101
	@$(done)
102

    
103
rm_mysql: _not_file rm_mysql_user
104

    
105
mysql-Linux: _not_file
106
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
107
	@$(wait)
108
	-sudo apt-get install mysql-server mysql-client python-mysqldb
109

    
110
mysql-Darwin: _not_file
111
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
112
	@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
113
	@echo 'Extract the archive'
114
	@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
115
	@echo 'Run sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
116
	@echo 'Run sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
117
	@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
118
	@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
119
	@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
120
	@echo 'Change to the directory of the extracted files'
121
	@echo 'Run sudo python setup.py clean'
122
	@echo 'Run sudo python setup.py build'
123
	@echo 'Run sudo python setup.py install'
124
	@echo "Run python -c 'import MySQLdb'"
125
	@$(wait)
126

    
127
mysql-: _not_file # other OSes
128

    
129
mysqlAsRoot = mysql --user=root --password='$(bienPassword)'
130

    
131
mysql_user: _not_file
132
	-$($@_cmd)
133
# ignore errors about user existing
134
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
135
'$(bienPassword)';"|$(mysqlAsRoot)
136

    
137
rm_mysql_user: _not_file
138
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
139
# ignore errors about user not existing
140

    
141
#####
142

    
143
mysqlAsBien = mysql --user=bien --password='$(bienPassword)'
144

    
145
#####
146

    
147
inputs: _not_file $(wildcard ../../inputs/*/)
148

    
149
#####
150

    
151
test: _not_file test-map
152
	@$(done)
153

    
154
test-%: _not_file
155
	./test/$(*F)
(1-1/3)