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
# SVN
50
addDir = $(if $(wildcard $(1)/),svn add --depth=empty $(1),svn mkdir $(1))
51
setSvnIgnore := svn propset svn:ignore
52
define addDirWithIgnore
53
$(addDir)
54
$(setSvnIgnore) $(2) $(1)
55
endef
56

    
57
##### General targets
58

    
59
all: _always maps ;
60

    
61
.SUFFIXES: # turn off built-in suffix rules
62
.SECONDARY: # don't automatically delete intermediate files
63
.DELETE_ON_ERROR: # delete target if recipe fails
64

    
65
_always:
66
.PHONY: _always
67

    
68
clean: _always
69
	$(RM) $(all)
70

    
71
%: %.make _always
72
	./$* >$@
73

    
74
%/: % _always ;
75

    
76
##### SVN
77

    
78
add: _always
79
	$(call addDirWithIgnore,.,$$'')
80
	$(call addDirWithIgnore,src,$$'*')
81
	$(call addDirWithIgnore,maps,$$'.~*')
82
	$(call addDirWithIgnore,import,$$'*')
83
	$(call addDirWithIgnore,test,$$'*.out\n*.xml')
84
	$(call addDirWithIgnore,verify,$$'*.out')
85

    
86
##### Installation
87

    
88
reinstall: _always uninstall install ;
89

    
90
##### Maps
91

    
92
srcMap := maps/src.%.csv
93
fullViaMap := maps/%.full.csv
94
directMap := maps/VegBIEN.%.csv
95
allViaMaps := $(filter-out $(srcMap) $(fullViaMap) $(directMap),\
96
$(wildcard maps/*.csv))
97
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
98

    
99
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
100
coreSelfMap := $(mappings)/$(via).self.%.csv
101
noEmptyMap := $(mappings)/$(via)-VegBIEN.%.no_empty.csv
102

    
103
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
104
viaMaps += $(filter-out $(viaMaps) $(fullViaMap),$(wildcard maps/$(via).*.csv))
105

    
106
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
107
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
108
$(wildcard maps/VegBIEN.*.csv))
109
tables := $(directMaps:maps/VegBIEN.%.csv=%)
110

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

    
113
# Must come before $(root)/% to override it
114
$(coreSelfMap): _always
115
	-+$(subMake)
116
# ignore errors if $(coreSelfMap) does not exist
117

    
118
# Via maps cleanup
119
ifneq ($(filter maps/.%.last_cleanup,$(MAKECMDGOALS)),)
120
maps/.$(via).%.csv.last_cleanup: maps/$(via).%.csv $(coreSelfMap)
121
	$(bin)/in_place $< $(bin)/subtract $(word 2,$+) 0 1
122
	touch $@
123
# default:
124
maps/.$(via).%.csv.last_cleanup: ;
125
else
126
$(viaMaps): _always
127
	$(selfMake) $(@:maps/%=maps/.%.last_cleanup)
128
endif
129

    
130
maps :=
131

    
132
joinSrcMap = $(if $(wildcard maps/src.$*.csv),$(bin)/in_place $@\
133
$(bin)/intersect maps/src.$*.csv 0)
134

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

    
139
maps/$(via).%.full.csv: maps/$(via).%.csv $(coreSelfMap)
140
	$(makeFullCsv)
141
	$(joinSrcMap)
142
maps += $(patsubst maps/%.csv,maps/%.full.csv,$(viaMaps))
143

    
144
maps/VegBIEN.%.csv: maps/$(via).%.full.csv $(coreMap)
145
	$(bin)/join <$+|$(bin)/sort_map >$@
146
maps += $(autogenMaps)
147

    
148
maps: $(maps) _always ;
149

    
150
all += $(maps)
151

    
152
##### External dependencies
153

    
154
$(root)/%: _always
155
	+$(subMake)
156

    
157
##### Mapping
158

    
159
dbExport := $(firstword $(wildcard src/db.*.sql))
160
inputFiles := $(wildcard $(exts:%=src/*.%))
161

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

    
172
##### Import to VegBIEN
173

    
174
ifneq ($(dbExport)$(inputFiles),)
175

    
176
log_ = import/$*$(if $(n),.n=$(n),).$(date).log
177
trace = $(log_:.log=.trace)
178
import = -(set -x; "time" env commit=1\
179
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
180
$(if $(n),,>>$(log_))) 2>&1$(if $(log),$(if $(n),|tee -a $(log_)))
181
# don't abort on import errors, which often relate to invalid input data
182

    
183
import: $(addprefix import-,$(tables)) _always ;
184

    
185
import-%: maps/VegBIEN.%.csv _always
186
	$(import)
187
# default:
188
import-%: _always ;
189

    
190
else
191
import: _always ;
192
endif
193

    
194
##### Log files from import
195

    
196
logs := $(wildcard import/*.log import/*.trace)
197

    
198
rm_logs: _always
199
	$(RM) $(logs)
200

    
201
##### Verification of import
202

    
203
verify: $(addprefix verify-,$(tables)) _always ;
204

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

    
214
verify = "time" $(psqlAsBien) --set=datasource="'$(datasrc)'" --no-align\
215
--field-separator=$$'\t' --pset=footer=off --pset=null=NULL <$< >$@
216

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

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

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

    
229
##### Testing
230

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

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

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

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

    
254
tests :=
255

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

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

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

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

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

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

    
277
test: _always $(testOutputs) ;
278

    
279
all += $(testOutputs)
280

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

    
285
else
286
test: _always ;
287
endif
288

    
289
##### Input-type-specific
290

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

    
293
#### DB export
294

    
295
ifneq ($(dbExport),)
296

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

    
300
### Installation
301

    
302
install: _always db ;
303

    
304
uninstall: _always rm_db ;
305

    
306
### DB-engine-specific
307

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

    
310
ifeq ($(dbEngineExt),my)
311

    
312
dbEngine := MySQL
313

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

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

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

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

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

    
336
## Unrecognized DB engine
337

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

    
342
### Other input types
343

    
344
else
345

    
346
install: _always ;
347
uninstall: _always ;
348

    
349
endif
350

    
351
#### DB connection info
352

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