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
psqlAsAdmin := sudo -u postgres psql $(psqlOpts)
70
psqlAsBien := ./util/psql_vegbien $(psqlOpts)
71
bienPassword := $(shell cat util/bien_password)
72

    
73
postgres_user: _not_file
74
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
75
# ignore errors about user existing
76

    
77
rm_postgres_user: _not_file
78
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
79

    
80
#####
81

    
82
db: _not_file
83
	$(psqlAsAdmin) <../mappings/schemas/vegbien.sql
84

    
85
rm_db: _not_file
86
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
87

    
88
reinstall_db: _not_file rm_db db
89

    
90
empty_db: _not_file
91
	$(psqlAsBien) <../mappings/schemas/vegbien_empty.sql
92

    
93
#####
94

    
95
mysql: _not_file $(call forOs,mysql) mysql_user
96
	@$(done)
97

    
98
rm_mysql: _not_file rm_mysql_user
99

    
100
mysql-Linux: _not_file
101
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
102
	@$(wait)
103
	-sudo apt-get install mysql-server mysql-client python-mysqldb
104

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

    
122
mysql-: _not_file # other OSes
123

    
124
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
125

    
126
mysql_user: _not_file
127
	-$($@_cmd)
128
# ignore errors about user existing
129
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
130
'$(bienPassword)';"|$(mysqlAsRoot)
131

    
132
rm_mysql_user: _not_file
133
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
134
# ignore errors about user not existing
135

    
136
#####
137

    
138
inputs: _not_file $(wildcard ../../inputs/*/)
139

    
140
#####
141

    
142
test: _not_file test-map
143
	@$(done)
144

    
145
test-%: _not_file
146
	./test/$(*F)
(1-1/3)