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