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
# note that this will also run if the self map has not yet been made
107

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

    
110
maps: $(maps) _always ;
111

    
112
all += $(maps)
113

    
114
##### Mapping
115

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

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

    
125
##### Import to VegBIEN
126

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

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

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

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

    
143
else
144
import: _always ;
145
endif
146

    
147
##### Log files from import
148

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

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

    
154
##### Verification of import
155

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

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

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

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

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

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

    
231
test: _always $(testOutputs) ;
232

    
233
all += $(testOutputs)
234

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

    
239
else
240
test: _always ;
241
endif
242

    
243
##### Input-type-specific
244

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

    
247
ifneq ($(dbFile),)
248

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

    
252
#### Installation
253

    
254
install: _always db ;
255

    
256
uninstall: _always rm_db ;
257

    
258
#### DB-engine-specific
259

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

    
262
ifeq ($(dbEngineExt),my)
263

    
264
dbEngine := MySQL
265

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

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

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

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

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

    
288
### Unrecognized DB engine
289

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

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

    
297
#### Unrecognized input type
298

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