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
##### SVN
63

    
64
svn_props: _always
65
	svn propset svn:ignore $$'*.log\n*.trace\nsrc*' .
66
	$(if $(wildcard maps/),svn propset svn:ignore $$'.~*' maps)
67
	$(if $(wildcard verify/),svn propset svn:ignore $$'*.out' verify)
68
	$(if $(wildcard test/),svn propset svn:ignore $$'*.out\n*.xml' test)
69

    
70
##### Installation
71

    
72
reinstall: _always uninstall install ;
73

    
74
##### Maps
75

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

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

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

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

    
92
# Must come before $(root)/% to override it
93
$(selfMap): _always
94
	-+$(subMake)
95
# ignore errors if $(selfMap) does not exist
96

    
97
$(root)/%: _always
98
	+$(subMake)
99

    
100
maps :=
101

    
102
maps/VegBIEN.%.csv: maps/$(via).%.csv $(coreMap)
103
	$(root)/bin/join_union_sort <$+ >$@
104

    
105
maps += $(autogenMaps)
106

    
107
makeFullCsv = $(if $(shell test -e $(word 2,$+) && echo t),\
108
env ignore=1 $(root)/bin/union <$+|$(root)/bin/sort_map >$@,$(CP) $< $@)
109

    
110
maps/$(via).%.full.csv: maps/$(via).%.csv $(selfMap)
111
	$(makeFullCsv)
112
# can't use $(wildcard) because it won't recheck file after $(selfMap) is run
113

    
114
maps += $(patsubst maps/%.csv,maps/%.full.csv,$(viaMaps))
115

    
116
maps: $(maps) _always ;
117

    
118
all += $(maps)
119

    
120
##### Mapping
121

    
122
dbFile := $(firstword $(wildcard src/db.*.sql))
123
inputFiles := $(wildcard src/*.csv src/*.xml)
124

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

    
131
##### Import to VegBIEN
132

    
133
ifneq ($(dbFile)$(inputFiles),)
134

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

    
142
import: $(addprefix import-,$(tables)) _always ;
143

    
144
import-%: maps/VegBIEN.%.csv _always
145
	$(import)
146
# default:
147
import-%: _always ;
148

    
149
else
150
import: _always ;
151
endif
152

    
153
##### Log files from import
154

    
155
logs := $(wildcard *.log *.trace)
156

    
157
rm_logs: _always
158
	$(RM) $(logs)
159

    
160
##### Verification of import
161

    
162
verify: $(addprefix verify-,$(tables)) _always ;
163

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

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

    
179
verify/%.out: $(mappings)/verify.%.sql _always
180
	$(verify)
181
# default:
182
verify/%.out: _always ;
183

    
184
all += $(wildcard verify/*.out)
185

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

    
191
##### Testing
192

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

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

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

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

    
216
tests :=
217

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

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

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

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

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

    
237
test: _always $(testOutputs) ;
238

    
239
all += $(testOutputs)
240

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

    
245
else
246
test: _always ;
247
endif
248

    
249
##### Input-type-specific
250

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

    
253
ifneq ($(dbFile),)
254

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

    
258
#### Installation
259

    
260
install: _always db ;
261

    
262
uninstall: _always rm_db ;
263

    
264
#### DB-engine-specific
265

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

    
268
ifeq ($(dbEngineExt),my)
269

    
270
dbEngine := MySQL
271

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

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

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

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

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

    
294
### Unrecognized DB engine
295

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

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

    
303
#### Unrecognized input type
304

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