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
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 src/),svn propset svn:ignore $$'*' src)
71
	$(if $(wildcard test/),svn propset svn:ignore $$'*.out\n*.xml' test)
72
	$(if $(wildcard verify/),svn propset svn:ignore $$'*.out' verify)
73

    
74
##### Installation
75

    
76
reinstall: _always uninstall install ;
77

    
78
##### Maps
79

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

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

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

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

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

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

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

    
110
maps :=
111

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

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

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

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

    
128
maps: $(maps) _always ;
129

    
130
all += $(maps)
131

    
132
##### Mapping
133

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

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

    
144
##### Import to VegBIEN
145

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

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

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

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

    
162
else
163
import: _always ;
164
endif
165

    
166
##### Log files from import
167

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

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

    
173
##### Verification of import
174

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

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

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

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

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

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

    
204
##### Testing
205

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

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

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

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

    
229
tests :=
230

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

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

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

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

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

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

    
252
test: _always $(testOutputs) ;
253

    
254
all += $(testOutputs)
255

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

    
260
else
261
test: _always ;
262
endif
263

    
264
##### Input-type-specific
265

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

    
268
#### DB export
269

    
270
ifneq ($(dbExport),)
271

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

    
275
### Installation
276

    
277
install: _always db ;
278

    
279
uninstall: _always rm_db ;
280

    
281
### DB-engine-specific
282

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

    
285
ifeq ($(dbEngineExt),my)
286

    
287
dbEngine := MySQL
288

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

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

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

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

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

    
311
## Unrecognized DB engine
312

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

    
317
### Other input types
318

    
319
else
320

    
321
install: _always ;
322
uninstall: _always ;
323

    
324
endif
325

    
326
#### DB connection info
327

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