Project

General

Profile

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