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