Project

General

Profile

1
##### Configuration
2

    
3
log ?= $(if $(test),,1)
4
profile ?=
5
exts ?= csv 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
subMake = $(MAKE) $(@F) --directory=$(@D)
16
+_ = $(+:_%=)
17
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
18

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

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

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

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

    
43
# Commands
44
join = $(bin)/join_union_sort
45
psqlOpts := --set ON_ERROR_STOP=1 --quiet
46
psqlAsBien := $(bin)/psql_vegbien $(psqlOpts)
47

    
48
##### General targets
49

    
50
all: _always maps ;
51

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

    
56
_always:
57
.PHONY: _always
58

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

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

    
65
##### SVN
66

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

    
73
##### Installation
74

    
75
reinstall: _always uninstall install ;
76

    
77
##### Maps
78

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

    
86
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
87
selfMap := $(mappings)/$(via).self.%.csv
88
noEmptyMap := $(mappings)/$(via)-VegBIEN.%.no_empty.csv
89

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

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

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

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

    
106
$(root)/%: _always
107
	+$(subMake)
108

    
109
maps :=
110

    
111
maps/VegBIEN.%.csv: maps/$(via).%.csv $(coreMap)
112
	$(join) <$+ >$@
113
maps += $(autogenMaps)
114

    
115
makeFullCsv = $(if $(shell test -e $(word 2,$+) && echo t),\
116
env ignore=1 $(bin)/union <$+|$(bin)/sort_map >$@,$(CP) $< $@)
117
# can't use $(wildcard) because it won't recheck file after $(selfMap) is run
118

    
119
maps/$(via).%.full.csv: maps/$(via).%.csv $(selfMap)
120
	$(makeFullCsv)
121
maps += $(patsubst maps/%.csv,maps/%.full.csv,$(viaMaps))
122

    
123
maps/src.join.%.csv: maps/src.%.csv maps/$(via).%.full.csv $(noEmptyMap)
124
	$(bin)/cols 0 0 <$<|$(bin)/join $(word 2,$+)|$(join) $(word 3,$+) >$@
