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