Project

General

Profile

1
##### Configuration
2

    
3
nolog ?= $(test)
4
exts ?= csv xml
5
test_n ?= 2
6
tablesSort ?= plots organisms stems
7

    
8
##### Vars/functions
9

    
10
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
11

    
12
# Make
13
SHELL := /bin/bash
14
subMake = $(MAKE) $(@F) --directory=$(@D)
15
+_ = $(+:_%=)
16
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
17

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

    
21
# Terminal
22
esc := '['
23
reset := $(esc)'0m'
24
emph := $(esc)'7m '
25
endEmph := ' '$(reset)
26

    
27
# Commands
28
CP = cp -p
29
DIFF = diff --unified=2
30

    
31
# Paths
32
root := $(selfDir_uZPPqC)..
33
mappings := $(root)/mappings
34
psqlOpts := --set ON_ERROR_STOP=1 --quiet
35
psqlAsBien := $(root)/bin/psql_vegbien $(psqlOpts)
36

    
37
##### General targets
38

    
39
all: _always maps ;
40

    
41
.SUFFIXES:
42

    
43
_always:
44
.PHONY: _always
45

    
46
clean: _always
47
	$(RM) $(all)
48

    
49
remake: _always clean all ;
50

    
51
%.out: %.make _always
52
	./$* >$@
53
.PRECIOUS: %.out
54

    
55
$(root)/%: _always
56
	+$(subMake)
57

    
58
##### SVN
59

    
60
svn_props: _always
61
	svn propset svn:ignore $$'.~*\n*.log\n*.out\nsrc*' .
62
	$(if $(wildcard maps/),svn propset svn:ignore $$'.~*' maps)
63
	$(if $(wildcard test/),svn propset svn:ignore $$'*.out\n*.xml' test)
