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