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
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
root := $(selfDir_uZPPqC)..
39
mappings := $(root)/mappings
40
datasrc := $(notdir $(realpath .))
41
psqlOpts := --set ON_ERROR_STOP=1 --quiet
42
psqlAsBien := $(root)/bin/psql_vegbien $(psqlOpts)
43

    
44
##### General targets
45

    
46
all: _always maps ;
47

    
48
.SUFFIXES:
49

    
50
_always:
51
.PHONY: _always
52

    
53
clean: _always
54
	$(RM) $(all)
55

    
56
remake: _always clean all ;
57

    
58
%.out: %.make _always
59
	./$* >$@
60
.PRECIOUS: %.out
61

    
62
$(root)/%: _always
63
	+$(subMake)
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 verify/),svn propset svn:ignore $$'*.out' verify)
71
	$(if $(wildcard test/),svn propset svn:ignore $$'*.out\n*.xml' test)
72

    
73
##### Installation
74

    
75
reinstall: _always uninstall install ;
76

    
77
##### Maps
78

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

    
84
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
85
selfMap := $(mappings)/$(via).self.%.csv
86

    
87
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
88
viaMaps += $(filter-out $(viaMaps) $(fullViaMap),$(wildcard maps/$(via).*.csv))
89

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

    
95
.PRECIOUS: $(allViaMaps) $(directMaps)
96

    
97
maps :=
98

    
99
maps/VegBIEN.%.csv: maps/$(via).%.csv $(coreMap)
100
	$(root)/bin/join_union_sort <$+ >$@
101
.PRECIOUS: maps/VegBIEN.%.csv $(coreMap)
102

    
103
maps += $(autogenMaps)
104

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

    
111
maps += $(patsubst maps/%.csv,maps/%.full.csv,$(viaMaps))
112

    
113
maps: $(maps) _always ;
114

    
115
all += $(maps)
116

    
117
##### Mapping
118

    
119
dbFile := $(firstword $(wildcard src/db.*.sql))
120
inputFiles := $(wildcard src/*.csv src/*.xml)
121

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

    
128
##### Import to VegBIEN
129

    
130
ifneq ($(dbFile)$(inputFiles),)
131

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

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

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

    
146
else
147
import: _always ;
148
endif
149

    
150
##### Log files from import
151

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

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

    
157
##### Verification of import
158

    
159
verify: $(addprefix verify-,$(tables)) _always ;
160

    
161
verify-%: verify/%.ref verify/%.out _always
162
	-$(diffVerbose) $(+_)
163
# don't abort on verification errors, which are expected during development
164
# default:
165
verify-%: verify/%.out _always
166
	$(if $(shell test -e $< && echo t),cat $<)
167
# don't run if verify/%.out's default do-nothing action was used
168
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
169

    
170
define verify
171
$(mkdir)
172
$(psqlAsBien) --set=datasource="'$(datasrc)'" --no-align\
173
--field-separator=$$'\t' --pset=footer=off --pset=null=NULL <$< >$@
174
endef
175

    
176
verify/%.out: $(mappings)/verify.%.sql _always
177
	$(verify)
178
.PRECIOUS: verify/%.out
179
# default:
180
verify/%.out: _always ;
181

    
182
all += $(wildcard verify/*.out)
183

    
184
ifneq ($(dbFile),)
185
%.ref: %.ref.sql
186
	$(dbAsBien) $(db) <$< >$@
187
.PRECIOUS: %.ref
188
endif
189

    
190
##### Testing
191

    
192
ifneq ($(wildcard test/),)
193

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

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

    
213
test2File = $(call runTest,$(map))
214

    
215
tests :=
216

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

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

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

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

    
234
.PRECIOUS: $(tests)
235

    
236
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
237

    
238
test: _always $(testOutputs) ;
239

    
240
all += $(testOutputs)
241

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

    
246
else
247
test: _always ;
248
endif
249

    
250
##### Input-type-specific
251

    
252
# Each input type needs var $(mapEnv) and targets install, uninstall
253

    
254
ifneq ($(dbFile),)
255

    
256
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
257
db := $(datasrc)
258

    
259
#### Installation
260

    
261
install: _always db ;
262

    
263
uninstall: _always rm_db ;
264

    
265
#### DB-engine-specific
266

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

    
269
ifeq ($(dbEngineExt),my)
270

    
271
dbEngine := MySQL
272

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

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

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

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

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

    
295
### Unrecognized DB engine
296

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

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

    
304
#### Unrecognized input type
305

    
306
else
307
install: _always ;
308
uninstall: _always ;
309
endif
(2-2/2)