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 verify ;
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
# ignore import errors, which are often benign (e.g. invalid date format)
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

    
141
all += verify.out
142

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

    
149
else
150
verify: _always ;
151
endif
152

    
153
##### Testing
154

    
155
ifneq ($(wildcard test/),)
156

    
157
hasOwnRef = $(filter-out %.2-step.xml,$@)
158
testRef = $(1:.2-step.xml=.xml).ref
159

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

    
176
test2File = $(call runTest,$(map))
177

    
178
tests :=
179

    
180
test/$(via).%.xml: maps/$(via).%.csv _always
181
	$(test2File)
182
tests += test/$(via).%.xml
183

    
184
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
185
	$(test2File)
186
tests += test/VegBIEN.%.xml
187

    
188
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _always
189
	-$(test2File)
190
# Don't abort tester if only 2-step test fails, as it's often finicky
191
tests += test/VegBIEN.%.2-step.xml
192

    
193
test/import.%.out: maps/VegBIEN.%.csv _always
194
	$(call runTest,$(map2db))
195
tests += test/import.%.out
196

    
197
.PRECIOUS: $(tests)
198

    
199
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
200

    
201
test: _always $(testOutputs) ;
202

    
203
all += $(testOutputs)
204

    
205
# Accepts a test output: make <test_output_path>-ok
206
%-ok: _always
207
	$(CP) $* $(call testRef,$*)
208

    
209
else
210
test: _always ;
211
endif
212

    
213
##### Input-type-specific
214

    
215
# Each input type needs var $(mapEnv) and targets install, uninstall
216

    
217
ifneq ($(dbFile),)
218

    
219
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
220
db := $(notdir $(realpath .))
221

    
222
%.ref: %.ref.sql
223
	$(inputDbAsBien) $(db) <$< >$@
224
.PRECIOUS: %.ref
225

    
226
#### Installation
227

    
228
install: _always db ;
229

    
230
uninstall: _always rm_db ;
231

    
232
#### DB-engine-specific
233

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

    
236
ifeq ($(dbEngineExt),my)
237

    
238
dbEngine := MySQL
239

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

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

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

    
256
### Unrecognized DB engine
257

    
258
else
259
$(error The DB filename $(dbFile) must be db.my.sql)
260
endif
261

    
262
# Must come after dbEngine is set
263
mapEnv := in_engine=$(dbEngine) in_database=$(db)
264

    
265
#### Unrecognized input type
266

    
267
else
268
install: _always ;
269
uninstall: _always ;
270
endif
(2-2/2)