Project

General

Profile

1
##### Configuration
2

    
3
log ?= $(if $(test),,1)
4
profile ?=
5
exts ?= csv tsv txt xml
6
test_n ?= 2
7
tablesSort ?= plots organisms stems
8

    
9
##### Vars/functions
10

    
11
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
12

    
13
# Make
14
SHELL := /bin/bash
15
selfMake = $(MAKE) --makefile=../input.Makefile
16
subMake = $(MAKE) $(@F) --directory=$(@D)
17
+_ = $(+:_%=)
18
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
19

    
20
# System
21
date = $(shell date +"%Y-%m-%d-%H-%M-%S")
22

    
23
# Terminal
24
termCols := $(shell tput cols)
25
esc := '['
26
reset := $(esc)'0m'
27
emph := $(esc)'7m '
28
endEmph := ' '$(reset)
29

    
30
# Commands
31
MKDIR = mkdir -p
32
mkdir = $(MKDIR) $(@D)
33
CP = cp -p
34
diff = diff --unified=2
35
diffVerbose = $(if $(verbose),diff --side-by-side --left-column\
36
--width=$(termCols),$(diff))
37

    
38
# Paths
39
datasrc := $(notdir $(realpath .))
40
root := $(selfDir_uZPPqC)..
41
bin := $(root)/bin
42
mappings := $(root)/mappings
43

    
44
# Commands
45
join = $(bin)/join_union_sort
46
selfMap = $(bin)/cols 0 0
47
psqlOpts := --set ON_ERROR_STOP=1 --quiet
48
psqlAsBien := $(bin)/psql_vegbien $(psqlOpts)
49

    
50
##### General targets
51

    
52
all: _always maps ;
53

    
54
.SUFFIXES: # turn off built-in suffix rules
55
.SECONDARY: # don't automatically delete intermediate files
56
.DELETE_ON_ERROR: # delete target if recipe fails
57

    
58
_always:
59
.PHONY: _always
60

    
61
clean: _always
62
	$(RM) $(all)
63

    
64
%.out: %.make _always
65
	./$* >$@
66

    
67
##### SVN
68

    
69
svn_props: _always
70
	svn propset svn:ignore $$'*.log\n*.trace\nsrc*' .
71
	$(if $(wildcard maps/),svn propset svn:ignore $$'.~*' maps)
72
	$(if $(wildcard src/),svn propset svn:ignore $$'*' src)
73
	$(if $(wildcard test/),svn propset svn:ignore $$'*.out\n*.xml' test)
74
	$(if $(wildcard verify/),svn propset svn:ignore $$'*.out' verify)
