Project

General

Profile

1
##### Configuration
2

    
3
log ?= $(if $(test),,1)
4
profile ?= $(log)
5
exts ?= csv 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
CP = cp -p
31
diff = diff --unified=2
32
diffVerbose = $(if $(verbose),diff --side-by-side --left-column\
33
--width=$(termCols),$(diff))
34

    
35
# Paths
36
root := $(selfDir_uZPPqC)..
37
mappings := $(root)/mappings
38
psqlOpts := --set ON_ERROR_STOP=1 --quiet
39
psqlAsBien := $(root)/bin/psql_vegbien $(psqlOpts)
40

    
41
##### General targets
42

    
43
all: _always maps ;
44

    
45
.SUFFIXES:
46

    
47
_always:
48
.PHONY: _always
49

    
50
clean: _always
51
	$(RM) $(all)
52

    
53
remake: _always clean all ;
54

    
55
%.out: %.make _always
56
	./$* >$@
57
.PRECIOUS: %.out
58

    
59
$(root)/%: _always
60
	+$(subMake)
61

    
62
##### SVN
63

    
64
svn_props: _always
65
	svn propset svn:ignore $$'.~*\n*.log\n*.trace\n*.out\nsrc*' .
66
	$(if $(wildcard maps/),svn propset svn:ignore $$'.~*' maps)
67
	$(if $(wildcard test/),svn propset svn:ignore $$'*.out\n*.xml' test)
