Project

General

Profile

1 3761 aaronmk
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
2
root := $(selfDir_uZPPqC)..
3
include $(root)/lib/common.Makefile
4
5
6 783 aaronmk
##### Configuration
7
8 1921 aaronmk
# Command line
9 1190 aaronmk
log ?= $(if $(test),,1)
10 1257 aaronmk
profile ?=
11 1983 aaronmk
quiet ?=
12 1620 aaronmk
reverify ?= 1
13 1990 aaronmk
use_staged ?= $(by_col)
14 1921 aaronmk
15
# Makefile
16
tablesSort ?= plots organisms stems specimens
17 1386 aaronmk
exts ?= csv tsv txt xml
18 783 aaronmk
test_n ?= 2
19
20
##### Vars/functions
21
22 1821 aaronmk
# Paths
23
datasrc := $(notdir $(realpath .))
24
bin := $(root)/bin
25
mappings := $(root)/mappings
26
27 368 aaronmk
# Make
28 640 aaronmk
SHELL := /bin/bash
29 1509 aaronmk
selfMake = $(MAKE) --makefile=../input.Makefile
30 1821 aaronmk
subMake = $(MAKE) $(@:$(root)/%=%) --directory=$(root)
31 404 aaronmk
+_ = $(+:_%=)
32 368 aaronmk
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
33
34 1742 aaronmk
# 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 790 aaronmk
# System
42
date = $(shell date +"%Y-%m-%d-%H-%M-%S")
43
44 640 aaronmk
# Terminal
45 1184 aaronmk
termCols := $(shell tput cols)
46 640 aaronmk
esc := '['
47
reset := $(esc)'0m'
48
emph := $(esc)'7m '
49
endEmph := ' '$(reset)
50
51 368 aaronmk
# Commands
52 1245 aaronmk
MKDIR = mkdir -p
53
mkdir = $(MKDIR) $(@D)
54 395 aaronmk
CP = cp -p
55 1184 aaronmk
diff = diff --unified=2
56 3721 aaronmk
diffIgnoreSpace = $(diff) --ignore-space-change
57 1184 aaronmk
diffVerbose = $(if $(verbose),diff --side-by-side --left-column\
58
--width=$(termCols),$(diff))
59 368 aaronmk
60 1918 aaronmk
# BIEN commands
61 1524 aaronmk
selfMap = $(bin)/cols 0 0
62 1081 aaronmk
psqlOpts := --set ON_ERROR_STOP=1 --quiet
63 1289 aaronmk
psqlAsBien := $(bin)/psql_vegbien $(psqlOpts)
64 353 aaronmk
65 1594 aaronmk
# SVN
66
addDir = $(if $(wildcard $(1)/),svn add --depth=empty $(1),svn mkdir $(1))
67 1771 aaronmk
setSvnIgnore = svn propset svn:ignore $(2) $(1)
68 1594 aaronmk
define addDirWithIgnore
69
$(addDir)
70 1771 aaronmk
$(setSvnIgnore)
71 1594 aaronmk
endef
72
73 783 aaronmk
##### General targets
74 250 aaronmk
75 418 aaronmk
all: _always maps ;
76 247 aaronmk
77 383 aaronmk
clean: _always
78 256 aaronmk
	$(RM) $(all)
79
80 1742 aaronmk
remake: _always clean
81
	+$(selfMake)
82
# re-run make so that cache of existing files is reset
83
84 1627 aaronmk
make_script = ./$< >$@
85 368 aaronmk
86 1604 aaronmk
%/: % _always ;
87
88 1627 aaronmk
# Must come before `%: %.make` to override it
89
src/%: src/%.make _always
90 3317 aaronmk
	(set -x; $(make_script)) 2>>$<.log.sql
91 1627 aaronmk
.PRECIOUS: src/% # save partial outputs of aborted src make scripts
92
93
%: %.make _always
94
	$(make_script)
