Project

General

Profile

1
##### Configuration
2

    
3
exts ?= csv xml
4
test_n ?= 2
5
tablesSort ?= plots organisms stems
6
mainCoreMapTry ?= organisms specimens
7

    
8
##### Vars/functions
9

    
10
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
11

    
12
# Make
13
SHELL := /bin/bash
14
subMake = $(MAKE) $(@F) --directory=$(@D)
15
+_ = $(+:_%=)
16
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
17

    
18
# Terminal
19
esc := '['
20
reset := $(esc)'0m'
21
emph := $(esc)'7m '
22
endEmph := ' '$(reset)
23

    
24
# Commands
25
CP = cp -p
26
DIFF = diff --unified=2
27

    
28
# Paths
29
root := $(selfDir_uZPPqC)..
30
mappings := $(root)/mappings
31
psqlAsBien := $(root)/bin/psql_vegbien
32

    
33
##### General targets
34

    
35
all: _always maps ;
36

    
37
.SUFFIXES:
38

    
39
_always:
40
.PHONY: _always
41

    
42
clean: _always
43
	$(RM) $(all)
44

    
45
remake: _always clean all ;
46

    
47
%.out: %.make _always
48
	./$* >$@
49
.PRECIOUS: %.out
50

    
51
$(root)/%: _always
52
	+$(subMake)
53

    
54
##### SVN
55

    
56
svn_props: _always
57
	svn propset svn:ignore $$'.~*\n*.log\n*.out\nsrc*' .
58
	$(if $(wildcard test/),svn propset svn:ignore $$'*.out\n*.xml' test)
