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