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