1
|
##### Configuration
|
2
|
|
3
|
nolog ?= $(test)
|
4
|
exts ?= csv xml
|
5
|
test_n ?= 2
|
6
|
tablesSort ?= plots organisms stems
|
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
|
# System
|
19
|
date = $(shell date +"%Y-%m-%d-%H-%M-%S")
|
20
|
|
21
|
# Terminal
|
22
|
termCols := $(shell tput cols)
|
23
|
esc := '['
|
24
|
reset := $(esc)'0m'
|
25
|
emph := $(esc)'7m '
|
26
|
endEmph := ' '$(reset)
|
27
|
|
28
|
# Commands
|
29
|
CP = cp -p
|
30
|
diff = diff --unified=2
|
31
|
diffVerbose = $(if $(verbose),diff --side-by-side --left-column\
|
32
|
--width=$(termCols),$(diff))
|
33
|
|
34
|
# Paths
|
35
|
root := $(selfDir_uZPPqC)..
|
36
|
mappings := $(root)/mappings
|
37
|
psqlOpts := --set ON_ERROR_STOP=1 --quiet
|
38
|
psqlAsBien := $(root)/bin/psql_vegbien $(psqlOpts)
|
39
|
|
40
|
##### General targets
|
41
|
|
42
|
all: _always maps ;
|
43
|
|
44
|
.SUFFIXES:
|
45
|
|
46
|
_always:
|
47
|
.PHONY: _always
|
48
|
|
49
|
clean: _always
|
50
|
$(RM) $(all)
|
51
|
|
52
|
remake: _always clean all ;
|
53
|
|
54
|
%.out: %.make _always
|
55
|
./$* >$@
|
56
|
.PRECIOUS: %.out
|
57
|
|
58
|
$(root)/%: _always
|
59
|
+$(subMake)
|
60
|
|
61
|
##### SVN
|
62
|
|
63
|
svn_props: _always
|
64
|
svn propset svn:ignore $$'.~*\n*.log\n*.out\nsrc*' .
|
65
|
$(if $(wildcard maps/),svn propset svn:ignore $$'.~*' maps)
|
66
|
$(if $(wildcard test/),svn propset svn:ignore $$'*.out\n*.xml' test)
|
67
|
|
68
|
##### Installation
|
69
|
|
70
|
reinstall: _always uninstall install ;
|
71
|
|
72
|
##### Maps
|
73
|
|
74
|
fullViaMap := maps/%.full.csv
|
75
|
allViaMaps := $(filter-out maps/VegBIEN.%.csv $(fullViaMap),\
|
76
|
$(wildcard maps/*.csv))
|
77
|
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
|
78
|
|
79
|
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
|
80
|
selfMap := $(mappings)/$(via).self.%.csv
|
81
|
|
82
|
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
|
83
|
viaMaps += $(filter-out $(viaMaps) $(fullViaMap),$(wildcard maps/$(via).*.csv))
|
84
|
|
85
|
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
|
86
|
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
|
87
|
$(wildcard maps/VegBIEN.*.csv))
|
88
|
tables := $(directMaps:maps/VegBIEN.%.csv=%)
|
89
|
|
90
|
.PRECIOUS: $(allViaMaps) $(directMaps)
|
91
|
|
92
|
maps :=
|
93
|
|
94
|
maps/VegBIEN.%.csv: maps/$(via).%.csv $(coreMap)
|
95
|
$(root)/bin/join_union_sort <$+ >$@
|
96
|
.PRECIOUS: maps/VegBIEN.%.csv $(coreMap)
|
97
|
|
98
|
maps += $(autogenMaps)
|
99
|
|
100
|
maps/$(via).%.full.csv: maps/$(via).%.csv $(selfMap)
|
101
|
env ignore=1 $(root)/bin/union <$+|$(root)/bin/sort_map >$@
|
102
|
maps/$(via).%.full.csv: maps/$(via).%.csv # fallback if no self map
|
103
|
cp -p $< $@
|
104
|
.PRECIOUS: maps/$(via).%.full.csv maps/$(via).%.csv $(selfMap)
|
105
|
|
106
|
maps += $(patsubst maps/%.csv,maps/%.full.csv,$(viaMaps))
|
107
|
|
108
|
maps: $(maps) _always ;
|
109
|
|
110
|
all += $(maps)
|
111
|
|
112
|
##### Mapping
|
113
|
|
114
|
dbFile := $(firstword $(wildcard src/db.*.sql))
|
115
|
inputFiles := $(wildcard src/*.csv src/*.xml)
|
116
|
|
117
|
+maps = $(filter maps/% $(mappings)/%,$(+_))
|
118
|
<in = $(firstword $(filter-out $(+maps),$(+_)) $(wildcard $(exts:%=src/*.$*.%)))
|
119
|
map = $(if $(<in),<$(<in) ,$(if $(mapEnv),env $(mapEnv) ,$(error\
|
120
|
No input file src/*.$*.{$(exts)} )))$(root)/map $(+maps)
|
121
|
map2db = env out_database=vegbien $(map)
|
122
|
|
123
|
##### Import to VegBIEN
|
124
|
|
125
|
ifneq ($(dbFile)$(inputFiles),)
|
126
|
|
127
|
import: _always import-all ;
|
128
|
|
129
|
log = $(@:-all=)$(if $(n),.n=$(n),).$(date).log
|
130
|
import = -(set -x; "time" env commit=1 verbose=1 $(map2db)) $(if $(nolog),,\
|
131
|
$(if $(n),,>>$(log))) 2>&1$(if $(nolog),,$(if $(n),|tee -a $(log)))
|
132
|
# don't abort on import errors, which often relate to invalid input data
|
133
|
|
134
|
import-all: $(addprefix import-,$(tables)) _always ;
|
135
|
|
136
|
import-%: maps/VegBIEN.%.csv _always
|
137
|
$(import)
|
138
|
# default:
|
139
|
import-%: _always ;
|
140
|
|
141
|
else
|
142
|
import: _always ;
|
143
|
endif
|
144
|
|
145
|
##### Log files from import
|
146
|
|
147
|
logs := $(wildcard *.log)
|
148
|
|
149
|
rm_logs: _always
|
150
|
$(RM) $(logs)
|
151
|
|
152
|
##### Verification of import
|
153
|
|
154
|
ifneq ($(wildcard verify.ref*),)
|
155
|
|
156
|
verify: verify.ref verify.out _always
|
157
|
-$(diffVerbose) $(+_)
|
158
|
# don't abort on verification errors, which are expected during development
|
159
|
|
160
|
all += verify.out
|
161
|
|
162
|
%.out: %.sql _always
|
163
|
$(out_cmd)
|
164
|
.PRECIOUS: %.out
|
165
|
out_cmd = $(psqlAsBien) --set=datasource="'$(db)'" --no-align\
|
166
|
--field-separator=' ' --pset=footer=off --pset=null=NULL <$< >$@
|
167
|
|
168
|
ifneq ($(dbFile),)
|
169
|
%.ref: %.ref.sql
|
170
|
$(dbAsBien) $(db) <$< >$@
|
171
|
.PRECIOUS: %.ref
|
172
|
endif
|
173
|
|
174
|
else
|
175
|
verify: _always ;
|
176
|
endif
|
177
|
|
178
|
##### Testing
|
179
|
|
180
|
ifneq ($(wildcard test/),)
|
181
|
|
182
|
hasOwnRef = $(filter-out %.2-step.xml,$@)
|
183
|
testRef = $(1:.2-step.xml=.xml).ref
|
184
|
|
185
|
define runTest
|
186
|
@echo "Testing $(abspath $@)..."
|
187
|
>$@ env test=1 n=$(test_n) $(1)
|
188
|
@(set -x; $(diff) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
|
189
|
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
|
190
|
$(if $(hasOwnRef),\
|
191
|
echo $(emph)"To accept new test output:"$(endEmph);\
|
192
|
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
|
193
|
,\
|
194
|
echo $(emph)"Note: The preceding failed test is compared to another test's\
|
195
|
output"$(endEmph);\
|
196
|
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
|
197
|
)\
|
198
|
exit $$e;}
|
199
|
endef
|
200
|
|
201
|
test2File = $(call runTest,$(map))
|
202
|
|
203
|
tests :=
|
204
|
|
205
|
test/$(via).%.xml: maps/$(via).%.full.csv _always
|
206
|
$(test2File)
|
207
|
tests += test/$(via).%.xml
|
208
|
|
209
|
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
|
210
|
$(test2File)
|
211
|
tests += test/VegBIEN.%.xml
|
212
|
|
213
|
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _always
|
214
|
-$(test2File)
|
215
|
# Don't abort tester if only 2-step test fails, as it's often finicky
|
216
|
tests += test/VegBIEN.%.2-step.xml
|
217
|
|
218
|
test/import.%.out: maps/VegBIEN.%.csv _always
|
219
|
$(call runTest,$(map2db))
|
220
|
tests += test/import.%.out
|
221
|
|
222
|
.PRECIOUS: $(tests)
|
223
|
|
224
|
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
|
225
|
|
226
|
test: _always $(testOutputs) ;
|
227
|
|
228
|
all += $(testOutputs)
|
229
|
|
230
|
# Accepts a test output: make <test_output_path>-ok
|
231
|
%-ok: _always
|
232
|
$(CP) $* $(call testRef,$*)
|
233
|
|
234
|
else
|
235
|
test: _always ;
|
236
|
endif
|
237
|
|
238
|
##### Input-type-specific
|
239
|
|
240
|
# Each input type needs var $(mapEnv) and targets install, uninstall
|
241
|
|
242
|
ifneq ($(dbFile),)
|
243
|
|
244
|
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
|
245
|
db := $(notdir $(realpath .))
|
246
|
|
247
|
#### Installation
|
248
|
|
249
|
install: _always db ;
|
250
|
|
251
|
uninstall: _always rm_db ;
|
252
|
|
253
|
#### DB-engine-specific
|
254
|
|
255
|
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
|
256
|
|
257
|
ifeq ($(dbEngineExt),my)
|
258
|
|
259
|
dbEngine := MySQL
|
260
|
|
261
|
bienPassword := $(shell cat $(root)/config/bien_password)
|
262
|
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
|
263
|
mysqlAsRoot := $(call mysqlAs,root)
|
264
|
dbAsBien := $(call mysqlAs,bien)
|
265
|
|
266
|
dbExists = $(shell echo "SHOW DATABASES LIKE '$(db)';"|$(mysqlAsRoot))
|
267
|
|
268
|
define createDb
|
269
|
echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
|
270
|
-$(mysqlAsRoot) --database=$(db) <$<
|
271
|
endef
|
272
|
# ignore errors in db import so that GRANT will still be run
|
273
|
|
274
|
db: $(dbFile) _always
|
275
|
$(if $(dbExists),,$(createDb))
|
276
|
echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
|
277
|
|
278
|
rm_db: _always
|
279
|
-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
|
280
|
echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
|
281
|
# ignore errors if grant not defined
|
282
|
|
283
|
### Unrecognized DB engine
|
284
|
|
285
|
else
|
286
|
$(error The DB filename $(dbFile) must be db.my.sql)
|
287
|
endif
|
288
|
|
289
|
# Must come after dbEngine is set
|
290
|
mapEnv := in_engine=$(dbEngine) in_database=$(db)
|
291
|
|
292
|
#### Unrecognized input type
|
293
|
|
294
|
else
|
295
|
install: _always ;
|
296
|
uninstall: _always ;
|
297
|
endif
|