Project

General

Profile

1
##### Configuration
2

    
3
nolog ?= $(test)
4
exts ?= csv xml
5
test_n ?= 2
6
tablesSort ?= plots organisms stems
7

    
8
##### Vars/functions
9

    
10
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
11

    
12
# Make
13
SHELL := /bin/bash
14
subMake = $(MAKE) $(@F) --directory=$(@D)
15
+_ = $(+:_%=)
16
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
17

    
18
# System
19
date = $(shell date +"%Y-%m-%d-%H-%M-%S")
20

    
21
# Terminal
22
esc := '['
23
reset := $(esc)'0m'
24
emph := $(esc)'7m '
25
endEmph := ' '$(reset)
26

    
27
# Commands
28
CP = cp -p
29
DIFF = diff --unified=2
30

    
31
# Paths
32
root := $(selfDir_uZPPqC)..
33
mappings := $(root)/mappings
34
psqlOpts := --set ON_ERROR_STOP=1 --quiet
35
psqlAsBien := $(root)/bin/psql_vegbien $(psqlOpts)
36

    
37
##### General targets
38

    
39
all: _always maps ;
40

    
41
.SUFFIXES:
42

    
43
_always:
44
.PHONY: _always
45

    
46
clean: _always
47
	$(RM) $(all)
48

    
49
remake: _always clean all ;
50

    
51
%.out: %.make _always
52
	./$* >$@
53
.PRECIOUS: %.out
54

    
55
$(root)/%: _always
56
	+$(subMake)
57

    
58
##### SVN
59

    
60
svn_props: _always
61
	svn propset svn:ignore $$'.~*\n*.log\n*.out\nsrc*' .
62
	$(if $(wildcard maps/),svn propset svn:ignore $$'.~*' maps)
63
	$(if $(wildcard test/),svn propset svn:ignore $$'*.out\n*.xml' test)