59

    
60
##### Installation
61

    
62
reinstall: _always uninstall install ;
63

    
64
##### Maps
65

    
66
allViaMaps := $(filter-out maps/VegBIEN.%.csv,$(wildcard maps/*.csv))
67
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
68

    
69
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
70
mainCoreMap := $(firstword $(wildcard $(mainCoreMapTry:%=$(coreMap))))
71

    
72
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
73
viaMaps += $(filter-out $(viaMaps),$(wildcard maps/$(via).*.csv))
74

    
75
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
76
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
77
$(wildcard maps/VegBIEN.*.csv))
78
tables := $(directMaps:maps/VegBIEN.%.csv=%)
79

    
80
maps: $(directMaps) _always ;
81

    
82
.PRECIOUS: $(allViaMaps) $(directMaps)
83

    
84
all += $(autogenMaps)
85

    
86
maps/VegBIEN.%.csv: maps/$(via).%.csv $(coreMap)
87
	$(root)/bin/join_sort <$+ >$@
88
.PRECIOUS: maps/VegBIEN.%.csv $(coreMap)
89

    
90
##### Mapping
91

    
92
dbFile := $(firstword $(wildcard src/db.*.sql))
93
inputFiles := $(wildcard src/*.csv src/*.xml)
94

    
95
+maps = $(filter maps/% $(mappings)/%,$(+_))
96
<in = $(firstword $(filter-out $(+maps),$(+_)) $(wildcard $(exts:%=src/*.$*.%)))
97
map = $(if $(<in),<$(<in) ,$(if $(mapEnv),env $(mapEnv) ))$(root)/map $(+maps)
98
map2db = env out_database=vegbien $(map)
99

    
100
##### Import to VegBIEN
101

    
102
ifneq ($(dbFile)$(inputFiles),)
103

    
104
import: _always import-all verify ;
105

    
106
log = $(@:-all=)$(if $(n),.n=$(n),).log
107
import = -(set -x; "time" env commit=1 $(map2db)) \
108
$(if $(n),,>>$(log)) 2>&1$(if $(n),|tee -a $(log))
109
# ignore import errors, which are often benign (e.g. invalid date format)
110

    
111
ifneq ($(inputFiles),)
112
import-all: $(addprefix import-,$(tables)) _always ;
113
else
114
import-all: $(directMaps) _always
115
	$(import)
116
endif
117

    
118
import-%: maps/VegBIEN.%.csv _always
119
	$(import)
120
# default:
121
import-%: _always ;
122

    
123
else
124
import: _always ;
125
endif
126

    
127
logs := $(wildcard *.log)
128

    
129
rm_logs: _always
130
	$(RM) $(logs)
131

    
132
##### Verification of import
133

    
134
ifneq ($(wildcard verify.ref*),)
135

    
136
verify: verify.ref verify.out _always
137
	$(DIFF) $(+_)
138

    
139
all += verify.out
140

    
141
%.out: %.sql _always
142
	$(out_cmd)
143
.PRECIOUS: %.out
144
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off\
145
--pset=null=NULL <$< >$@
146

    
147
else
148
verify: _always ;
149
endif
150

    
151
##### Testing
152

    
153
ifneq ($(wildcard test/),)
154

    
155
tests :=
156

    
157
hasOwnRef = $(filter-out %.2-step.xml,$@)
158
testRef = $(1:.2-step.xml=.xml).ref
159

    
160
define test
161
@echo "Testing $(abspath $@)..."
162
>$@ env test=1 verbose=1 n=$(test_n) $(1)
163
@(set -x; $(DIFF) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
164
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
165
$(if $(hasOwnRef),\
166
echo $(emph)"To accept new test output:"$(endEmph);\
167
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
168
,\
169
echo $(emph)"Note: The preceding failed test is compared to another test's\
170
output"$(endEmph);\
171
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
172
)\
173
exit $$e;}
174
endef
175

    
176
test2File = $(call test,$(map))
177

    
178
test/$(via).xml: $(viaMaps) _always
179
	$(test2File)
180
tests += test/$(via).xml
181

    
182
test/VegBIEN.xml: $(directMaps) _always
183
	$(test2File)
184
tests += test/VegBIEN.xml
185

    
186
test/VegBIEN.2-step.xml: test/$(via).xml $(mainCoreMap) _always
187
	-$(test2File)
188
# Don't abort tester if only 2-step test fails, as it's often finicky
189
tests += test/VegBIEN.2-step.xml
190

    
191
test/import.out: $(directMaps) _always
192
	$(call test,$(map2db))
193
tests += test/import.out
194

    
195
test: _always $(tests) ;
196

    
197
.PRECIOUS: $(tests)
198

    
199
all += $(tests)
200

    
201
# Accepts a test output: make <test_output_path>-ok
202
%-ok: _always
203
	$(CP) $* $(call testRef,$*)
204

    
205
else
206
test: _always ;
207
endif
208

    
209
##### Input-type-specific
210

    
211
# Each input type needs var $(mapEnv) and targets install, uninstall
212

    
213
ifneq ($(dbFile),)
214

    
215
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
216
db := $(notdir $(realpath .))
217

    
218
%.ref: %.ref.sql
219
	$(inputDbAsBien) $(db) <$< >$@
220
.PRECIOUS: %.ref
221

    
222
#### Installation
223

    
224
install: _always db ;
225

    
226
uninstall: _always rm_db ;
227

    
228
#### DB-engine-specific
229

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

    
232
ifeq ($(dbEngineExt),my)
233

    
234
dbEngine := MySQL
235

    
236
bienPassword := $(shell cat $(root)/config/bien_password)
237
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
238
mysqlAsRoot := $(call mysqlAs,root)
239
dbAsBien := $(call mysqlAs,bien)
240

    
241
db: $(dbFile) _always
242
	echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
243
	-$(mysqlAsRoot) --database=$(db) <$<
244
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
245
# ignore errors in db import so that GRANT will still be run
246

    
247
rm_db: _always
248
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
249
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
250
# ignore errors if grant not defined
251

    
252
### Unrecognized DB engine
253

    
254
else
255
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
256
endif
257

    
258
# Must come after dbEngine is set
259
mapEnv := in_engine=$(dbEngine) in_database=$(db)
260

    
261
#### Unrecognized input type
262

    
263
else
264
install: _always ;
265
uninstall: _always ;
266
endif
(2-2/2)