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
datasrc := $(notdir $(realpath .))
39
root := $(selfDir_uZPPqC)..
40
bin := $(root)/bin
41
mappings := $(root)/mappings
42

    
43
# Commands
44
join = $(bin)/join_union_sort
45
psqlOpts := --set ON_ERROR_STOP=1 --quiet
46
psqlAsBien := $(bin)/psql_vegbien $(psqlOpts)
47

    
48
##### General targets
49

    
50
all: _always maps ;
51

    
52
.SUFFIXES: # turn off built-in suffix rules
53
.SECONDARY: # don't automatically delete intermediate files
54
.DELETE_ON_ERROR: # delete target if recipe fails
55

    
56
_always:
57
.PHONY: _always
58

    
59
clean: _always
60
	$(RM) $(all)
61

    
62
%.out: %.make _always
63
	./$* >$@
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
srcMap := maps/src.%.csv
80
fullViaMap := maps/%.full.csv
81
directMap := maps/VegBIEN.%.csv
82
allViaMaps := $(filter-out $(srcMap) $(fullViaMap) $(directMap),\
83
$(wildcard maps/*.csv))
84
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
85

    
86
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
87
selfMap := $(mappings)/$(via).self.%.csv
88
noEmptyMap := $(mappings)/$(via)-VegBIEN.%.no_empty.csv
89

    
90
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
91
viaMaps += $(filter-out $(viaMaps) $(fullViaMap),$(wildcard maps/$(via).*.csv))
92

    
93
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
94
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
95
$(wildcard maps/VegBIEN.*.csv))
96
tables := $(directMaps:maps/VegBIEN.%.csv=%)
97

    
98
srcMaps := $(filter-out maps/src.join.%.csv,$(wildcard maps/src.*.csv))
99
srcJoinMaps := $(srcMaps:maps/src.%.csv=maps/src.join.%.csv)
100

    
101
# Must come before $(root)/% to override it
102
$(selfMap): _always
103
	-+$(subMake)
104
# ignore errors if $(selfMap) does not exist
105

    
106
$(root)/%: _always
107
	+$(subMake)
108

    
109
maps :=
110

    
111
maps/VegBIEN.%.csv: maps/$(via).%.csv $(coreMap)
112
	$(join) <$+ >$@
113
maps += $(autogenMaps)
114

    
115
makeFullCsv = $(if $(shell test -e $(word 2,$+) && echo t),\
116
env ignore=1 $(bin)/union <$+|$(bin)/sort_map >$@,$(CP) $< $@)
117
# can't use $(wildcard) because it won't recheck file after $(selfMap) is run
118

    
119
maps/$(via).%.full.csv: maps/$(via).%.csv $(selfMap)
120
	$(makeFullCsv)
121
maps += $(patsubst maps/%.csv,maps/%.full.csv,$(viaMaps))
122

    
123
maps/src.join.%.csv: maps/src.%.csv maps/$(via).%.full.csv $(noEmptyMap)
124
	$(bin)/cols 0 0 <$<|$(bin)/join $(word 2,$+)|$(join) $(word 3,$+) >$@
125
maps += $(srcJoinMaps)
126

    
127
maps: $(maps) _always ;
128

    
129
all += $(maps)
130

    
131
##### Mapping
132

    
133
dbFile := $(firstword $(wildcard src/db.*.sql))
134
inputFiles := $(wildcard src/*.csv src/*.xml)
135

    
136
+maps = $(filter maps/% $(mappings)/%,$(+_))
137
<in = $(firstword $(filter-out $(+maps),$(+_)) $(wildcard $(exts:%=src/*.$*.%)))
138
map = $(if $(<in),<$(<in) ,$(if $(mapEnv),env $(mapEnv) ,$(error\
139
No input file src/*.$*.{$(exts)} )))$(root)/map $(+maps)
140
map2db = env out_database=vegbien $(map)
141

    
142
##### Import to VegBIEN
143

    
144
ifneq ($(dbFile)$(inputFiles),)
145

    
146
log_ = $(@:-all=)$(if $(n),.n=$(n),).$(date).log
147
trace = $(log_:.log=.trace)
148
import = -(set -x; "time" env commit=1 verbose=1\
149
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
150
$(if $(n),,>>$(log_))) 2>&1$(if $(log),$(if $(n),|tee -a $(log_)))
151
# don't abort on import errors, which often relate to invalid input data
152

    
153
import: $(addprefix import-,$(tables)) _always ;
154

    
155
import-%: maps/VegBIEN.%.csv _always
156
	$(import)
157
# default:
158
import-%: _always ;
159

    
160
else
161
import: _always ;
162
endif
163

    
164
##### Log files from import
165

    
166
logs := $(wildcard *.log *.trace)
167

    
168
rm_logs: _always
169
	$(RM) $(logs)
170

    
171
##### Verification of import
172

    
173
verify: $(addprefix verify-,$(tables)) _always ;
174

    
175
verify-%: verify/%.ref verify/%.out _always
176
	-$(diffVerbose) $(+_)
177
# don't abort on verification errors, which are expected during development
178
# default:
179
verify-%: verify/%.out _always
180
	$(if $(shell test -e $< && echo t),cat $<)
181
# don't run if verify/%.out's default do-nothing action was used
182
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
183

    
184
define verify
185
$(mkdir)
186
"time" $(psqlAsBien) --set=datasource="'$(datasrc)'" --no-align\
187
--field-separator=$$'\t' --pset=footer=off --pset=null=NULL <$< >$@
188
endef
189

    
190
verify/%.out: $(mappings)/verify.%.sql _always
191
	$(verify)
192
# default:
193
verify/%.out: _always ;
194

    
195
all += $(wildcard verify/*.out)
196

    
197
ifneq ($(dbFile),)
198
%.ref: %.ref.sql
199
	$(dbAsBien) $(db) <$< >$@
200
endif
201

    
202
##### Testing
203

    
204
ifneq ($(wildcard test/),)
205

    
206
hasOwnRef = $(filter-out %.2-step.xml,$@)
207
testRef = $(1:.2-step.xml=.xml).ref
208

    
209
define runTest
210
@echo "Testing $(abspath $@)..."
211
>$@ env test=1 n=$(test_n) $(1)
212
@(set -x; $(diff) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
213
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
214
$(if $(hasOwnRef),\
215
echo $(emph)"To accept new test output:"$(endEmph);\
216
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
217
,\
218
echo $(emph)"Note: The preceding failed test is compared to another test's\
219
output"$(endEmph);\
220
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
221
)\
222
exit $$e;}
223
endef
224

    
225
test2File = $(call runTest,$(map))
226

    
227
tests :=
228

    
229
test/$(via).%.xml: maps/$(via).%.full.csv _always
230
	$(test2File)
231
tests += test/$(via).%.xml
232

    
233
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
234
	$(test2File)
235
tests += test/VegBIEN.%.xml
236

    
237
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _always
238
	-$(test2File)
239
# Don't abort tester if only 2-step test fails, as it's often finicky
240
tests += test/VegBIEN.%.2-step.xml
241

    
242
test/import.%.out: maps/VegBIEN.%.csv _always
243
	$(call runTest,$(map2db))
244
tests += test/import.%.out
245

    
246
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
247

    
248
test: _always $(testOutputs) ;
249

    
250
all += $(testOutputs)
251

    
252
# Accepts a test output: make <test_output_path>-ok
253
%-ok: _always
254
	$(CP) $* $(call testRef,$*)
255

    
256
else
257
test: _always ;
258
endif
259

    
260
##### Input-type-specific
261

    
262
# Each input type needs var $(mapEnv) and targets install, uninstall
263

    
264
ifneq ($(dbFile),)
265

    
266
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
267
db := $(datasrc)
268

    
269
#### Installation
270

    
271
install: _always db ;
272

    
273
uninstall: _always rm_db ;
274

    
275
#### DB-engine-specific
276

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

    
279
ifeq ($(dbEngineExt),my)
280

    
281
dbEngine := MySQL
282

    
283
bienPassword := $(shell cat $(root)/config/bien_password)
284
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
285
mysqlAsRoot := $(call mysqlAs,root)
286
dbAsBien := $(call mysqlAs,bien)
287

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

    
290
define createDb
291
echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
292
-$(mysqlAsRoot) --database=$(db) <$<
293
endef
294
# ignore errors in db import so that GRANT will still be run
295

    
296
db: $(dbFile) _always
297
	$(if $(dbExists),,$(createDb))
298
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
299

    
300
rm_db: _always
301
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
302
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
303
# ignore errors if grant not defined
304

    
305
### Unrecognized DB engine
306

    
307
else
308
$(error The DB filename $(dbFile) must be db.my.sql)
309
endif
310

    
311
# Must come after dbEngine is set
312
mapEnv := in_engine=$(dbEngine) in_database=$(db)
313

    
314
#### Unrecognized input type
315

    
316
else
317
install: _always ;
318
uninstall: _always ;
319
endif
(2-2/2)