Project

General

Profile

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