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