1
|
##### Configuration
|
2
|
|
3
|
log ?= $(if $(test),,1)
|
4
|
profile ?=
|
5
|
reverify ?= 1
|
6
|
exts ?= csv tsv txt xml
|
7
|
test_n ?= 2
|
8
|
tablesSort ?= plots organisms stems specimens
|
9
|
|
10
|
##### Vars/functions
|
11
|
|
12
|
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
|
13
|
|
14
|
# Make
|
15
|
SHELL := /bin/bash
|
16
|
selfMake = $(MAKE) --makefile=../input.Makefile
|
17
|
subMake = $(MAKE) $(@F) --directory=$(@D)
|
18
|
+_ = $(+:_%=)
|
19
|
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
|
20
|
|
21
|
# OS
|
22
|
os := $(shell uname)
|
23
|
forOs = $(patsubst %,%-$(filter Linux Darwin,$(os)),$(1))
|
24
|
|
25
|
# Formatting
|
26
|
SED = sed -$(if $(filter Darwin,$(os)),E,r)
|
27
|
|
28
|
# System
|
29
|
date = $(shell date +"%Y-%m-%d-%H-%M-%S")
|
30
|
|
31
|
# Terminal
|
32
|
termCols := $(shell tput cols)
|
33
|
esc := '['
|
34
|
reset := $(esc)'0m'
|
35
|
emph := $(esc)'7m '
|
36
|
endEmph := ' '$(reset)
|
37
|
|
38
|
# Commands
|
39
|
MKDIR = mkdir -p
|
40
|
mkdir = $(MKDIR) $(@D)
|
41
|
CP = cp -p
|
42
|
diff = diff --unified=2
|
43
|
diffVerbose = $(if $(verbose),diff --side-by-side --left-column\
|
44
|
--width=$(termCols),$(diff))
|
45
|
|
46
|
# Paths
|
47
|
datasrc := $(notdir $(realpath .))
|
48
|
root := $(selfDir_uZPPqC)..
|
49
|
bin := $(root)/bin
|
50
|
mappings := $(root)/mappings
|
51
|
|
52
|
# Commands
|
53
|
selfMap = $(bin)/cols 0 0
|
54
|
psqlOpts := --set ON_ERROR_STOP=1 --quiet
|
55
|
psqlAsBien := $(bin)/psql_vegbien $(psqlOpts)
|
56
|
|
57
|
# SVN
|
58
|
addDir = $(if $(wildcard $(1)/),svn add --depth=empty $(1),svn mkdir $(1))
|
59
|
setSvnIgnore = svn propset svn:ignore $(2) $(1)
|
60
|
define addDirWithIgnore
|
61
|
$(addDir)
|
62
|
$(setSvnIgnore)
|
63
|
endef
|
64
|
|
65
|
##### General targets
|
66
|
|
67
|
all: _always maps ;
|
68
|
|
69
|
.SUFFIXES: # turn off built-in suffix rules
|
70
|
.SECONDARY: # don't automatically delete intermediate files
|
71
|
.DELETE_ON_ERROR: # delete target if recipe fails
|
72
|
|
73
|
_always:
|
74
|
.PHONY: _always
|
75
|
|
76
|
clean: _always
|
77
|
$(RM) $(all)
|
78
|
|
79
|
remake: _always clean
|
80
|
+$(selfMake)
|
81
|
# re-run make so that cache of existing files is reset
|
82
|
|
83
|
make_script = ./$< >$@
|
84
|
|
85
|
%/: % _always ;
|
86
|
|
87
|
# Must come before `%: %.make` to override it
|
88
|
src/%: src/%.make _always
|
89
|
(set -x; $(make_script)) 2>>$<.log
|
90
|
.PRECIOUS: src/% # save partial outputs of aborted src make scripts
|
91
|
|
92
|
%: %.make _always
|
93
|
$(make_script)
|
94
|
|
95
|
##### SVN
|
96
|
|
97
|
add: _always
|
98
|
$(call setSvnIgnore,.,$$'')
|
99
|
$(call addDirWithIgnore,src,$$'*')
|
100
|
$(call addDirWithIgnore,maps,$$'.~*')
|
101
|
$(call addDirWithIgnore,import,$$'*')
|
102
|
$(call addDirWithIgnore,test,$$'*.out\n*.xml')
|
103
|
$(call addDirWithIgnore,verify,$$'*.out')
|
104
|
|
105
|
##### Installation
|
106
|
|
107
|
reinstall: _always uninstall install ;
|
108
|
|
109
|
##### Existing maps discovery
|
110
|
|
111
|
srcMap := maps/src.%.csv
|
112
|
fullViaMap := maps/%.full.csv
|
113
|
directMap := maps/VegBIEN.%.csv
|
114
|
allViaMaps := $(filter-out $(srcMap) $(fullViaMap) $(directMap),\
|
115
|
$(wildcard maps/*.csv))
|
116
|
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
|
117
|
|
118
|
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
|
119
|
coreSelfMap := $(mappings)/$(via).self.%.csv
|
120
|
noEmptyMap := $(mappings)/$(via)-VegBIEN.%.no_empty.csv
|
121
|
|
122
|
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
|
123
|
viaMaps += $(filter-out $(viaMaps) $(fullViaMap),$(wildcard maps/$(via).*.csv))
|
124
|
|
125
|
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
|
126
|
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
|
127
|
$(wildcard maps/VegBIEN.*.csv))
|
128
|
tables := $(directMaps:maps/VegBIEN.%.csv=%)
|
129
|
|
130
|
##### Sources
|
131
|
|
132
|
srcs = $(shell $(bin)/sort_filenames $(wildcard $(exts:%=src/*.$*.%)))
|
133
|
isXml = $(filter %.xml,$(firstword $(srcs)))
|
134
|
catSrcs = $(bin)/cat$(if $(isXml),,_csv) $(srcs)
|
135
|
withCatSrcs = $(catSrcs:$(bin)/%=$(bin)/with_%) --
|
136
|
|
137
|
cat: $(addprefix cat-,$(tables)) _always ;
|
138
|
|
139
|
cat-%: _always
|
140
|
$(catSrcs)
|
141
|
|
142
|
##### Maps building
|
143
|
|
144
|
# Maps to (try to) build are added to this
|
145
|
maps :=
|
146
|
|
147
|
srcMaps := $(wildcard maps/src.*.csv)
|
148
|
|
149
|
mkSrcMap = env datasrc=$(datasrc) $(withCatSrcs) $(bin)/src_map >$@
|
150
|
|
151
|
# Autogen src maps with known table names
|
152
|
maps/src.%.csv: # no prereqs so only built if doesn't exist
|
153
|
$(if $(srcs),$(if $(isXml),,$(mkSrcMap)))
|
154
|
# only build if CSV srcs exist for that table name
|
155
|
# Try all table names
|
156
|
maps += $(tablesSort:%=maps/src.%.csv)
|
157
|
|
158
|
# Must come before $(root)/% to override it
|
159
|
$(coreSelfMap): _always
|
160
|
-+$(subMake)
|
161
|
# ignore errors if $(coreSelfMap) does not exist
|
162
|
|
163
|
# Via maps cleanup
|
164
|
ifneq ($(filter maps/.%.last_cleanup,$(MAKECMDGOALS)),)
|
165
|
maps/.$(via).%.csv.last_cleanup: maps/$(via).%.csv $(coreMap)
|
166
|
$(bin)/in_place $< $(bin)/subtract $(word 2,$+) 0 1
|
167
|
touch $@
|
168
|
# default:
|
169
|
maps/.$(via).%.csv.last_cleanup: ;
|
170
|
else
|
171
|
$(viaMaps): _always
|
172
|
$(selfMake) $(@:maps/%=maps/.%.last_cleanup)
|
173
|
endif
|
174
|
|
175
|
joinSrcMap = $(if $(wildcard maps/src.$*.csv),$(bin)/in_place $@\
|
176
|
$(bin)/intersect maps/src.$*.csv 0)
|
177
|
|
178
|
makeFullCsv = $(if $(shell test -e $(word 2,$+) && echo t),\
|
179
|
env ignore=1 $(bin)/union <$+|$(bin)/sort_map >$@,$(CP) $< $@)
|
180
|
# can't use $(wildcard) because it won't recheck file after $(coreSelfMap) runs
|
181
|
|
182
|
maps/$(via).%.full.csv: maps/$(via).%.csv $(coreSelfMap)
|
183
|
$(makeFullCsv)
|
184
|
$(joinSrcMap)
|
185
|
maps += $(patsubst maps/%.csv,maps/%.full.csv,$(viaMaps))
|
186
|
|
187
|
maps/VegBIEN.%.csv: maps/$(via).%.full.csv $(coreMap)
|
188
|
$(bin)/join <$+|$(bin)/sort_map >$@
|
189
|
maps += $(autogenMaps)
|
190
|
|
191
|
maps: $(maps) _always ;
|
192
|
|
193
|
all += $(maps)
|
194
|
|
195
|
##### Maps validation
|
196
|
|
197
|
missing_mappings: _always missing_join_mappings missing_input_mappings ;
|
198
|
|
199
|
missing_%_mappings: _always # stem is one of join|input
|
200
|
@echo $(emph)"Missing $* mappings:"$(endEmph)
|
201
|
@+$(selfMake) remake 2>&1\
|
202
|
|$(SED) -n 's/^.*No $* mapping for ([0-9A-Za-z_-]+).*$$/\1/p'\
|
203
|
$(if $(filter join,$*),|$(bin)/ucase_first 0)|sort|uniq
|
204
|
|
205
|
##### External dependencies
|
206
|
|
207
|
$(root)/%: _always
|
208
|
+$(subMake)
|
209
|
.PRECIOUS: $(root)/% # let ext. dir's Makefile decide whether to delete on error
|
210
|
|
211
|
##### Mapping
|
212
|
|
213
|
dbExport := $(firstword $(wildcard src/db.*.sql))
|
214
|
inputFiles := $(wildcard $(exts:%=src/*.%))
|
215
|
|
216
|
+maps = $(filter maps/% $(mappings)/%,$(+_))
|
217
|
<in = $(firstword $(filter-out $(+maps),$(+_)))
|
218
|
map = $(if $(<in),<$(<in),\
|
219
|
$(if $(srcs),$(withCatSrcs),\
|
220
|
$(if $(mapEnv),env $(mapEnv),\
|
221
|
$(error No input file src/*.$*.{$(exts)}))))\
|
222
|
$(root)/map $(+maps)
|
223
|
map2db = env out_database=vegbien $(map)
|
224
|
|
225
|
##### Import to VegBIEN
|
226
|
|
227
|
ifneq ($(dbExport)$(inputFiles),)
|
228
|
|
229
|
log_ = import/$*$(if $(n),.n=$(n),).$(date).log
|
230
|
trace = $(log_:.log=.trace)
|
231
|
import = -(set -x; "time" env commit=1\
|
232
|
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
|
233
|
$(if $(n),,>>$(log_))) 2>&1$(if $(log),$(if $(n),|tee -a $(log_)))
|
234
|
# don't abort on import errors, which often relate to invalid input data
|
235
|
|
236
|
import: $(addprefix import-,$(tables)) _always ;
|
237
|
|
238
|
import-%: maps/VegBIEN.%.csv _always
|
239
|
$(import)
|
240
|
# default:
|
241
|
import-%: _always ;
|
242
|
|
243
|
else
|
244
|
import: _always ;
|
245
|
endif
|
246
|
|
247
|
##### Log files from import
|
248
|
|
249
|
logs := $(wildcard import/*.log import/*.trace)
|
250
|
|
251
|
rm_logs: _always
|
252
|
$(RM) $(logs)
|
253
|
|
254
|
##### Verification of import
|
255
|
|
256
|
verify: $(addprefix verify-,$(tables)) _always ;
|
257
|
|
258
|
verify-%: verify/%.ref verify/%.out _always
|
259
|
-$(diffVerbose) $(+_)
|
260
|
# don't abort on verification errors, which are expected during development
|
261
|
# default:
|
262
|
verify-%: verify/%.out _always
|
263
|
$(if $(shell test -e $< && echo t),cat $<)
|
264
|
# don't run if verify/%.out's default do-nothing action was used
|
265
|
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
|
266
|
|
267
|
verify = $(if $(reverify),"time" $(psqlAsBien) --set=datasource="'$(datasrc)'"\
|
268
|
--no-align --field-separator=$$'\t' --pset=footer=off --pset=null=NULL <$< >$@)
|
269
|
|
270
|
verify/%.out: $(mappings)/verify.%.sql _always
|
271
|
$(verify)
|
272
|
# default:
|
273
|
verify/%.out: _always ;
|
274
|
|
275
|
all += $(wildcard verify/*.out)
|
276
|
|
277
|
ifneq ($(dbExport),)
|
278
|
%.ref: %.ref.sql
|
279
|
$(dbAsBien) $(db) <$< >$@
|
280
|
endif
|
281
|
|
282
|
##### Editing import
|
283
|
|
284
|
import/rotate: _always
|
285
|
echo "UPDATE party SET organizationname = organizationname||'.$(date)'\
|
286
|
WHERE organizationname = '$(db)';"|$(psqlAsBien)
|
287
|
|
288
|
import/rm: _always
|
289
|
echo "DELETE FROM party WHERE organizationname = '$(db)';"|$(psqlAsBien)
|
290
|
|
291
|
##### Testing
|
292
|
|
293
|
hasOwnRef = $(filter-out %.2-step.xml,$@)
|
294
|
testRef = $(1:.2-step.xml=.xml).ref
|
295
|
|
296
|
define runTest
|
297
|
@echo "Testing $(abspath $@)..."
|
298
|
>$@ env test=1 n=$(test_n) $(1)
|
299
|
@(set -x; $(diff) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
|
300
|
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
|
301
|
$(if $(hasOwnRef),\
|
302
|
echo $(emph)"To accept new test output:"$(endEmph);\
|
303
|
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
|
304
|
,\
|
305
|
echo $(emph)"Note: The preceding failed test is compared to another test's\
|
306
|
output"$(endEmph);\
|
307
|
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
|
308
|
)\
|
309
|
exit $$e;}
|
310
|
endef
|
311
|
|
312
|
test2File = $(call runTest,$(map))
|
313
|
|
314
|
tests :=
|
315
|
|
316
|
test/$(via).%.xml: maps/$(via).%.full.csv _always
|
317
|
$(test2File)
|
318
|
tests += test/$(via).%.xml
|
319
|
|
320
|
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
|
321
|
$(test2File)
|
322
|
tests += test/VegBIEN.%.xml
|
323
|
|
324
|
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _always
|
325
|
-$(test2File)
|
326
|
# Don't abort tester if only 2-step test fails, as it's often finicky
|
327
|
tests += test/VegBIEN.%.2-step.xml
|
328
|
|
329
|
test/import.%.out: maps/VegBIEN.%.csv _always
|
330
|
$(call runTest,$(map2db))
|
331
|
tests += test/import.%.out
|
332
|
|
333
|
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
|
334
|
|
335
|
.PRECIOUS: $(testOutputs) # save outputs of failed tests so they can be accepted
|
336
|
|
337
|
test: _always $(testOutputs) ;
|
338
|
|
339
|
all += $(testOutputs)
|
340
|
|
341
|
# Accepts a test output: make <test_output_path>-ok
|
342
|
%-ok: _always
|
343
|
$(CP) $* $(call testRef,$*)
|
344
|
|
345
|
##### Input-type-specific
|
346
|
|
347
|
# Each input type needs var $(mapEnv) and targets install, uninstall
|
348
|
|
349
|
#### DB export
|
350
|
|
351
|
ifneq ($(dbExport),)
|
352
|
|
353
|
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbExport)))))
|
354
|
db := $(datasrc)
|
355
|
|
356
|
### Installation
|
357
|
|
358
|
install: _always db ;
|
359
|
|
360
|
uninstall: _always rm_db ;
|
361
|
|
362
|
### DB-engine-specific
|
363
|
|
364
|
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
|
365
|
|
366
|
ifeq ($(dbEngineExt),my)
|
367
|
|
368
|
dbEngine := MySQL
|
369
|
|
370
|
bienPassword := $(shell cat $(root)/config/bien_password)
|
371
|
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
|
372
|
mysqlAsRoot := $(call mysqlAs,root)
|
373
|
dbAsBien := $(call mysqlAs,bien)
|
374
|
|
375
|
dbExists = $(shell echo "SHOW DATABASES LIKE '$(db)';"|$(mysqlAsRoot))
|
376
|
|
377
|
define createDb
|
378
|
echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
|
379
|
-$(mysqlAsRoot) --database=$(db) <$<
|
380
|
endef
|
381
|
# ignore errors in db import so that GRANT will still be run
|
382
|
|
383
|
db: $(dbExport) _always
|
384
|
$(if $(dbExists),,$(createDb))
|
385
|
echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
|
386
|
|
387
|
rm_db: _always
|
388
|
-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
|
389
|
echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
|
390
|
# ignore errors if grant not defined
|
391
|
|
392
|
## Unrecognized DB engine
|
393
|
|
394
|
else
|
395
|
$(error The DB filename $(dbExport) must be db.my.sql)
|
396
|
endif
|
397
|
|
398
|
### Other input types
|
399
|
|
400
|
else
|
401
|
|
402
|
install: _always ;
|
403
|
uninstall: _always ;
|
404
|
|
405
|
endif
|
406
|
|
407
|
#### DB connection info
|
408
|
|
409
|
ifneq ($(dbEngine),)
|
410
|
# Must come after dbEngine is set
|
411
|
mapEnv := in_engine=$(dbEngine) in_database=$(db)
|
412
|
endif
|