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
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*.trace\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
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-all: $(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
ifneq ($(wildcard verify.ref*),)
158

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

    
163
all += verify.out
164

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

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

    
177
else
178
verify: _always ;
179
endif
180

    
181
##### Testing
182

    
183
ifneq ($(wildcard test/),)
184

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

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

    
204
test2File = $(call runTest,$(map))
205

    
206
tests :=
207

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

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

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

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

    
225
.PRECIOUS: $(tests)
226

    
227
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
228

    
229
test: _always $(testOutputs) ;
230

    
231
all += $(testOutputs)
232

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

    
237
else
238
test: _always ;
239
endif
240

    
241
##### Input-type-specific
242

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

    
245
ifneq ($(dbFile),)
246

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

    
250
#### Installation
251

    
252
install: _always db ;
253

    
254
uninstall: _always rm_db ;
255

    
256
#### DB-engine-specific
257

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

    
260
ifeq ($(dbEngineExt),my)
261

    
262
dbEngine := MySQL
263

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

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

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

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

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

    
286
### Unrecognized DB engine
287

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

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

    
295
#### Unrecognized input type
296

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