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 $$'.~*\n*.log\n*.trace\n*.out\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 $(wildcard $<),cat $<)
165
# don't run if verify/%.out's default do-nothing action was used
166

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

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

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

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

    
184
##### Testing
185

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

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

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

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

    
209
tests :=
210

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

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

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

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

    
228
.PRECIOUS: $(tests)
229

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

    
232
test: _always $(testOutputs) ;
233

    
234
all += $(testOutputs)
235

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

    
240
else
241
test: _always ;
242
endif
243

    
244
##### Input-type-specific
245

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

    
248
ifneq ($(dbFile),)
249

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

    
253
#### Installation
254

    
255
install: _always db ;
256

    
257
uninstall: _always rm_db ;
258

    
259
#### DB-engine-specific
260

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

    
263
ifeq ($(dbEngineExt),my)
264

    
265
dbEngine := MySQL
266

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

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

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

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

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

    
289
### Unrecognized DB engine
290

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

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

    
298
#### Unrecognized input type
299

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