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
maps/VegBIEN.%.csv: maps/$(via).%.csv $(coreMap)
124
	$(join) <$+ >$@
125
maps += $(autogenMaps)
126

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

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

    
135
maps/src.join.%.csv: maps/src.%.csv maps/$(via).%.full.csv $(noEmptyMap)
136
	$(selfMap) <$<|$(bin)/join $(word 2,$+)|$(join) $(word 3,$+) >$@
137
maps += $(srcJoinMaps)
138

    
139
maps: $(maps) _always ;
140

    
141
all += $(maps)
142

    
143
##### External dependencies
144

    
145
$(root)/%: _always
146
	+$(subMake)
147

    
148
##### Mapping
149

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

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

    
163
##### Import to VegBIEN
164

    
165
ifneq ($(dbExport)$(inputFiles),)
166

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

    
174
import: $(addprefix import-,$(tables)) _always ;
175

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

    
181
else
182
import: _always ;
183
endif
184

    
185
##### Log files from import
186

    
187
logs := $(wildcard *.log *.trace)
188

    
189
rm_logs: _always
190
	$(RM) $(logs)
191

    
192
##### Verification of import
193

    
194
verify: $(addprefix verify-,$(tables)) _always ;
195

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

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

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

    
216
all += $(wildcard verify/*.out)
217

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

    
223
##### Testing
224

    
225
ifneq ($(wildcard test/),)
226

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

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

    
246
test2File = $(call runTest,$(map))
247

    
248
tests :=
249

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

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

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

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

    
267
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
268

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

    
271
test: _always $(testOutputs) ;
272

    
273
all += $(testOutputs)
274

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

    
279
else
280
test: _always ;
281
endif
282

    
283
##### Input-type-specific
284

    
285
# Each input type needs var $(mapEnv) and targets install, uninstall
286

    
287
#### DB export
288

    
289
ifneq ($(dbExport),)
290

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

    
294
### Installation
295

    
296
install: _always db ;
297

    
298
uninstall: _always rm_db ;
299

    
300
### DB-engine-specific
301

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

    
304
ifeq ($(dbEngineExt),my)
305

    
306
dbEngine := MySQL
307

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

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

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

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

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

    
330
## Unrecognized DB engine
331

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

    
336
### Other input types
337

    
338
else
339

    
340
install: _always ;
341
uninstall: _always ;
342

    
343
endif
344

    
345
#### DB connection info
346

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