125
maps += $(srcJoinMaps)
126

    
127
maps: $(maps) _always ;
128

    
129
all += $(maps)
130

    
131
##### Mapping
132

    
133
dbExport := $(firstword $(wildcard src/db.*.sql))
134
inputFiles := $(wildcard $(exts:%=src/*.%))
135

    
136
+maps = $(filter maps/% $(mappings)/%,$(+_))
137
<in = $(firstword $(filter-out $(+maps),$(+_)) $(wildcard $(exts:%=src/*.$*.%)))
138
inputs = $(wildcard $(<in).header $(<in))
139
map = $(if $(inputs),$(bin)/with_cat $(inputs) -- ,$(if $(mapEnv),env $(mapEnv)\
140
,$(error No input file src/*.$*.{$(exts)} )))$(root)/map $(+maps)
141
map2db = env out_database=vegbien $(map)
142

    
143
##### Import to VegBIEN
144

    
145
ifneq ($(dbExport)$(inputFiles),)
146

    
147
log_ = $(@:-all=)$(if $(n),.n=$(n),).$(date).log
148
trace = $(log_:.log=.trace)
149
import = -(set -x; "time" env commit=1 verbose=1\
150
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
151
$(if $(n),,>>$(log_))) 2>&1$(if $(log),$(if $(n),|tee -a $(log_)))
152
# don't abort on import errors, which often relate to invalid input data
153

    
154
import: $(addprefix import-,$(tables)) _always ;
155

    
156
import-%: maps/VegBIEN.%.csv _always
157
	$(import)
158
# default:
159
import-%: _always ;
160

    
161
else
162
import: _always ;
163
endif
164

    
165
##### Log files from import
166

    
167
logs := $(wildcard *.log *.trace)
168

    
169
rm_logs: _always
170
	$(RM) $(logs)
171

    
172
##### Verification of import
173

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

    
176
verify-%: verify/%.ref verify/%.out _always
177
	-$(diffVerbose) $(+_)
178
# don't abort on verification errors, which are expected during development
179
# default:
180
verify-%: verify/%.out _always
181
	$(if $(shell test -e $< && echo t),cat $<)
182
# don't run if verify/%.out's default do-nothing action was used
183
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
184

    
185
define verify
186
$(mkdir)
187
"time" $(psqlAsBien) --set=datasource="'$(datasrc)'" --no-align\
188
--field-separator=$$'\t' --pset=footer=off --pset=null=NULL <$< >$@
189
endef
190

    
191
verify/%.out: $(mappings)/verify.%.sql _always
192
	$(verify)
193
# default:
194
verify/%.out: _always ;
195

    
196
all += $(wildcard verify/*.out)
197

    
198
ifneq ($(dbExport),)
199
%.ref: %.ref.sql
200
	$(dbAsBien) $(db) <$< >$@
201
endif
202

    
203
##### Testing
204

    
205
ifneq ($(wildcard test/),)
206

    
207
hasOwnRef = $(filter-out %.2-step.xml,$@)
208
testRef = $(1:.2-step.xml=.xml).ref
209

    
210
define runTest
211
@echo "Testing $(abspath $@)..."
212
>$@ env test=1 n=$(test_n) $(1)
213
@(set -x; $(diff) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
214
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
215
$(if $(hasOwnRef),\
216
echo $(emph)"To accept new test output:"$(endEmph);\
217
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
218
,\
219
echo $(emph)"Note: The preceding failed test is compared to another test's\
220
output"$(endEmph);\
221
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
222
)\
223
exit $$e;}
224
endef
225

    
226
test2File = $(call runTest,$(map))
227

    
228
tests :=
229

    
230
test/$(via).%.xml: maps/$(via).%.full.csv _always
231
	$(test2File)
232
tests += test/$(via).%.xml
233

    
234
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
235
	$(test2File)
236
tests += test/VegBIEN.%.xml
237

    
238
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _always
239
	-$(test2File)
240
# Don't abort tester if only 2-step test fails, as it's often finicky
241
tests += test/VegBIEN.%.2-step.xml
242

    
243
test/import.%.out: maps/VegBIEN.%.csv _always
244
	$(call runTest,$(map2db))
245
tests += test/import.%.out
246

    
247
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
248

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

    
251
test: _always $(testOutputs) ;
252

    
253
all += $(testOutputs)
254

    
255
# Accepts a test output: make <test_output_path>-ok
256
%-ok: _always
257
	$(CP) $* $(call testRef,$*)
258

    
259
else
260
test: _always ;
261
endif
262

    
263
##### Input-type-specific
264

    
265
# Each input type needs var $(mapEnv) and targets install, uninstall
266

    
267
#### DB export
268

    
269
ifneq ($(dbExport),)
270

    
271
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbExport)))))
272
db := $(datasrc)
273

    
274
### Installation
275

    
276
install: _always db ;
277

    
278
uninstall: _always rm_db ;
279

    
280
### DB-engine-specific
281

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

    
284
ifeq ($(dbEngineExt),my)
285

    
286
dbEngine := MySQL
287

    
288
bienPassword := $(shell cat $(root)/config/bien_password)
289
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
290
mysqlAsRoot := $(call mysqlAs,root)
291
dbAsBien := $(call mysqlAs,bien)
292

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

    
295
define createDb
296
echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
297
-$(mysqlAsRoot) --database=$(db) <$<
298
endef
299
# ignore errors in db import so that GRANT will still be run
300

    
301
db: $(dbExport) _always
302
	$(if $(dbExists),,$(createDb))
303
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
304

    
305
rm_db: _always
306
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
307
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
308
# ignore errors if grant not defined
309

    
310
## Unrecognized DB engine
311

    
312
else
313
$(error The DB filename $(dbExport) must be db.my.sql)
314
endif
315

    
316
### Other input types
317

    
318
else
319

    
320
install: _always ;
321
uninstall: _always ;
322

    
323
endif
324

    
325
#### DB connection info
326

    
327
ifneq ($(dbEngine),)
328
# Must come after dbEngine is set
329
mapEnv := in_engine=$(dbEngine) in_database=$(db)
330
endif
(2-2/2)