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