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