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
schemas := $(root)/schemas
39
psqlOpts := --set ON_ERROR_STOP=1 --quiet
40
psqlAsBien := $(root)/bin/psql_vegbien $(psqlOpts)
41

    
42
##### General targets
43

    
44
all: _always maps ;
45

    
46
.SUFFIXES:
47

    
48
_always:
49
.PHONY: _always
50

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

    
54
remake: _always clean all ;
55

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

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

    
63
##### SVN
64

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

    
70
##### Installation
71

    
72
reinstall: _always uninstall install ;
73

    
74
##### Maps
75

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

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

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

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

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

    
94
maps :=
95

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

    
100
maps += $(autogenMaps)
101

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

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

    
110
maps: $(maps) _always ;
111

    
112
all += $(maps)
113

    
114
##### Mapping
115

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

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

    
125
##### Import to VegBIEN
126

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

    
129
import: _always import-all ;
130

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

    
138
import-all: $(addprefix import-,$(tables)) _always ;
139

    
140
import-%: maps/VegBIEN.%.csv _always
141
	$(import)
142
# default:
143
import-%: _always ;
144

    
145
else
146
import: _always ;
147
endif
148

    
149
##### Log files from import
150

    
151
logs := $(wildcard *.log *.trace)
152

    
153
rm_logs: _always
154
	$(RM) $(logs)
155

    
156
##### Verification of import
157

    
158
ifneq ($(wildcard verify.ref*),)
159

    
160
verify: verify.ref verify.out _always
161
	-$(diffVerbose) $(+_)
162
# don't abort on verification errors, which are expected during development
163

    
164
all += verify.out
165

    
166
verify.out: $(schemas)/verify.sql _always
167
	$(out_cmd)
168
.PRECIOUS: %.out
169
out_cmd = $(psqlAsBien) --set=datasource="'$(db)'" --no-align\
170
--field-separator='	' --pset=footer=off --pset=null=NULL <$< >$@
171

    
172
ifneq ($(dbFile),)
173
%.ref: %.ref.sql
174
	$(dbAsBien) $(db) <$< >$@
175
.PRECIOUS: %.ref
176
endif
177

    
178
else
179
verify: _always ;
180
endif
181

    
182
##### Testing
183

    
184
ifneq ($(wildcard test/),)
185

    
186
hasOwnRef = $(filter-out %.2-step.xml,$@)
187
testRef = $(1:.2-step.xml=.xml).ref
188

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

    
205
test2File = $(call runTest,$(map))
206

    
207
tests :=
208

    
209
test/$(via).%.xml: maps/$(via).%.full.csv _always
210
	$(test2File)
211
tests += test/$(via).%.xml
212

    
213
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
214
	$(test2File)
215
tests += test/VegBIEN.%.xml
216

    
217
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _always
218
	-$(test2File)
219
# Don't abort tester if only 2-step test fails, as it's often finicky
220
tests += test/VegBIEN.%.2-step.xml
221

    
222
test/import.%.out: maps/VegBIEN.%.csv _always
223
	$(call runTest,$(map2db))
224
tests += test/import.%.out
225

    
226
.PRECIOUS: $(tests)
227

    
228
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
229

    
230
test: _always $(testOutputs) ;
231

    
232
all += $(testOutputs)
233

    
234
# Accepts a test output: make <test_output_path>-ok
235
%-ok: _always
236
	$(CP) $* $(call testRef,$*)
237

    
238
else
239
test: _always ;
240
endif
241

    
242
##### Input-type-specific
243

    
244
# Each input type needs var $(mapEnv) and targets install, uninstall
245

    
246
ifneq ($(dbFile),)
247

    
248
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
249
db := $(notdir $(realpath .))
250

    
251
#### Installation
252

    
253
install: _always db ;
254

    
255
uninstall: _always rm_db ;
256

    
257
#### DB-engine-specific
258

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

    
261
ifeq ($(dbEngineExt),my)
262

    
263
dbEngine := MySQL
264

    
265
bienPassword := $(shell cat $(root)/config/bien_password)
266
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
267
mysqlAsRoot := $(call mysqlAs,root)
268
dbAsBien := $(call mysqlAs,bien)
269

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

    
272
define createDb
273
echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
274
-$(mysqlAsRoot) --database=$(db) <$<
275
endef
276
# ignore errors in db import so that GRANT will still be run
277

    
278
db: $(dbFile) _always
279
	$(if $(dbExists),,$(createDb))
280
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
281

    
282
rm_db: _always
283
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
284
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
285
# ignore errors if grant not defined
286

    
287
### Unrecognized DB engine
288

    
289
else
290
$(error The DB filename $(dbFile) must be db.my.sql)
291
endif
292

    
293
# Must come after dbEngine is set
294
mapEnv := in_engine=$(dbEngine) in_database=$(db)
295

    
296
#### Unrecognized input type
297

    
298
else
299
install: _always ;
300
uninstall: _always ;
301
endif
(2-2/2)