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