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
|
$(import_install_table)
|
153
|
import_install_table = (prefix=; . $(bin)/vegbien_dest; env schema=$(datasrc)\
|
154
|
table=$* $(bin)/csv2db $(catSrcs)$(if $(log), &>import/install.log))
|
155
|
|
156
|
##### Sources
|
157
|
|
158
|
srcs = $(shell $(bin)/sort_filenames $(wildcard $(exts:%=src/*.$*.%)))
|
159
|
isXml = $(filter %.xml,$(firstword $(srcs)))
|
160
|
catSrcs = $(bin)/cat$(if $(isXml),,_csv) $(srcs)
|
161
|
withCatSrcs = $(catSrcs:$(bin)/%=$(bin)/with_%) --
|
162
|
|
163
|
# Usage: `make {--silent|-s} cat` (removes echoed make commands)
|
164
|
cat: $(addprefix cat-,$(tables)) _always ;
|
165
|
|
166
|
cat-%: _always
|
167
|
$(catSrcs)
|
168
|
|
169
|
##### Maps building
|
170
|
|
171
|
# Maps to (try to) build are added to one of these
|
172
|
maps :=
|
173
|
createOnlyMaps :=
|
174
|
|
175
|
srcMaps := $(wildcard maps/src.*.csv)
|
176
|
|
177
|
mkSrcMap = $(catSrcs)|env datasrc=$(datasrc) $(bin)/src_map >$@
|
178
|
|
179
|
# Autogen src maps with known table names
|
180
|
maps/src.%.csv: # no prereqs so only built if doesn't exist
|
181
|
$(if $(srcs),$(if $(isXml),,$(mkSrcMap)))
|
182
|
# only build if CSV srcs exist for that table name
|
183
|
# Try all table names
|
184
|
createOnlyMaps += $(tablesSort:%=maps/src.%.csv)
|
185
|
|
186
|
# Must come before $(root)/% to override it
|
187
|
$(coreSelfMap): _always
|
188
|
-+$(subMake)
|
189
|
# ignore errors if $(coreSelfMap) does not exist
|
190
|
|
191
|
# Via maps cleanup
|
192
|
ifneq ($(filter maps/.%.last_cleanup,$(MAKECMDGOALS)),)
|
193
|
maps/.$(via).%.csv.last_cleanup: maps/$(via).%.csv $(coreMap)
|
194
|
$(bin)/in_place $< env ignore=1 $(bin)/subtract $(word 2,$+) 0 1
|
195
|
touch $@
|
196
|
else
|
197
|
$(viaMaps): _always
|
198
|
$(selfMake) $(@:maps/%=maps/.%.last_cleanup)
|
199
|
endif
|
200
|
|
201
|
joinSrcMap = $(if $(wildcard maps/src.$*.csv),$(bin)/in_place $@\
|
202
|
$(bin)/intersect maps/src.$*.csv 0)
|
203
|
|
204
|
makeFullCsv = $(if $(shell test -e $(word 2,$+) && echo t),\
|
205
|
env ignore=1 $(bin)/union <$+|,<$< )$(bin)/sort_map >$@
|
206
|
# can't use $(wildcard) because it won't recheck file after $(coreSelfMap) runs
|
207
|
|
208
|
maps/$(via).%.full.csv: maps/$(via).%.csv $(coreSelfMap)
|
209
|
$(makeFullCsv)
|
210
|
$(joinSrcMap)
|
211
|
maps += $(patsubst maps/%.csv,maps/%.full.csv,$(viaMaps))
|
212
|
|
213
|
maps/VegBIEN.%.csv: maps/$(via).%.full.csv $(coreMap)
|
214
|
$(bin)/join <$+|$(bin)/sort_map >$@
|
215
|
maps += $(autogenMaps)
|
216
|
|
217
|
maps: $(createOnlyMaps) $(maps) _always ;
|
218
|
|
219
|
all += $(maps)
|
220
|
|
221
|
##### Maps validation
|
222
|
|
223
|
missing_mappings: _always missing_join_mappings missing_input_mappings ;
|
224
|
|
225
|
missing_%_mappings: _always # stem is one of join|input
|
226
|
@echo $(emph)"Missing $* mappings:"$(endEmph)
|
227
|
@+$(selfMake) remake 2>&1\
|
228
|
|$(SED) -n 's/^.*No.* $* mapping for ([0-9A-Za-z_-]+).*$$/\1/p'\
|
229
|
$(if $(filter join,$*),|$(bin)/ucase_first 0)|sort|uniq
|
230
|
|
231
|
##### External dependencies
|
232
|
|
233
|
$(root)/%: _always
|
234
|
+$(subMake)
|
235
|
.PRECIOUS: $(root)/% # let ext. dir's Makefile decide whether to delete on error
|
236
|
|
237
|
##### Mapping
|
238
|
|
239
|
inputFiles := $(wildcard $(exts:%=src/*.%))
|
240
|
ifeq ($(inputFiles),)
|
241
|
dbExport := $(firstword $(wildcard src/db.*.sql))
|
242
|
endif
|
243
|
|
244
|
+maps = $(filter maps/% $(mappings)/%,$(+_))
|
245
|
<in = $(firstword $(filter-out $(+maps),$(+_)))
|
246
|
map = $(if $(<in),<$(<in),\
|
247
|
$(if $(srcs),$(withCatSrcs),\
|
248
|
$(if $(mapEnv),env $(mapEnv),\
|
249
|
$(error No input file src/*.$*.{$(exts)}))))\
|
250
|
$(root)/map $(+maps)
|
251
|
map2db = env out_database=vegbien $(map)
|
252
|
|
253
|
##### Import to VegBIEN
|
254
|
|
255
|
ifneq ($(dbExport)$(inputFiles),)
|
256
|
|
257
|
profileTest = $(if $(profile),$(if $(test),1))
|
258
|
profileOnly = -env profile_to=/dev/fd/3 $(map2db) 3>&1 1>&2|\
|
259
|
$(bin)/profile_stats /dev/fd/0
|
260
|
|
261
|
log_ = import/$*$(if $(n),.n=$(n),).$(date).log
|
262
|
trace = $(log_:.log=.trace)
|
263
|
import = -$(if $(profileTest),$(profileOnly),(set -x; "time" env commit=1\
|
264
|
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
|
265
|
$(if $(n),,>>$(log_))) 2>&1$(if $(log),$(if $(n),|tee -a $(log_))))
|
266
|
# don't abort on import errors, which often relate to invalid input data
|
267
|
|
268
|
import: $(addprefix import-,$(tables)) _always ;
|
269
|
|
270
|
import-%: maps/VegBIEN.%.csv _always
|
271
|
$(import)
|
272
|
# default:
|
273
|
import-%: _always ;
|
274
|
|
275
|
else
|
276
|
import: _always ;
|
277
|
endif
|
278
|
|
279
|
##### Log files from import
|
280
|
|
281
|
logs := $(wildcard import/*.log import/*.trace)
|
282
|
|
283
|
rm_logs: _always
|
284
|
$(RM) $(logs)
|
285
|
|
286
|
##### Verification of import
|
287
|
|
288
|
verify: $(addprefix verify-,$(tables)) _always ;
|
289
|
|
290
|
verify-%: verify/%.ref verify/%.out _always
|
291
|
-$(diffVerbose) $(+_)
|
292
|
# don't abort on verification errors, which are expected during development
|
293
|
# default:
|
294
|
verify-%: verify/%.out _always
|
295
|
$(if $(shell test -e $< && echo t),cat $<)
|
296
|
# don't run if verify/%.out's default do-nothing action was used
|
297
|
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
|
298
|
|
299
|
verify = $(if $(reverify),"time" $(psqlAsBien) --set=datasource="'$(datasrc)'"\
|
300
|
--no-align --field-separator=$$'\t' --pset=footer=off --pset=null=NULL <$< >$@)
|
301
|
|
302
|
verify/%.out: $(mappings)/verify.%.sql _always
|
303
|
$(verify)
|
304
|
# default:
|
305
|
verify/%.out: _always ;
|
306
|
|
307
|
all += $(wildcard verify/*.out)
|
308
|
|
309
|
ifneq ($(dbExport),)
|
310
|
%.ref: %.ref.sql
|
311
|
$(dbAsBien) $(db) <$< >$@
|
312
|
endif
|
313
|
|
314
|
##### Editing import
|
315
|
|
316
|
import/rotate: _always
|
317
|
echo "UPDATE party SET organizationname = organizationname||'.$(date)'\
|
318
|
WHERE organizationname = '$(datasrc)';"|$(psqlAsBien)
|
319
|
|
320
|
import/rm: _always
|
321
|
echo "DELETE FROM party WHERE organizationname = '$(datasrc)';"|\
|
322
|
$(psqlAsBien)
|
323
|
|
324
|
##### Testing
|
325
|
|
326
|
hasOwnRef = $(filter-out %.2-step.xml,$@)
|
327
|
testRef = $(1:.2-step.xml=.xml).ref
|
328
|
|
329
|
define runTest
|
330
|
@echo "Testing $(abspath $@)..."
|
331
|
>$@ env test=1 n=$(test_n) $(1)
|
332
|
@(set -x; $(diff) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
|
333
|
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
|
334
|
$(if $(hasOwnRef),\
|
335
|
echo $(emph)"To accept new test output:"$(endEmph);\
|
336
|
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
|
337
|
,\
|
338
|
echo $(emph)"Note: The preceding failed test is compared to another test's\
|
339
|
output"$(endEmph);\
|
340
|
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
|
341
|
)\
|
342
|
exit $$e;}
|
343
|
endef
|
344
|
|
345
|
test2File = $(call runTest,$(map))
|
346
|
|
347
|
tests :=
|
348
|
|
349
|
test/$(via).%.xml: maps/$(via).%.full.csv _always
|
350
|
$(test2File)
|
351
|
tests += test/$(via).%.xml
|
352
|
|
353
|
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
|
354
|
$(test2File)
|
355
|
tests += test/VegBIEN.%.xml
|
356
|
|
357
|
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _always
|
358
|
-$(test2File)
|
359
|
# Don't abort tester if only 2-step test fails, as it's often finicky
|
360
|
tests += test/VegBIEN.%.2-step.xml
|
361
|
|
362
|
test/import.%.out: maps/VegBIEN.%.csv _always
|
363
|
$(call runTest,$(map2db))
|
364
|
tests += test/import.%.out
|
365
|
|
366
|
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
|
367
|
|
368
|
.PRECIOUS: $(testOutputs) # save outputs of failed tests so they can be accepted
|
369
|
|
370
|
test: _always $(testOutputs) ;
|
371
|
|
372
|
all += $(testOutputs)
|
373
|
|
374
|
# Accepts a test output: make <test_output_path>-ok
|
375
|
%-ok: _always
|
376
|
$(CP) $* $(call testRef,$*)
|
377
|
|
378
|
##### Input-type-specific
|
379
|
|
380
|
# Each input type needs var $(mapEnv) and targets src/install, src/uninstall
|
381
|
|
382
|
#### DB export
|
383
|
|
384
|
ifneq ($(dbExport),)
|
385
|
|
386
|
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbExport)))))
|
387
|
db := $(datasrc)
|
388
|
|
389
|
### Installation
|
390
|
|
391
|
src/install: _always db ;
|
392
|
|
393
|
src/uninstall: _always rm_db ;
|
394
|
|
395
|
### DB-engine-specific
|
396
|
|
397
|
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
|
398
|
|
399
|
ifeq ($(dbEngineExt),my)
|
400
|
|
401
|
dbEngine := MySQL
|
402
|
|
403
|
bienPassword := $(shell cat $(root)/config/bien_password)
|
404
|
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
|
405
|
mysqlAsRoot := $(call mysqlAs,root)
|
406
|
dbAsBien := $(call mysqlAs,bien)
|
407
|
|
408
|
dbExists = $(shell echo "SHOW DATABASES LIKE '$(db)';"|$(mysqlAsRoot))
|
409
|
|
410
|
define createDb
|
411
|
echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
|
412
|
-$(mysqlAsRoot) --database=$(db) <$<
|
413
|
endef
|
414
|
# ignore errors in db import so that GRANT will still be run
|
415
|
|
416
|
db: $(dbExport) _always
|
417
|
$(if $(dbExists),,$(createDb))
|
418
|
echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
|
419
|
|
420
|
rm_db: _always
|
421
|
-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
|
422
|
echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
|
423
|
# ignore errors if grant not defined
|
424
|
|
425
|
## Unrecognized DB engine
|
426
|
|
427
|
else
|
428
|
$(error The DB filename $(dbExport) must be db.my.sql)
|
429
|
endif
|
430
|
|
431
|
### Other input types
|
432
|
|
433
|
else
|
434
|
|
435
|
src/install: _always ;
|
436
|
src/uninstall: _always ;
|
437
|
|
438
|
endif
|
439
|
|
440
|
#### DB connection info
|
441
|
|
442
|
ifneq ($(dbEngine),)
|
443
|
# Must come after dbEngine is set
|
444
|
mapEnv := in_engine=$(dbEngine) in_database=$(db)
|
445
|
endif
|