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
datasrc := $(notdir $(realpath .))
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 $$'*.log\n*.trace\nsrc*' .
67
	$(if $(wildcard maps/),svn propset svn:ignore $$'.~*' maps)
68
	$(if $(wildcard verify/),svn propset svn:ignore $$'*.out' verify)
69
	$(if $(wildcard test/),svn propset svn:ignore $$'*.out\n*.xml' test)
70

    
71
##### Installation
72

    
73
reinstall: _always uninstall install ;
74

    
75
##### Maps
76

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

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

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

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

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

    
95
maps :=
96

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

    
101
maps += $(autogenMaps)
102

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

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

    
111
maps: $(maps) _always ;
112

    
113
all += $(maps)
114

    
115
##### Mapping
116

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

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

    
126
##### Import to VegBIEN
127

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

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

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

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

    
144
else
145
import: _always ;
146
endif
147

    
148
##### Log files from import
149

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

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

    
155
##### Verification of import
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 $(shell test -e $< && echo t),cat $<)
165
# don't run if verify/%.out's default do-nothing action was used
166
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
167

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

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

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

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

    
185
##### Testing
186

    
187
ifneq ($(wildcard test/),)
188

    
189
hasOwnRef = $(filter-out %.2-step.xml,$@)
190
testRef = $(1:.2-step.xml=.xml).ref
191

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

    
208
test2File = $(call runTest,$(map))
209

    
210
tests :=
211

    
212
test/$(via).%.xml: maps/$(via).%.full.csv _always
213
	$(test2File)
214
tests += test/$(via).%.xml
215

    
216
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
217
	$(test2File)
218
tests += test/VegBIEN.%.xml
219

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

    
225
test/import.%.out: maps/VegBIEN.%.csv _always
226
	$(call runTest,$(map2db))
227
tests += test/import.%.out
228

    
229
.PRECIOUS: $(tests)
230

    
231
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
232

    
233
test: _always $(testOutputs) ;
234

    
235
all += $(testOutputs)
236

    
237
# Accepts a test output: make <test_output_path>-ok
238
%-ok: _always
239
	$(CP) $* $(call testRef,$*)
240

    
241
else
242
test: _always ;
243
endif
244

    
245
##### Input-type-specific
246

    
247
# Each input type needs var $(mapEnv) and targets install, uninstall
248

    
249
ifneq ($(dbFile),)
250

    
251
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
252
db := $(datasrc)
253

    
254
#### Installation
255

    
256
install: _always db ;
257

    
258
uninstall: _always rm_db ;
259

    
260
#### DB-engine-specific
261

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

    
264
ifeq ($(dbEngineExt),my)
265

    
266
dbEngine := MySQL
267

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

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

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

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

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

    
290
### Unrecognized DB engine
291

    
292
else
293
$(error The DB filename $(dbFile) must be db.my.sql)
294
endif
295

    
296
# Must come after dbEngine is set
297
mapEnv := in_engine=$(dbEngine) in_database=$(db)
298

    
299
#### Unrecognized input type
300

    
301
else
302
install: _always ;
303
uninstall: _always ;
304
endif
(2-2/2)