64

    
65
##### Installation
66

    
67
reinstall: _always uninstall install ;
68

    
69
##### Maps
70

    
71
fullViaMap := maps/%.full.csv
72
allViaMaps := $(filter-out maps/VegBIEN.%.csv $(fullViaMap),\
73
$(wildcard maps/*.csv))
74
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
75

    
76
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
77
selfMap := $(mappings)/$(via).self.%.csv
78

    
79
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
80
viaMaps += $(filter-out $(viaMaps) $(fullViaMap),$(wildcard maps/$(via).*.csv))
81

    
82
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
83
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
84
$(wildcard maps/VegBIEN.*.csv))
85
tables := $(directMaps:maps/VegBIEN.%.csv=%)
86

    
87
.PRECIOUS: $(allViaMaps) $(directMaps)
88

    
89
maps :=
90

    
91
maps/VegBIEN.%.csv: maps/$(via).%.csv $(coreMap)
92
	$(root)/bin/join_union_sort <$+ >$@
93
.PRECIOUS: maps/VegBIEN.%.csv $(coreMap)
94

    
95
maps += $(autogenMaps)
96

    
97
maps/$(via).%.full.csv: maps/$(via).%.csv $(selfMap)
98
	env ignore=1 $(root)/bin/union <$+|$(root)/bin/sort_map >$@
99
maps/$(via).%.full.csv: maps/$(via).%.csv # fallback if no self map
100
	cp -p $< $@
101
.PRECIOUS: maps/$(via).%.full.csv maps/$(via).%.csv $(selfMap)
102

    
103
maps += $(patsubst maps/%.csv,maps/%.full.csv,$(viaMaps))
104

    
105
maps: $(maps) _always ;
106

    
107
all += $(maps)
108

    
109
##### Mapping
110

    
111
dbFile := $(firstword $(wildcard src/db.*.sql))
112
inputFiles := $(wildcard src/*.csv src/*.xml)
113

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

    
120
##### Import to VegBIEN
121

    
122
ifneq ($(dbFile)$(inputFiles),)
123

    
124
import: _always import-all ;
125

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

    
131
import-all: $(addprefix import-,$(tables)) _always ;
132

    
133
import-%: maps/VegBIEN.%.csv _always
134
	$(import)
135
# default:
136
import-%: _always ;
137

    
138
else
139
import: _always ;
140
endif
141

    
142
##### Log files from import
143

    
144
logs := $(wildcard *.log)
145

    
146
rm_logs: _always
147
	$(RM) $(logs)
148

    
149
##### Verification of import
150

    
151
ifneq ($(wildcard verify.ref*),)
152

    
153
verify: verify.ref verify.out _always
154
	-$(DIFF) $(+_)
155
# don't abort on verification errors, which are expected during development
156

    
157
all += verify.out
158

    
159
%.out: %.sql _always
160
	$(out_cmd)
161
.PRECIOUS: %.out
162
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off\
163
--pset=null=NULL <$< >$@
164

    
165
ifneq ($(dbFile),)
166
%.ref: %.ref.sql
167
	$(dbAsBien) $(db) <$< >$@
168
.PRECIOUS: %.ref
169
endif
170

    
171
else
172
verify: _always ;
173
endif
174

    
175
##### Testing
176

    
177
ifneq ($(wildcard test/),)
178

    
179
hasOwnRef = $(filter-out %.2-step.xml,$@)
180
testRef = $(1:.2-step.xml=.xml).ref
181

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

    
198
test2File = $(call runTest,$(map))
199

    
200
tests :=
201

    
202
test/$(via).%.xml: maps/$(via).%.full.csv _always
203
	$(test2File)
204
tests += test/$(via).%.xml
205

    
206
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
207
	$(test2File)
208
tests += test/VegBIEN.%.xml
209

    
210
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _always
211
	-$(test2File)
212
# Don't abort tester if only 2-step test fails, as it's often finicky
213
tests += test/VegBIEN.%.2-step.xml
214

    
215
test/import.%.out: maps/VegBIEN.%.csv _always
216
	$(call runTest,$(map2db))
217
tests += test/import.%.out
218

    
219
.PRECIOUS: $(tests)
220

    
221
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
222

    
223
test: _always $(testOutputs) ;
224

    
225
all += $(testOutputs)
226

    
227
# Accepts a test output: make <test_output_path>-ok
228
%-ok: _always
229
	$(CP) $* $(call testRef,$*)
230

    
231
else
232
test: _always ;
233
endif
234

    
235
##### Input-type-specific
236

    
237
# Each input type needs var $(mapEnv) and targets install, uninstall
238

    
239
ifneq ($(dbFile),)
240

    
241
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
242
db := $(notdir $(realpath .))
243

    
244
#### Installation
245

    
246
install: _always db ;
247

    
248
uninstall: _always rm_db ;
249

    
250
#### DB-engine-specific
251

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

    
254
ifeq ($(dbEngineExt),my)
255

    
256
dbEngine := MySQL
257

    
258
bienPassword := $(shell cat $(root)/config/bien_password)
259
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
260
mysqlAsRoot := $(call mysqlAs,root)
261
dbAsBien := $(call mysqlAs,bien)
262

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

    
265
define createDb
266
echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
267
-$(mysqlAsRoot) --database=$(db) <$<
268
endef
269
# ignore errors in db import so that GRANT will still be run
270

    
271
db: $(dbFile) _always
272
	$(if $(dbExists),,$(createDb))
273
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
274

    
275
rm_db: _always
276
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
277
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
278
# ignore errors if grant not defined
279

    
280
### Unrecognized DB engine
281

    
282
else
283
$(error The DB filename $(dbFile) must be db.my.sql)
284
endif
285

    
286
# Must come after dbEngine is set
287
mapEnv := in_engine=$(dbEngine) in_database=$(db)
288

    
289
#### Unrecognized input type
290

    
291
else
292
install: _always ;
293
uninstall: _always ;
294
endif
(2-2/2)