Project

General

Profile

1
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
2

    
3
# Make
4
SHELL := /bin/bash
5
subMake = $(MAKE) $(@F) --directory=$(@D)
6
+_ = $(+:_%=)
7
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
8

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

    
15
# Commands
16
CP = cp -p
17
DIFF = diff --unified=2
18

    
19
tablesSort := plots organisms stems
20
vegxMaps := $(wildcard $(tablesSort:%=maps/VegX.%.csv))
21
vegxMaps += $(filter-out $(vegxMaps),$(wildcard maps/VegX.*.csv))
22
tables := $(vegxMaps:maps/VegX.%.csv=%)
23
vegbienMaps := $(subst VegX.,VegBIEN.,$(vegxMaps))
24
dbFile := $(firstword $(wildcard src/db.*.sql))
25

    
26
+maps = $(filter maps/%,$(+_))
27
+in = $(filter-out maps/%,$(+_))
28

    
29
root := $(selfDir_uZPPqC)..
30
psqlAsBien := $(root)/bin/psql_vegbien
31

    
32
# Config
33
test_n ?= 2
34

    
35
#####
36

    
37
all: _always maps ;
38

    
39
.SUFFIXES:
40

    
41
_always:
42
.PHONY: _always
43

    
44
clean: _always
45
	$(RM) $(all)
46

    
47
remake: _always clean all ;
48

    
49
%.out: %.make _always
50
	./$* >$@
51
.PRECIOUS: %.out
52

    
53
$(root)/%: _always
54
	+$(subMake)
55

    
56
#####
57

    
58
reinstall: _always uninstall install ;
59

    
60
#####
61

    
62
maps: $(vegbienMaps) _always ;
63

    
64
all += $(vegbienMaps)
65

    
66
maps/VegBIEN.%.csv: maps/VegX.%.csv $(root)/mappings/VegX-VegBIEN.%.csv
67
	$(root)/bin/join_sort <$+ >$@
68
.PRECIOUS: maps/VegBIEN.%.csv
69

    
70
#####
71

    
72
ifneq ($(wildcard $(dbFile)),)
73

    
74
import: _always import-all verify ;
75

    
76
log = $(@:-all=)$(if $(n),.n=$(n),).log
77
import = -(set -x; "time" env commit=1 $(map2db) $(+_)) \
78
$(if $(n),,>>$(log)) 2>&1$(if $(n),|tee -a $(log))
79
# ignore import errors, which are often benign (e.g. invalid date format)
80

    
81
import-all: $(vegbienMaps) _always
82
	$(import)
83

    
84
import-%: maps/VegBIEN.%.csv _always
85
	$(import)
86

    
87
logs := $(wildcard *.log)
88

    
89
rm_logs: _always
90
	$(RM) $(logs)
91

    
92
else
93
import: _always ;
94
import-%: _always ;
95
rm_logs: _always ;
96
endif
97

    
98
#####
99

    
100
ifneq ($(wildcard verify.ref*),)
101

    
102
verify: verify.ref verify.out _always
103
	$(DIFF) $(+_)
104

    
105
all += verify.out
106

    
107
%.out: %.sql _always
108
	$(out_cmd)
109
.PRECIOUS: %.out
110
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off \
111
--pset=null=NULL <$< >$@
112

    
113
else
114
verify: _always ;
115
endif
116

    
117
#####
118

    
119
ifneq ($(wildcard test/),)
120

    
121
tests := test/VegX.xml test/VegBIEN.xml test/VegBIEN.2-step.xml test/import.out
122

    
123
test: _always $(tests) ;
124

    
125
.PRECIOUS: $(tests)
126

    
127
all += $(tests)
128

    
129
testRef = $(1:.2-step.xml=.xml).ref
130

    
131
define test
132
env test=1 verbose=1 n=$(test_n) $(1) $(if $(+in),<$(+in)) $(+maps) >$@
133
@(set -x; $(DIFF) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
134
echo $(emph)"To accept new test output:"$(endEmph);\
135
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
136
exit $$e;}
137
endef
138

    
139
test/VegX.xml: $(vegxMaps) _always
140
	$(call test,$(map))
141

    
142
test/VegBIEN.xml: $(vegbienMaps) _always
143
	$(call test,$(map))
144

    
145
test/VegBIEN.2-step.xml: test/VegX.xml\
146
$(root)/mappings/VegX-VegBIEN.organisms.csv _always
147
	$(call test,$(root)/map)
148

    
149
test/import.out: $(vegbienMaps) _always
150
	$(call test,$(map2db))
151

    
152
# Accepts a test output: make <test_output_path>-ok
153
%-ok: _always
154
	$(CP) $* $(call testRef,$*)
155

    
156
else
157
test: _always ;
158
endif
159

    
160
#####
161

    
162
# Each input type needs var $(mapEnv) and targets install, uninstall
163

    
164
ifneq ($(dbFile),)
165

    
166
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
167
db := $(notdir $(realpath .))
168

    
169
%.ref: %.ref.sql
170
	$(inputDbAsBien) $(db) <$< >$@
171
.PRECIOUS: %.ref
172

    
173
####
174

    
175
install: _always db ;
176

    
177
uninstall: _always rm_db ;
178

    
179
####
180

    
181
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
182

    
183
ifeq ($(dbEngineExt),my)
184

    
185
dbEngine := MySQL
186

    
187
bienPassword := $(shell cat $(root)/config/bien_password)
188
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
189
mysqlAsRoot := $(call mysqlAs,root)
190
dbAsBien := $(call mysqlAs,bien)
191

    
192
db: $(dbFile) _always
193
	echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
194
	-$(mysqlAsRoot) --database=$(db) <$<
195
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
196
# ignore errors in db import so that GRANT will still be run
197

    
198
rm_db: _always
199
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
200
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
201
# ignore errors if grant not defined
202

    
203
###
204

    
205
else
206
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
207
endif
208

    
209
# Must come after dbEngine is set
210
mapEnv := in_engine=$(dbEngine) in_database=$(db)
211

    
212
####
213

    
214
else
215
mapEnv :=
216
install: _always ;
217
uninstall: _always ;
218
endif
219

    
220
#####
221

    
222
# Must come after mapEnv is set
223
map := $(if $(mapEnv),env $(mapEnv) ,)$(root)/map
224
map2db := env out_database=vegbien $(map)
(2-2/2)