64

    
65
##### Installation
66

    
67
reinstall: _always uninstall install ;
68

    
69
##### Maps
70

    
71
allViaMaps := $(filter-out maps/VegBIEN.%.csv,$(wildcard maps/*.csv))
72
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
73

    
74
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
75

    
76
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
77
viaMaps += $(filter-out $(viaMaps),$(wildcard maps/$(via).*.csv))
78

    
79
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
80
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
81
$(wildcard maps/VegBIEN.*.csv))
82
tables := $(directMaps:maps/VegBIEN.%.csv=%)
83

    
84
maps: $(directMaps) _always ;
85

    
86
.PRECIOUS: $(allViaMaps) $(directMaps)
87

    
88
all += $(autogenMaps)
89

    
90
maps/VegBIEN.%.csv: maps/$(via).%.csv $(coreMap)
91
	$(root)/bin/join_sort <$+ >$@
92
.PRECIOUS: maps/VegBIEN.%.csv $(coreMap)
93

    
94
##### Mapping
95

    
96
dbFile := $(firstword $(wildcard src/db.*.sql))
97
inputFiles := $(wildcard src/*.csv src/*.xml)
98

    
99
+maps = $(filter maps/% $(mappings)/%,$(+_))
100
<in = $(firstword $(filter-out $(+maps),$(+_)) $(wildcard $(exts:%=src/*.$*.%)))
101
map = $(if $(<in),<$(<in) ,$(if $(mapEnv),env $(mapEnv) ,$(error\
102
No input file src/*.{$(tables)}.{$(exts)} )))$(root)/map $(+maps)
103
map2db = env out_database=vegbien $(map)
104

    
105
##### Import to VegBIEN
106

    
107
ifneq ($(dbFile)$(inputFiles),)
108

    
109
import: _always import-all ;
110

    
111
log = $(@:-all=)$(if $(n),.n=$(n),).$(date).log
112
import = -(set -x; "time" env commit=1 verbose=1 $(map2db)) $(if $(nolog),,\
113
$(if $(n),,>>$(log))) 2>&1$(if $(nolog),,$(if $(n),|tee -a $(log)))
114
# don't abort on import errors, which often relate to invalid input data
115

    
116
import-all: $(addprefix import-,$(tables)) _always ;
117

    
118
import-%: maps/VegBIEN.%.csv _always
119
	$(import)
120
# default:
121
import-%: _always ;
122

    
123
else
124
import: _always ;
125
endif
126

    
127
##### Log files from import
128

    
129
logs := $(wildcard *.log)
130

    
131
rm_logs: _always
132
	$(RM) $(logs)
133

    
134
##### Verification of import
135

    
136
ifneq ($(wildcard verify.ref*),)
137

    
138
verify: verify.ref verify.out _always
139
	-$(DIFF) $(+_)
140
# don't abort on verification errors, which are expected during development
141

    
142
all += verify.out
143

    
144
%.out: %.sql _always
145
	$(out_cmd)
146
.PRECIOUS: %.out
147
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off\
148
--pset=null=NULL <$< >$@
149

    
150
ifneq ($(dbFile),)
151
%.ref: %.ref.sql
152
	$(dbAsBien) $(db) <$< >$@
153
.PRECIOUS: %.ref
154
endif
155

    
156
else
157
verify: _always ;
158
endif
159

    
160
##### Testing
161

    
162
ifneq ($(wildcard test/),)
163

    
164
hasOwnRef = $(filter-out %.2-step.xml,$@)
165
testRef = $(1:.2-step.xml=.xml).ref
166

    
167
define runTest
168
@echo "Testing $(abspath $@)..."
169
>$@ env test=1 n=$(test_n) $(1)
170
@(set -x; $(DIFF) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
171
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
172
$(if $(hasOwnRef),\
173
echo $(emph)"To accept new test output:"$(endEmph);\
174
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
175
,\
176
echo $(emph)"Note: The preceding failed test is compared to another test's\
177
output"$(endEmph);\
178
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
179
)\
180
exit $$e;}
181
endef
182

    
183
test2File = $(call runTest,$(map))
184

    
185
tests :=
186

    
187
test/$(via).%.xml: maps/$(via).%.csv _always
188
	$(test2File)
189
tests += test/$(via).%.xml
190

    
191
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
192
	$(test2File)
193
tests += test/VegBIEN.%.xml
194

    
195
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _always
196
	-$(test2File)
197
# Don't abort tester if only 2-step test fails, as it's often finicky
198
tests += test/VegBIEN.%.2-step.xml
199

    
200
test/import.%.out: maps/VegBIEN.%.csv _always
201
	$(call runTest,$(map2db))
202
tests += test/import.%.out
203

    
204
.PRECIOUS: $(tests)
205

    
206
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
207

    
208
test: _always $(testOutputs) ;
209

    
210
all += $(testOutputs)
211

    
212
# Accepts a test output: make <test_output_path>-ok
213
%-ok: _always
214
	$(CP) $* $(call testRef,$*)
215

    
216
else
217
test: _always ;
218
endif
219

    
220
##### Input-type-specific
221

    
222
# Each input type needs var $(mapEnv) and targets install, uninstall
223

    
224
ifneq ($(dbFile),)
225

    
226
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
227
db := $(notdir $(realpath .))
228

    
229
#### Installation
230

    
231
install: _always db ;
232

    
233
uninstall: _always rm_db ;
234

    
235
#### DB-engine-specific
236

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

    
239
ifeq ($(dbEngineExt),my)
240

    
241
dbEngine := MySQL
242

    
243
bienPassword := $(shell cat $(root)/config/bien_password)
244
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
245
mysqlAsRoot := $(call mysqlAs,root)
246
dbAsBien := $(call mysqlAs,bien)
247

    
248
db: $(dbFile) _always
249
	echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
250
	-$(mysqlAsRoot) --database=$(db) <$<
251
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
252
# ignore errors in db import so that GRANT will still be run
253

    
254
rm_db: _always
255
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
256
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
257
# ignore errors if grant not defined
258

    
259
### Unrecognized DB engine
260

    
261
else
262
$(error The DB filename $(dbFile) must be db.my.sql)
263
endif
264

    
265
# Must come after dbEngine is set
266
mapEnv := in_engine=$(dbEngine) in_database=$(db)
267

    
268
#### Unrecognized input type
269

    
270
else
271
install: _always ;
272
uninstall: _always ;
273
endif
(2-2/2)