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