68

    
69
##### Installation
70

    
71
reinstall: _always uninstall install ;
72

    
73
##### Maps
74

    
75
fullViaMap := maps/%.full.csv
76
allViaMaps := $(filter-out maps/VegBIEN.%.csv $(fullViaMap),\
77
$(wildcard maps/*.csv))
78
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
79

    
80
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
81
selfMap := $(mappings)/$(via).self.%.csv
82

    
83
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
84
viaMaps += $(filter-out $(viaMaps) $(fullViaMap),$(wildcard maps/$(via).*.csv))
85

    
86
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
87
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
88
$(wildcard maps/VegBIEN.*.csv))
89
tables := $(directMaps:maps/VegBIEN.%.csv=%)
90

    
91
.PRECIOUS: $(allViaMaps) $(directMaps)
92

    
93
maps :=
94

    
95
maps/VegBIEN.%.csv: maps/$(via).%.csv $(coreMap)
96
	$(root)/bin/join_union_sort <$+ >$@
97
.PRECIOUS: maps/VegBIEN.%.csv $(coreMap)
98

    
99
maps += $(autogenMaps)
100

    
101
maps/$(via).%.full.csv: maps/$(via).%.csv $(selfMap)
102
	env ignore=1 $(root)/bin/union <$+|$(root)/bin/sort_map >$@
103
maps/$(via).%.full.csv: maps/$(via).%.csv # fallback if no self map
104
	$(CP) $< $@
105
.PRECIOUS: maps/$(via).%.full.csv maps/$(via).%.csv $(selfMap)
106

    
107
maps += $(patsubst maps/%.csv,maps/%.full.csv,$(viaMaps))
108

    
109
maps: $(maps) _always ;
110

    
111
all += $(maps)
112

    
113
##### Mapping
114

    
115
dbFile := $(firstword $(wildcard src/db.*.sql))
116
inputFiles := $(wildcard src/*.csv src/*.xml)
117

    
118
+maps = $(filter maps/% $(mappings)/%,$(+_))
119
<in = $(firstword $(filter-out $(+maps),$(+_)) $(wildcard $(exts:%=src/*.$*.%)))
120
map = $(if $(<in),<$(<in) ,$(if $(mapEnv),env $(mapEnv) ,$(error\
121
No input file src/*.$*.{$(exts)} )))$(root)/map $(+maps)
122
map2db = env out_database=vegbien $(map)
123

    
124
##### Import to VegBIEN
125

    
126
ifneq ($(dbFile)$(inputFiles),)
127

    
128
log_ = $(@:-all=)$(if $(n),.n=$(n),).$(date).log
129
trace = $(log_:.log=.trace)
130
import = -(set -x; "time" env commit=1 verbose=1\
131
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
132
$(if $(n),,>>$(log_))) 2>&1$(if $(log),$(if $(n),|tee -a $(log_)))
133
# don't abort on import errors, which often relate to invalid input data
134

    
135
import: $(addprefix import-,$(tables)) _always ;
136

    
137
import-%: maps/VegBIEN.%.csv _always
138
	$(import)
139
# default:
140
import-%: _always ;
141

    
142
else
143
import: _always ;
144
endif
145

    
146
##### Log files from import
147

    
148
logs := $(wildcard *.log *.trace)
149

    
150
rm_logs: _always
151
	$(RM) $(logs)
152

    
153
##### Verification of import
154

    
155
ifneq ($(wildcard verify.*.ref*),)
156

    
157
verify: $(addprefix verify-,$(tables)) _always ;
158

    
159
verify-%: verify.%.ref verify.%.out _always
160
	-$(diffVerbose) $(+_)
161
# don't abort on verification errors, which are expected during development
162
# default:
163
verify-%: verify.%.out _always
164
	$(if $(wildcard $<),cat $<)
165
# don't run if verify.%.out's default do-nothing action was used
166

    
167
verify = $(psqlAsBien) --set=datasource="'$(db)'" --no-align\
168
--field-separator='	' --pset=footer=off --pset=null=NULL <$< >$@
169

    
170
verify.%.out: $(mappings)/verify.%.sql _always
171
	$(verify)
172
.PRECIOUS: verify.%.out
173
# default:
174
verify.%.out: _always ;
175

    
176
all += $(wildcard verify.*.out)
177

    
178
ifneq ($(dbFile),)
179
%.ref: %.ref.sql
180
	$(dbAsBien) $(db) <$< >$@
181
.PRECIOUS: %.ref
182
endif
183

    
184
else
185
verify: _always ;
186
endif
187

    
188
##### Testing
189

    
190
ifneq ($(wildcard test/),)
191

    
192
hasOwnRef = $(filter-out %.2-step.xml,$@)
193
testRef = $(1:.2-step.xml=.xml).ref
194

    
195
define runTest
196
@echo "Testing $(abspath $@)..."
197
>$@ env test=1 n=$(test_n) $(1)
198
@(set -x; $(diff) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
199
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
200
$(if $(hasOwnRef),\
201
echo $(emph)"To accept new test output:"$(endEmph);\
202
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
203
,\
204
echo $(emph)"Note: The preceding failed test is compared to another test's\
205
output"$(endEmph);\
206
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
207
)\
208
exit $$e;}
209
endef
210

    
211
test2File = $(call runTest,$(map))
212

    
213
tests :=
214

    
215
test/$(via).%.xml: maps/$(via).%.full.csv _always
216
	$(test2File)
217
tests += test/$(via).%.xml
218

    
219
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
220
	$(test2File)
221
tests += test/VegBIEN.%.xml
222

    
223
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _always
224
	-$(test2File)
225
# Don't abort tester if only 2-step test fails, as it's often finicky
226
tests += test/VegBIEN.%.2-step.xml
227

    
228
test/import.%.out: maps/VegBIEN.%.csv _always
229
	$(call runTest,$(map2db))
230
tests += test/import.%.out
231

    
232
.PRECIOUS: $(tests)
233

    
234
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
235

    
236
test: _always $(testOutputs) ;
237

    
238
all += $(testOutputs)
239

    
240
# Accepts a test output: make <test_output_path>-ok
241
%-ok: _always
242
	$(CP) $* $(call testRef,$*)
243

    
244
else
245
test: _always ;
246
endif
247

    
248
##### Input-type-specific
249

    
250
# Each input type needs var $(mapEnv) and targets install, uninstall
251

    
252
ifneq ($(dbFile),)
253

    
254
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
255
db := $(notdir $(realpath .))
256

    
257
#### Installation
258

    
259
install: _always db ;
260

    
261
uninstall: _always rm_db ;
262

    
263
#### DB-engine-specific
264

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

    
267
ifeq ($(dbEngineExt),my)
268

    
269
dbEngine := MySQL
270

    
271
bienPassword := $(shell cat $(root)/config/bien_password)
272
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
273
mysqlAsRoot := $(call mysqlAs,root)
274
dbAsBien := $(call mysqlAs,bien)
275

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

    
278
define createDb
279
echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
280
-$(mysqlAsRoot) --database=$(db) <$<
281
endef
282
# ignore errors in db import so that GRANT will still be run
283

    
284
db: $(dbFile) _always
285
	$(if $(dbExists),,$(createDb))
286
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
287

    
288
rm_db: _always
289
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
290
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
291
# ignore errors if grant not defined
292

    
293
### Unrecognized DB engine
294

    
295
else
296
$(error The DB filename $(dbFile) must be db.my.sql)
297
endif
298

    
299
# Must come after dbEngine is set
300
mapEnv := in_engine=$(dbEngine) in_database=$(db)
301

    
302
#### Unrecognized input type
303

    
304
else
305
install: _always ;
306
uninstall: _always ;
307
endif
(2-2/2)