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