75

    
76
##### Installation
77

    
78
reinstall: _always uninstall install ;
79

    
80
##### Maps
81

    
82
srcMap := maps/src.%.csv
83
fullViaMap := maps/%.full.csv
84
directMap := maps/VegBIEN.%.csv
85
allViaMaps := $(filter-out $(srcMap) $(fullViaMap) $(directMap),\
86
$(wildcard maps/*.csv))
87
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
88

    
89
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
90
coreSelfMap := $(mappings)/$(via).self.%.csv
91
noEmptyMap := $(mappings)/$(via)-VegBIEN.%.no_empty.csv
92

    
93
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
94
viaMaps += $(filter-out $(viaMaps) $(fullViaMap),$(wildcard maps/$(via).*.csv))
95

    
96
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
97
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
98
$(wildcard maps/VegBIEN.*.csv))
99
tables := $(directMaps:maps/VegBIEN.%.csv=%)
100

    
101
srcMaps := $(filter-out maps/src.join.%.csv,$(wildcard maps/src.*.csv))
102
srcJoinMaps := $(srcMaps:maps/src.%.csv=maps/src.join.%.csv)
103

    
104
# Must come before $(root)/% to override it
105
$(coreSelfMap): _always
106
	-+$(subMake)
107
# ignore errors if $(coreSelfMap) does not exist
108

    
109
# Via maps cleanup
110
ifneq ($(filter maps/.%.last_cleanup,$(MAKECMDGOALS)),)
111
maps/.$(via).%.csv.last_cleanup: maps/$(via).%.csv $(coreSelfMap)
112
	$(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

    
121
maps :=
122

    
123
joinSrcMap = $(if $(wildcard maps/src.$*.csv),$(bin)/in_place $@\
124
$(bin)/intersect maps/src.$*.csv 0)
125

    
126
makeFullCsv = $(if $(shell test -e $(word 2,$+) && echo t),\
127
env ignore=1 $(bin)/union <$+|$(bin)/sort_map >$@,$(CP) $< $@)
128
# can't use $(wildcard) because it won't recheck file after $(coreSelfMap) runs
129

    
130
maps/$(via).%.full.csv: maps/$(via).%.csv $(coreSelfMap)
131
	$(makeFullCsv)
132
	$(joinSrcMap)
133
maps += $(patsubst maps/%.csv,maps/%.full.csv,$(viaMaps))
134

    
135
maps/VegBIEN.%.csv: maps/$(via).%.full.csv $(coreMap)
136
	$(bin)/join <$+|$(bin)/sort_map >$@
137
maps += $(autogenMaps)
138

    
139
maps/src.join.%.csv: maps/src.%.csv maps/$(via).%.full.csv $(noEmptyMap)
140
	$(selfMap) <$<|$(bin)/join $(word 2,$+)|$(join) $(word 3,$+) >$@
141
maps += $(srcJoinMaps)
142

    
143
maps: $(maps) _always ;
144

    
145
all += $(maps)
146

    
147
##### External dependencies
148

    
149
$(root)/%: _always
150
	+$(subMake)
151

    
152
##### Mapping
153

    
154
dbExport := $(firstword $(wildcard src/db.*.sql))
155
inputFiles := $(wildcard $(exts:%=src/*.%))
156

    
157
+maps = $(filter maps/% $(mappings)/%,$(+_))
158
<in = $(firstword $(filter-out $(+maps),$(+_)))
159
srcs = $(sort $(wildcard $(exts:%=src/*.$*.%)))
160
map = $(if $(<in),<$(<in),\
161
$(if $(srcs),$(bin)/with_cat_csv $(srcs) --,\
162
$(if $(mapEnv),env $(mapEnv),\
163
$(error No input file src/*.$*.{$(exts)}))))\
164
$(root)/map $(+maps)
165
map2db = env out_database=vegbien $(map)
166

    
167
##### Import to VegBIEN
168

    
169
ifneq ($(dbExport)$(inputFiles),)
170

    
171
log_ = $(@:-all=)$(if $(n),.n=$(n),).$(date).log
172
trace = $(log_:.log=.trace)
173
import = -(set -x; "time" env commit=1 verbose=1\
174
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
175
$(if $(n),,>>$(log_))) 2>&1$(if $(log),$(if $(n),|tee -a $(log_)))
176
# don't abort on import errors, which often relate to invalid input data
177

    
178
import: $(addprefix import-,$(tables)) _always ;
179

    
180
import-%: maps/VegBIEN.%.csv _always
181
	$(import)
182
# default:
183
import-%: _always ;
184

    
185
else
186
import: _always ;
187
endif
188

    
189
##### Log files from import
190

    
191
logs := $(wildcard *.log *.trace)
192

    
193
rm_logs: _always
194
	$(RM) $(logs)
195

    
196
##### Verification of import
197

    
198
verify: $(addprefix verify-,$(tables)) _always ;
199

    
200
verify-%: verify/%.ref verify/%.out _always
201
	-$(diffVerbose) $(+_)
202
# don't abort on verification errors, which are expected during development
203
# default:
204
verify-%: verify/%.out _always
205
	$(if $(shell test -e $< && echo t),cat $<)
206
# don't run if verify/%.out's default do-nothing action was used
207
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
208

    
209
define verify
210
$(mkdir)
211
"time" $(psqlAsBien) --set=datasource="'$(datasrc)'" --no-align\
212
--field-separator=$$'\t' --pset=footer=off --pset=null=NULL <$< >$@
213
endef
214

    
215
verify/%.out: $(mappings)/verify.%.sql _always
216
	$(verify)
217
# default:
218
verify/%.out: _always ;
219

    
220
all += $(wildcard verify/*.out)
221

    
222
ifneq ($(dbExport),)
223
%.ref: %.ref.sql
224
	$(dbAsBien) $(db) <$< >$@
225
endif
226

    
227
##### Testing
228

    
229
ifneq ($(wildcard test/),)
230

    
231
hasOwnRef = $(filter-out %.2-step.xml,$@)
232
testRef = $(1:.2-step.xml=.xml).ref
233

    
234
define runTest
235
@echo "Testing $(abspath $@)..."
236
>$@ env test=1 n=$(test_n) $(1)
237
@(set -x; $(diff) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
238
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
239
$(if $(hasOwnRef),\
240
echo $(emph)"To accept new test output:"$(endEmph);\
241
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
242
,\
243
echo $(emph)"Note: The preceding failed test is compared to another test's\
244
output"$(endEmph);\
245
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
246
)\
247
exit $$e;}
248
endef
249

    
250
test2File = $(call runTest,$(map))
251

    
252
tests :=
253

    
254
test/$(via).%.xml: maps/$(via).%.full.csv _always
255
	$(test2File)
256
tests += test/$(via).%.xml
257

    
258
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
259
	$(test2File)
260
tests += test/VegBIEN.%.xml
261

    
262
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _always
263
	-$(test2File)
264
# Don't abort tester if only 2-step test fails, as it's often finicky
265
tests += test/VegBIEN.%.2-step.xml
266

    
267
test/import.%.out: maps/VegBIEN.%.csv _always
268
	$(call runTest,$(map2db))
269
tests += test/import.%.out
270

    
271
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
272

    
273
.PRECIOUS: $(testOutputs) # save outputs of failed tests so they can be accepted
274

    
275
test: _always $(testOutputs) ;
276

    
277
all += $(testOutputs)
278

    
279
# Accepts a test output: make <test_output_path>-ok
280
%-ok: _always
281
	$(CP) $* $(call testRef,$*)
282

    
283
else
284
test: _always ;
285
endif
286

    
287
##### Input-type-specific
288

    
289
# Each input type needs var $(mapEnv) and targets install, uninstall
290

    
291
#### DB export
292

    
293
ifneq ($(dbExport),)
294

    
295
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbExport)))))
296
db := $(datasrc)
297

    
298
### Installation
299

    
300
install: _always db ;
301

    
302
uninstall: _always rm_db ;
303

    
304
### DB-engine-specific
305

    
306
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
307

    
308
ifeq ($(dbEngineExt),my)
309

    
310
dbEngine := MySQL
311

    
312
bienPassword := $(shell cat $(root)/config/bien_password)
313
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
314
mysqlAsRoot := $(call mysqlAs,root)
315
dbAsBien := $(call mysqlAs,bien)
316

    
317
dbExists = $(shell echo "SHOW DATABASES LIKE '$(db)';"|$(mysqlAsRoot))
318

    
319
define createDb
320
echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
321
-$(mysqlAsRoot) --database=$(db) <$<
322
endef
323
# ignore errors in db import so that GRANT will still be run
324

    
325
db: $(dbExport) _always
326
	$(if $(dbExists),,$(createDb))
327
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
328

    
329
rm_db: _always
330
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
331
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
332
# ignore errors if grant not defined
333

    
334
## Unrecognized DB engine
335

    
336
else
337
$(error The DB filename $(dbExport) must be db.my.sql)
338
endif
339

    
340
### Other input types
341

    
342
else
343

    
344
install: _always ;
345
uninstall: _always ;
346

    
347
endif
348

    
349
#### DB connection info
350

    
351
ifneq ($(dbEngine),)
352
# Must come after dbEngine is set
353
mapEnv := in_engine=$(dbEngine) in_database=$(db)
354
endif
(2-2/2)