95
96 783 aaronmk
##### SVN
97 250 aaronmk
98 1594 aaronmk
add: _always
99 1771 aaronmk
	$(call setSvnIgnore,.,$$'')
100 1594 aaronmk
	$(call addDirWithIgnore,src,$$'*')
101
	$(call addDirWithIgnore,maps,$$'.~*')
102
	$(call addDirWithIgnore,import,$$'*')
103
	$(call addDirWithIgnore,test,$$'*.out\n*.xml')
104
	$(call addDirWithIgnore,verify,$$'*.out')
105 766 aaronmk
106 1969 aaronmk
##### Existing maps discovery
107 1955 aaronmk
108
inputFiles := $(wildcard $(exts:%=src/*.%))
109
ifeq ($(inputFiles),)
110
dbExport := $(firstword $(wildcard src/db.*.sql))
111
endif
112
113 3574 aaronmk
srcMaps := $(wildcard maps/src.*.csv)
114
srcRoots := $(srcMaps:maps/src.%.csv=$(mappings)/roots/%.sh)
115
116 1289 aaronmk
srcMap := maps/src.%.csv
117 1139 aaronmk
fullViaMap := maps/%.full.csv
118 1289 aaronmk
directMap := maps/VegBIEN.%.csv
119
allViaMaps := $(filter-out $(srcMap) $(fullViaMap) $(directMap),\
120 1139 aaronmk
$(wildcard maps/*.csv))
121 728 aaronmk
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
122 783 aaronmk
123 3574 aaronmk
# 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 775 aaronmk
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
132 1524 aaronmk
coreSelfMap := $(mappings)/$(via).self.%.csv
133 783 aaronmk
134 749 aaronmk
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
135 3574 aaronmk
viaMaps += $(filter-out $(viaMaps),$(srcMaps:maps/src.%=maps/$(via).%))
136 1139 aaronmk
viaMaps += $(filter-out $(viaMaps) $(fullViaMap),$(wildcard maps/$(via).*.csv))
137 783 aaronmk
138 728 aaronmk
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
139
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
140
$(wildcard maps/VegBIEN.*.csv))
141 772 aaronmk
tables := $(directMaps:maps/VegBIEN.%.csv=%)
142 728 aaronmk
143 1969 aaronmk
##### 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 1986 aaronmk
# Usage: `make {--silent|-s} inputs/<datasrc>/cat` (don't echo make commands)
152 1969 aaronmk
cat: $(addprefix cat-,$(tables)) _always ;
153
154
cat-%: _always
155
	$(catSrcs)
156
157 1921 aaronmk
##### 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 1955 aaronmk
	$(if $(isCsv),$(import_install_))
181 3476 aaronmk
import_install_ = (prefix=; . $(bin)/vegbien_dest; "time" nice -n +5\
182
env schema=$(datasrc) table=$* $(bin)/csv2db $(catSrcs)\
183 3317 aaronmk
$(if $(log),$(if $(quiet),&>,2>&1|tee )import/install-$*.log.sql))
184 1921 aaronmk
185 1777 aaronmk
##### Maps building
186
187 1789 aaronmk
# Maps to (try to) build are added to one of these
188 1779 aaronmk
maps :=
189 1789 aaronmk
createOnlyMaps :=
190 1779 aaronmk
191 3569 aaronmk
srcRoot = $(mappings)/roots/$*.sh
192
mkSrcMap = $(catSrcs)|(. $(srcRoot); env datasrc=$(datasrc) $(bin)/src_map >$@)
193 1779 aaronmk
194 3567 aaronmk
# 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 1779 aaronmk
# Autogen src maps with known table names
201 3567 aaronmk
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 1779 aaronmk
# only build if CSV srcs exist for that table name
206 3567 aaronmk
endif
207
208 1779 aaronmk
# Try all table names
209 1789 aaronmk
createOnlyMaps += $(tablesSort:%=maps/src.%.csv)
210 1779 aaronmk
211 1273 aaronmk
# Must come before $(root)/% to override it
212 1524 aaronmk
$(coreSelfMap): _always
213 1273 aaronmk
	-+$(subMake)
214 1524 aaronmk
# ignore errors if $(coreSelfMap) does not exist
215 1273 aaronmk
216 3561 aaronmk
joinSrcMap = $(if $(hasSrc),$(bin)/in_place $@ $(bin)/intersect $(word 3,$+) 0)
217
hasSrc = $(shell test -s $(word 3,$+) && echo t)
218 1526 aaronmk
219 1273 aaronmk
makeFullCsv = $(if $(shell test -e $(word 2,$+) && echo t),\
220 3561 aaronmk
env ignore=1 $(bin)/union <$(wordlist 1,2,$+)|,<$< )$(bin)/sort_map >$@
221 1524 aaronmk
# can't use $(wildcard) because it won't recheck file after $(coreSelfMap) runs
222 1273 aaronmk
223 3572 aaronmk
# Must come before maps/$(via).%.csv to override it
224 3561 aaronmk
maps/$(via).%.full.csv: maps/$(via).%.csv $(coreSelfMap) $(srcMap)
225 1273 aaronmk
	$(makeFullCsv)
226 1526 aaronmk
	$(joinSrcMap)
227 1139 aaronmk
maps += $(patsubst maps/%.csv,maps/%.full.csv,$(viaMaps))
228
229 3572 aaronmk
# 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 3574 aaronmk
maps/$(via).%.csv: maps/src.%.csv _always
237
	$(if $(wildcard $@),,$(if $(wildcard $<),$(CP) $< $@))
238 3572 aaronmk
	+$(selfMake) $(@:maps/%=maps/.%.last_cleanup)
239
endif
240
241 1530 aaronmk
maps/VegBIEN.%.csv: maps/$(via).%.full.csv $(coreMap)
242
	$(bin)/join <$+|$(bin)/sort_map >$@
243
maps += $(autogenMaps)
244
245 1789 aaronmk
maps: $(createOnlyMaps) $(maps) _always ;
246 1139 aaronmk
247
all += $(maps)
248
249 1742 aaronmk
##### Maps validation
250
251 3764 aaronmk
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 1742 aaronmk
255 3764 aaronmk
include $(root)/lib/mappings.Makefile
256 1742 aaronmk
257 1509 aaronmk
##### External dependencies
258
259
$(root)/%: _always
260
	+$(subMake)
261 1628 aaronmk
.PRECIOUS: $(root)/% # let ext. dir's Makefile decide whether to delete on error
262 1509 aaronmk
263 783 aaronmk
##### Mapping
264 368 aaronmk
265 775 aaronmk
+maps = $(filter maps/% $(mappings)/%,$(+_))
266 1451 aaronmk
<in = $(firstword $(filter-out $(+maps),$(+_)))
267
map = $(if $(<in),<$(<in),\
268 1777 aaronmk
$(if $(srcs),$(withCatSrcs),\
269 1451 aaronmk
$(if $(mapEnv),env $(mapEnv),\
270 3387 aaronmk
$(error No input file src/*.$*.{$(exts)}))))\
271 1451 aaronmk
$(root)/map $(+maps)
272 3387 aaronmk
map2db = $(if $(use_staged),env in_database=vegbien in_schema=$(datasrc)\
273
in_table=$*) env out_database=vegbien $(map)
274 368 aaronmk
275 783 aaronmk
##### Import to VegBIEN
276 772 aaronmk
277 1378 aaronmk
ifneq ($(dbExport)$(inputFiles),)
278 772 aaronmk
279 1853 aaronmk
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 3317 aaronmk
log_ = import/$*$(if $(n),.n=$(n),).$(date).log.sql
284
trace = $(log_:.log.sql=.trace)
285 1853 aaronmk
import = -$(if $(profileTest),$(profileOnly),(set -x; "time" env commit=1\
286 1190 aaronmk
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
287 3616 aaronmk
$(if $(n),,&>$(log_)))$(if $(log),$(if $(n), 2>&1|tee -a $(log_))))
288 1088 aaronmk
# don't abort on import errors, which often relate to invalid input data
289 624 aaronmk
290 1193 aaronmk
import: $(addprefix import-,$(tables)) _always ;
291 718 aaronmk
292
import-%: maps/VegBIEN.%.csv _always
293
	$(import)
294 772 aaronmk
# default:
295
import-%: _always ;
296 718 aaronmk
297 772 aaronmk
else
298
import: _always ;
299
endif
300
301 790 aaronmk
##### Log files from import
302
303 3317 aaronmk
logs := $(wildcard import/*.log.sql import/*.trace)
304 339 aaronmk
305 383 aaronmk
rm_logs: _always
306 339 aaronmk
	$(RM) $(logs)
307
308 783 aaronmk
##### Verification of import
309 264 aaronmk
310 1194 aaronmk
verify: $(addprefix verify-,$(tables)) _always ;
311
312 1199 aaronmk
verify-%: verify/%.ref verify/%.out _always
313 1184 aaronmk
	-$(diffVerbose) $(+_)
314 1088 aaronmk
# don't abort on verification errors, which are expected during development
315 1194 aaronmk
# default:
316 1199 aaronmk
verify-%: verify/%.out _always
317 1200 aaronmk
	$(if $(shell test -e $< && echo t),cat $<)
318 1199 aaronmk
# don't run if verify/%.out's default do-nothing action was used
319 1200 aaronmk
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
320 368 aaronmk
321 1620 aaronmk
verify = $(if $(reverify),"time" $(psqlAsBien) --set=datasource="'$(datasrc)'"\
322
--no-align --field-separator=$$'\t' --pset=footer=off --pset=null=NULL <$< >$@)
323 514 aaronmk
324 1199 aaronmk
verify/%.out: $(mappings)/verify.%.sql _always
325 1192 aaronmk
	$(verify)
326 1194 aaronmk
# default:
327 1199 aaronmk
verify/%.out: _always ;
328 369 aaronmk
329 1199 aaronmk
all += $(wildcard verify/*.out)
330 1192 aaronmk
331 1378 aaronmk
ifneq ($(dbExport),)
332 1082 aaronmk
%.ref: %.ref.sql
333
	$(dbAsBien) $(db) <$< >$@
334
endif
335
336 1667 aaronmk
##### Editing import
337
338
import/rotate: _always
339 1668 aaronmk
	echo "UPDATE party SET organizationname = organizationname||'.$(date)'\
340 1854 aaronmk
WHERE organizationname = '$(datasrc)';"|$(psqlAsBien)
341 1667 aaronmk
342
import/rm: _always
343 1854 aaronmk
	echo "DELETE FROM party WHERE organizationname = '$(datasrc)';"|\
344
$(psqlAsBien)
345 1667 aaronmk
346 783 aaronmk
##### Testing
347 368 aaronmk
348 1986 aaronmk
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 630 aaronmk
353 897 aaronmk
define runTest
354 776 aaronmk
@echo "Testing $(abspath $@)..."
355 991 aaronmk
>$@ env test=1 n=$(test_n) $(1)
356 3721 aaronmk
@(set -x; $(diffIgnoreSpace) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
357 780 aaronmk
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
358 777 aaronmk
$(if $(hasOwnRef),\
359 3179 aaronmk
{\
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 777 aaronmk
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 652 aaronmk
exit $$e;}
371 628 aaronmk
endef
372
373 1986 aaronmk
test2Db = $(call runTest,$(map2db))
374 775 aaronmk
375 876 aaronmk
tests :=
376
377 3642 aaronmk
test/import.%.xml: maps/VegBIEN.%.csv _always
378 1986 aaronmk
	$(test2Db)
379 3642 aaronmk
tests += test/import.%.xml
380 627 aaronmk
381 1987 aaronmk
testStaged2Db = $(foreach use_staged,1,$(test2Db))
382
    # run with use_staged=1
383 1986 aaronmk
384
# Requires staging tables. To create them, run `make inputs/<datasrc>/install`.
385 3642 aaronmk
test/import.%.staging.xml: maps/VegBIEN.%.csv _always
386
	-$(if $(isCsv),$(testStaged2Db),cp -p $(@:.staging.xml=.xml) $@)
387 1988 aaronmk
# 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 3642 aaronmk
tests += test/import.%.staging.xml
390 1986 aaronmk
391 876 aaronmk
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
392 724 aaronmk
393 1361 aaronmk
.PRECIOUS: $(testOutputs) # save outputs of failed tests so they can be accepted
394
395 876 aaronmk
test: _always $(testOutputs) ;
396
397 1987 aaronmk
all += $(filter-out %.ref,$(wildcard test/*))
398 876 aaronmk
399 634 aaronmk
# Accepts a test output: make <test_output_path>-ok
400 626 aaronmk
%-ok: _always
401 630 aaronmk
	$(CP) $* $(call testRef,$*)
402 502 aaronmk
403 3579 aaronmk
test/accept-all: _always
404
	+yes|$(selfMake) test
405
406 3133 aaronmk
##### Documentation
407
408 3590 aaronmk
steps = $(selfMake) -s import test=1 by_col=1 verbosity=2 n=100\
409 3385 aaronmk
2>&1|$(bin)/debug2redmine >$@
410 3188 aaronmk
411 3133 aaronmk
import/steps.by_col.sql: _always
412 3188 aaronmk
	+$(steps)
413 3133 aaronmk
414 783 aaronmk
##### Input-type-specific
415 368 aaronmk
416 1917 aaronmk
# Each input type needs var $(mapEnv) and targets src/install, src/uninstall
417 624 aaronmk
418 1379 aaronmk
#### DB export
419
420 1378 aaronmk
ifneq ($(dbExport),)
421 256 aaronmk
422 1378 aaronmk
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbExport)))))
423 1196 aaronmk
db := $(datasrc)
424 622 aaronmk
425 1379 aaronmk
### Installation
426 256 aaronmk
427 1917 aaronmk
src/install: _always db ;
428 339 aaronmk
429 1917 aaronmk
src/uninstall: _always rm_db ;
430 368 aaronmk
431 1379 aaronmk
### DB-engine-specific
432 621 aaronmk
433 624 aaronmk
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
434 256 aaronmk
435 622 aaronmk
ifeq ($(dbEngineExt),my)
436 621 aaronmk
437 622 aaronmk
dbEngine := MySQL
438
439 621 aaronmk
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 1097 aaronmk
dbExists = $(shell echo "SHOW DATABASES LIKE '$(db)';"|$(mysqlAsRoot))
445
446
define createDb
447 1098 aaronmk
echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
448 1097 aaronmk
-$(mysqlAsRoot) --database=$(db) <$<
449
endef
450
# ignore errors in db import so that GRANT will still be run
451
452 1378 aaronmk
db: $(dbExport) _always
453 1097 aaronmk
	$(if $(dbExists),,$(createDb))
454 368 aaronmk
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
455 250 aaronmk
456 383 aaronmk
rm_db: _always
457 368 aaronmk
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
458
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
459 365 aaronmk
# ignore errors if grant not defined
460 250 aaronmk
461 1379 aaronmk
## Unrecognized DB engine
462 368 aaronmk
463
else
464 1378 aaronmk
$(error The DB filename $(dbExport) must be db.my.sql)
465 368 aaronmk
endif
466
467 1379 aaronmk
### Other input types
468 622 aaronmk
469 1379 aaronmk
else
470 624 aaronmk
471 1917 aaronmk
src/install: _always ;
472
src/uninstall: _always ;
473 1379 aaronmk
474 256 aaronmk
endif
475 1379 aaronmk
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