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
maps/VegBIEN.%.csv: maps/$(via).%.csv $(coreMap)
127
	$(join) <$+ >$@
128
	$(joinSrcMap)
129
maps += $(autogenMaps)
130

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

    
135
maps/$(via).%.full.csv: maps/$(via).%.csv $(coreSelfMap)
136
	$(makeFullCsv)
137
	$(joinSrcMap)
138
maps += $(patsubst maps/%.csv,maps/%.full.csv,$(viaMaps))
139

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

    
144
maps: $(maps) _always ;
145

    
146
all += $(maps)
147

    
148
##### External dependencies
149

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

    
153
##### Mapping
154

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

    
158
+maps = $(filter maps/% $(mappings)/%,$(+_))
159
<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
map2db = env out_database=vegbien $(map)
167

    
168
##### Import to VegBIEN
169

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

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

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

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

    
186
else
187
import: _always ;
188
endif
189

    
190
##### Log files from import
191

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

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

    
197
##### Verification of import
198

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

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

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

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

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

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

    
228
##### Testing
229

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

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

    
235
define runTest
236
@echo "Testing $(abspath $@)..."
237
>$@ env test=1 n=$(test_n) $(1)
238
@(set -x; $(diff) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
239
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
240
$(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
exit $$e;}
249
endef
250

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

    
253
tests :=
254

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

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

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

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

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

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

    
276
test: _always $(testOutputs) ;
277

    
278
all += $(testOutputs)
279

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

    
284
else
285
test: _always ;
286
endif
287

    
288
##### Input-type-specific
289

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

    
292
#### DB export
293

    
294
ifneq ($(dbExport),)
295

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

    
299
### Installation
300

    
301
install: _always db ;
302

    
303
uninstall: _always rm_db ;
304

    
305
### DB-engine-specific
306

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

    
309
ifeq ($(dbEngineExt),my)
310

    
311
dbEngine := MySQL
312

    
313
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
dbExists = $(shell echo "SHOW DATABASES LIKE '$(db)';"|$(mysqlAsRoot))
319

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

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

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

    
335
## Unrecognized DB engine
336

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

    
341
### Other input types
342

    
343
else
344

    
345
install: _always ;
346
uninstall: _always ;
347

    
348
endif
349

    
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
(2-2/2)