Project

General

Profile

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