Project

General

Profile

1
##### Configuration
2

    
3
log ?= $(if $(test),,1)
4
profile ?=
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: # turn off built-in suffix rules
49
.SECONDARY: # don't automatically delete intermediate files
50

    
51
_always:
52
.PHONY: _always
53

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

    
57
remake: _always clean all ;
58

    
59
%.out: %.make _always
60
	./$* >$@
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
maps :=
96

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

    
100
maps += $(autogenMaps)
101

    
102
maps/$(via).%.full.csv: maps/$(via).%.csv $(selfMap)
103
	env ignore=1 $(root)/bin/union <$+|$(root)/bin/sort_map >$@
104
maps/$(via).%.full.csv: maps/$(via).%.csv # fallback if no self map
105
	$(CP) $< $@
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
log_ = $(@:-all=)$(if $(n),.n=$(n),).$(date).log
129
trace = $(log_:.log=.trace)
130
import = -(set -x; "time" env commit=1 verbose=1\
131
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
132
$(if $(n),,>>$(log_))) 2>&1$(if $(log),$(if $(n),|tee -a $(log_)))
133
# don't abort on import errors, which often relate to invalid input data
134

    
135
import: $(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 *.trace)
149

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

    
153
##### Verification of import
154

    
155
verify: $(addprefix verify-,$(tables)) _always ;
156

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

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

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

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

    
179
ifneq ($(dbFile),)
180
%.ref: %.ref.sql
181
	$(dbAsBien) $(db) <$< >$@
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
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
229

    
230
test: _always $(testOutputs) ;
231

    
232
all += $(testOutputs)
233

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

    
238
else
239
test: _always ;
240
endif
241

    
242
##### Input-type-specific
243

    
244
# Each input type needs var $(mapEnv) and targets install, uninstall
245

    
246
ifneq ($(dbFile),)
247

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

    
251
#### Installation
252

    
253
install: _always db ;
254

    
255
uninstall: _always rm_db ;
256

    
257
#### DB-engine-specific
258

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

    
261
ifeq ($(dbEngineExt),my)
262

    
263
dbEngine := MySQL
264

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

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

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

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

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

    
287
### Unrecognized DB engine
288

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

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

    
296
#### Unrecognized input type
297

    
298
else
299
install: _always ;
300
uninstall: _always ;
301
endif
(2-2/2)