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
# Config
20
test_n ?= 2
21
tablesSort ?= plots organisms stems
22

    
23
# Paths
24
viaMaps := $(filter-out maps/VegBIEN.%.csv,$(wildcard maps/*.csv))
25
via := $(firstword $(sort $(basename $(basename $(notdir $(viaMaps))))))
26
viaMapsSort := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
27
viaMaps := $(viaMapsSort) $(filter-out $(viaMapsSort),$(viaMaps))
28
tables := $(viaMaps:maps/$(via).%.csv=%)
29
directMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
30
directMaps += $(filter-out $(directMaps),$(wildcard maps/VegBIEN.*.csv))
31
dbFile := $(firstword $(wildcard src/db.*.sql))
32

    
33
+maps = $(filter maps/%,$(+_))
34
+in = $(filter-out maps/%,$(+_))
35

    
36
root := $(selfDir_uZPPqC)..
37
psqlAsBien := $(root)/bin/psql_vegbien
38

    
39
#####
40

    
41
all: _always maps ;
42

    
43
.SUFFIXES:
44

    
45
_always:
46
.PHONY: _always
47

    
48
clean: _always
49
	$(RM) $(all)
50

    
51
remake: _always clean all ;
52

    
53
%.out: %.make _always
54
	./$* >$@
55
.PRECIOUS: %.out
56

    
57
$(root)/%: _always
58
	+$(subMake)
59

    
60
#####
61

    
62
reinstall: _always uninstall install ;
63

    
64
#####
65

    
66
maps: $(directMaps) _always ;
67

    
68
all += $(directMaps)
69

    
70
maps/VegBIEN.%.csv: maps/$(via).%.csv $(root)/mappings/$(via)-VegBIEN.%.csv
71
	$(root)/bin/join_sort <$+ >$@
72
.PRECIOUS: maps/VegBIEN.%.csv
73

    
74
#####
75

    
76
ifneq ($(wildcard $(dbFile)),)
77

    
78
import: _always import-all verify ;
79

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

    
85
import-all: $(directMaps) _always
86
	$(import)
87

    
88
import-%: maps/VegBIEN.%.csv _always
89
	$(import)
90

    
91
logs := $(wildcard *.log)
92

    
93
rm_logs: _always
94
	$(RM) $(logs)
95

    
96
else
97
import: _always ;
98
import-%: _always ;
99
rm_logs: _always ;
100
endif
101

    
102
#####
103

    
104
ifneq ($(wildcard verify.ref*),)
105

    
106
verify: verify.ref verify.out _always
107
	$(DIFF) $(+_)
108

    
109
all += verify.out
110

    
111
%.out: %.sql _always
112
	$(out_cmd)
113
.PRECIOUS: %.out
114
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off \
115
--pset=null=NULL <$< >$@
116

    
117
else
118
verify: _always ;
119
endif
120

    
121
#####
122

    
123
ifneq ($(wildcard test/),)
124

    
125
tests :=
126

    
127
hasOwnRef = $(filter-out %.2-step.xml,$@)
128
testRef = $(1:.2-step.xml=.xml).ref
129

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

    
138
test/$(via).xml: $(viaMaps) _always
139
	$(call test,$(map))
140
tests += test/$(via).xml
141

    
142
test/VegBIEN.xml: $(directMaps) _always
143
	$(call test,$(map))
144
tests += test/VegBIEN.xml
145

    
146
test/VegBIEN.2-step.xml: test/$(via).xml\
147
$(root)/mappings/$(via)-VegBIEN.organisms.csv _always
148
	-$(call test,$(root)/map)
149
# Don't abort tester if only 2-step test fails, as it's often finicky
150
tests += test/VegBIEN.2-step.xml
151

    
152
test/import.out: $(directMaps) _always
153
	$(call test,$(map2db))
154
tests += test/import.out
155

    
156
test: _always $(tests) ;
157

    
158
.PRECIOUS: $(tests)
159

    
160
all += $(tests)
161

    
162
# Accepts a test output: make <test_output_path>-ok
163
%-ok: _always
164
	$(CP) $* $(call testRef,$*)
165

    
166
else
167
test: _always ;
168
endif
169

    
170
#####
171

    
172
# Each input type needs var $(mapEnv) and targets install, uninstall
173

    
174
ifneq ($(dbFile),)
175

    
176
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
177
db := $(notdir $(realpath .))
178

    
179
%.ref: %.ref.sql
180
	$(inputDbAsBien) $(db) <$< >$@
181
.PRECIOUS: %.ref
182

    
183
####
184

    
185
install: _always db ;
186

    
187
uninstall: _always rm_db ;
188

    
189
####
190

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

    
193
ifeq ($(dbEngineExt),my)
194

    
195
dbEngine := MySQL
196

    
197
bienPassword := $(shell cat $(root)/config/bien_password)
198
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
199
mysqlAsRoot := $(call mysqlAs,root)
200
dbAsBien := $(call mysqlAs,bien)
201

    
202
db: $(dbFile) _always
203
	echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
204
	-$(mysqlAsRoot) --database=$(db) <$<
205
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
206
# ignore errors in db import so that GRANT will still be run
207

    
208
rm_db: _always
209
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
210
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
211
# ignore errors if grant not defined
212

    
213
###
214

    
215
else
216
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
217
endif
218

    
219
# Must come after dbEngine is set
220
mapEnv := in_engine=$(dbEngine) in_database=$(db)
221

    
222
####
223

    
224
else
225
mapEnv :=
226
install: _always ;
227
uninstall: _always ;
228
endif
229

    
230
#####
231

    
232
# Must come after mapEnv is set
233
map := $(if $(mapEnv),env $(mapEnv) ,)$(root)/map
234
map2db := env out_database=vegbien $(map)
(2-2/2)