Project

General

Profile

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