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
selfMap = $(bin)/cols 0 0
46
psqlOpts := --set ON_ERROR_STOP=1 --quiet
47
psqlAsBien := $(bin)/psql_vegbien $(psqlOpts)
48

    
49
##### General targets
50

    
51
all: _always maps ;
52

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

    
57
_always:
58
.PHONY: _always
59

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

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

    
66
##### SVN
67

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

    
75
##### Installation
76

    
77
reinstall: _always uninstall install ;
78

    
79
##### Maps
80

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

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

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

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

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

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

    
107
# Via maps cleanup
108
ifneq ($(filter maps/.%.last_cleanup,$(MAKECMDGOALS)),)
109
maps/.$(via).%.csv.last_cleanup: maps/$(via).%.csv $(coreSelfMap)
110
	$(bin)/in_place $< $(bin)/subtract $(word 2,$+) 0 1
111
	touch $@
112
# default:
113
maps/.$(via).%.csv.last_cleanup: ;
114
else
115
$(viaMaps): _always
116
	$(selfMake) $(@:maps/%=maps/.%.last_cleanup)
117
endif
118

    
119
maps :=
120

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

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

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

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

    
137
maps: $(maps) _always ;
138

    
139
all += $(maps)
140

    
141
##### External dependencies
142

    
143
$(root)/%: _always
144
	+$(subMake)
145

    
146
##### Mapping
147

    
148
dbExport := $(firstword $(wildcard src/db.*.sql))
149
inputFiles := $(wildcard $(exts:%=src/*.%))
150

    
151
+maps = $(filter maps/% $(mappings)/%,$(+_))
152
<in = $(firstword $(filter-out $(+maps),$(+_)))
153
srcs = $(sort $(wildcard $(exts:%=src/*.$*.%)))
154
map = $(if $(<in),<$(<in),\
155
$(if $(srcs),$(bin)/with_cat_csv $(srcs) --,\
156
$(if $(mapEnv),env $(mapEnv),\
157
$(error No input file src/*.$*.{$(exts)}))))\
158
$(root)/map $(+maps)
159
map2db = env out_database=vegbien $(map)
160

    
161
##### Import to VegBIEN
162

    
163
ifneq ($(dbExport)$(inputFiles),)
164

    
165
log_ = $(@:-all=)$(if $(n),.n=$(n),).$(date).log
166
trace = $(log_:.log=.trace)
167
import = -(set -x; "time" env commit=1\
168
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
169
$(if $(n),,>>$(log_))) 2>&1$(if $(log),$(if $(n),|tee -a $(log_)))
170
# don't abort on import errors, which often relate to invalid input data
171

    
172
import: $(addprefix import-,$(tables)) _always ;
173

    
174
import-%: maps/VegBIEN.%.csv _always
175
	$(import)
176
# default:
177
import-%: _always ;
178

    
179
else
180
import: _always ;
181
endif
182

    
183
##### Log files from import
184

    
185
logs := $(wildcard *.log *.trace)
186

    
187
rm_logs: _always
188
	$(RM) $(logs)
189

    
190
##### Verification of import
191

    
192
verify: $(addprefix verify-,$(tables)) _always ;
193

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

    
203
define verify
204
$(mkdir)
205
"time" $(psqlAsBien) --set=datasource="'$(datasrc)'" --no-align\
206
--field-separator=$$'\t' --pset=footer=off --pset=null=NULL <$< >$@
207
endef
208

    
209
verify/%.out: $(mappings)/verify.%.sql _always
210
	$(verify)
211
# default:
212
verify/%.out: _always ;
213

    
214
all += $(wildcard verify/*.out)
215

    
216
ifneq ($(dbExport),)
217
%.ref: %.ref.sql
218
	$(dbAsBien) $(db) <$< >$@
219
endif
220

    
221
##### Testing
222

    
223
ifneq ($(wildcard test/),)
224

    
225
hasOwnRef = $(filter-out %.2-step.xml,$@)
226
testRef = $(1:.2-step.xml=.xml).ref
227

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

    
244
test2File = $(call runTest,$(map))
245

    
246
tests :=
247

    
248
test/$(via).%.xml: maps/$(via).%.full.csv _always
249
	$(test2File)
250
tests += test/$(via).%.xml
251

    
252
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
253
	$(test2File)
254
tests += test/VegBIEN.%.xml
255

    
256
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _always
257
	-$(test2File)
258
# Don't abort tester if only 2-step test fails, as it's often finicky
259
tests += test/VegBIEN.%.2-step.xml
260

    
261
test/import.%.out: maps/VegBIEN.%.csv _always
262
	$(call runTest,$(map2db))
263
tests += test/import.%.out
264

    
265
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
266

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

    
269
test: _always $(testOutputs) ;
270

    
271
all += $(testOutputs)
272

    
273
# Accepts a test output: make <test_output_path>-ok
274
%-ok: _always
275
	$(CP) $* $(call testRef,$*)
276

    
277
else
278
test: _always ;
279
endif
280

    
281
##### Input-type-specific
282

    
283
# Each input type needs var $(mapEnv) and targets install, uninstall
284

    
285
#### DB export
286

    
287
ifneq ($(dbExport),)
288

    
289
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbExport)))))
290
db := $(datasrc)
291

    
292
### Installation
293

    
294
install: _always db ;
295

    
296
uninstall: _always rm_db ;
297

    
298
### DB-engine-specific
299

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

    
302
ifeq ($(dbEngineExt),my)
303

    
304
dbEngine := MySQL
305

    
306
bienPassword := $(shell cat $(root)/config/bien_password)
307
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
308
mysqlAsRoot := $(call mysqlAs,root)
309
dbAsBien := $(call mysqlAs,bien)
310

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

    
313
define createDb
314
echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
315
-$(mysqlAsRoot) --database=$(db) <$<
316
endef
317
# ignore errors in db import so that GRANT will still be run
318

    
319
db: $(dbExport) _always
320
	$(if $(dbExists),,$(createDb))
321
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
322

    
323
rm_db: _always
324
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
325
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
326
# ignore errors if grant not defined
327

    
328
## Unrecognized DB engine
329

    
330
else
331
$(error The DB filename $(dbExport) must be db.my.sql)
332
endif
333

    
334
### Other input types
335

    
336
else
337

    
338
install: _always ;
339
uninstall: _always ;
340

    
341
endif
342

    
343
#### DB connection info
344

    
345
ifneq ($(dbEngine),)
346
# Must come after dbEngine is set
347
mapEnv := in_engine=$(dbEngine) in_database=$(db)
348
endif
(2-2/2)