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
termCols := $(shell tput cols)
23
esc := '['
24
reset := $(esc)'0m'
25
emph := $(esc)'7m '
26
endEmph := ' '$(reset)
27

    
28
# Commands
29
CP = cp -p
30
diff = diff --unified=2
31
diffVerbose = $(if $(verbose),diff --side-by-side --left-column\
32
--width=$(termCols),$(diff))
33

    
34
# Paths
35
root := $(selfDir_uZPPqC)..
36
mappings := $(root)/mappings
37
schemas := $(root)/schemas
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*.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 -p $< $@
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
import: _always import-all ;
129

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

    
135
import-all: $(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)
149

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

    
153
##### Verification of import
154

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

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

    
161
all += verify.out
162

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

    
169
ifneq ($(dbFile),)
170
%.ref: %.ref.sql
171
	$(dbAsBien) $(db) <$< >$@
172
.PRECIOUS: %.ref
173
endif
174

    
175
else
176
verify: _always ;
177
endif
178

    
179
##### Testing
180

    
181
ifneq ($(wildcard test/),)
182

    
183
hasOwnRef = $(filter-out %.2-step.xml,$@)
184
testRef = $(1:.2-step.xml=.xml).ref
185

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

    
202
test2File = $(call runTest,$(map))
203

    
204
tests :=
205

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

    
210
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
211
	$(test2File)
212
tests += test/VegBIEN.%.xml
213

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

    
219
test/import.%.out: maps/VegBIEN.%.csv _always
220
	$(call runTest,$(map2db))
221
tests += test/import.%.out
222

    
223
.PRECIOUS: $(tests)
224

    
225
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
226

    
227
test: _always $(testOutputs) ;
228

    
229
all += $(testOutputs)
230

    
231
# Accepts a test output: make <test_output_path>-ok
232
%-ok: _always
233
	$(CP) $* $(call testRef,$*)
234

    
235
else
236
test: _always ;
237
endif
238

    
239
##### Input-type-specific
240

    
241
# Each input type needs var $(mapEnv) and targets install, uninstall
242

    
243
ifneq ($(dbFile),)
244

    
245
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
246
db := $(notdir $(realpath .))
247

    
248
#### Installation
249

    
250
install: _always db ;
251

    
252
uninstall: _always rm_db ;
253

    
254
#### DB-engine-specific
255

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

    
258
ifeq ($(dbEngineExt),my)
259

    
260
dbEngine := MySQL
261

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

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

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

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

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

    
284
### Unrecognized DB engine
285

    
286
else
287
$(error The DB filename $(dbFile) must be db.my.sql)
288
endif
289

    
290
# Must come after dbEngine is set
291
mapEnv := in_engine=$(dbEngine) in_database=$(db)
292

    
293
#### Unrecognized input type
294

    
295
else
296
install: _always ;
297
uninstall: _always ;
298
endif
(2-2/2)