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
# ignore errors in sub-makes
41

    
42
#####
43

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

    
47
uninstall: _not_file rm_mysql rm_core
48

    
49
reinstall: _not_file uninstall install
50

    
51
#####
52

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

    
56
rm_core: _not_file rm_db rm_postgres_user
57

    
58
reinstall_core: _not_file rm_core core
59

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

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

    
66
postgres-: _not_file # other OSes
67

    
68
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

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

    
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
	$(psqlAsBien) <../mappings/schemas/vegbien_empty.sql
98

    
99
#####
100

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

    
104
rm_mysql: _not_file rm_mysql_user
105

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

    
111
mysql-Darwin: _not_file
112
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
113
	@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
	@$(wait)
127

    
128
mysql-: _not_file # other OSes
129

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

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

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

    
142
#####
143

    
144
inputs: _not_file $(wildcard ../../inputs/*/)
145

    
146
#####
147

    
148
test: _not_file test-map
149
	@$(done)
150

    
151
test-%: _not_file
152
	./test/$(*F)
(1-1/3)