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
psqlAsBien := $(root)/bin/psql_vegbien
35

    
36
##### General targets
37

    
38
all: _always maps ;
39

    
40
.SUFFIXES:
41

    
42
_always:
43
.PHONY: _always
44

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

    
48
remake: _always clean all ;
49

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

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

    
57
##### SVN
58

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

    
64
##### Installation
65

    
66
reinstall: _always uninstall install ;
67

    
68
##### Maps
69

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

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

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

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

    
83
maps: $(directMaps) _always ;
84

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

    
87
all += $(autogenMaps)
88

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

    
93
##### Mapping
94

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

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

    
104
##### Import to VegBIEN
105

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

    
108
import: _always import-all verify ;
109

    
110
log = $(@:-all=)$(if $(n),.n=$(n),).$(date).log
111
import = -(set -x; "time" env commit=1 $(map2db)) $(if $(nolog),,\
112
$(if $(n),,>>$(log))) 2>&1$(if $(nolog),,$(if $(n),|tee -a $(log)))
113
# ignore import errors, which are often benign (e.g. invalid date format)
114

    
115
ifneq ($(inputFiles),)
116
import-all: $(addprefix import-,$(tables)) _always ;
117
else
118
import-all: $(directMaps) _always
119
	$(import)
120
endif
121

    
122
import-%: maps/VegBIEN.%.csv _always
123
	$(import)
124
# default:
125
import-%: _always ;
126

    
127
else
128
import: _always ;
129
endif
130

    
131
##### Log files from import
132

    
133
logs := $(wildcard *.log)
134

    
135
rm_logs: _always
136
	$(RM) $(logs)
137

    
138
##### Verification of import
139

    
140
ifneq ($(wildcard verify.ref*),)
141

    
142
verify: verify.ref verify.out _always
143
	$(DIFF) $(+_)
144

    
145
all += verify.out
146

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

    
153
else
154
verify: _always ;
155
endif
156

    
157
##### Testing
158

    
159
ifneq ($(wildcard test/),)
160

    
161
hasOwnRef = $(filter-out %.2-step.xml,$@)
162
testRef = $(1:.2-step.xml=.xml).ref
163

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

    
180
test2File = $(call runTest,$(map))
181

    
182
tests :=
183

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

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

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

    
197
test/import.%.out: maps/VegBIEN.%.csv _always
198
	$(call runTest,$(map2db))
199
tests += test/import.%.out
200

    
201
.PRECIOUS: $(tests)
202

    
203
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
204

    
205
test: _always $(testOutputs) ;
206

    
207
all += $(testOutputs)
208

    
209
# Accepts a test output: make <test_output_path>-ok
210
%-ok: _always
211
	$(CP) $* $(call testRef,$*)
212

    
213
else
214
test: _always ;
215
endif
216

    
217
##### Input-type-specific
218

    
219
# Each input type needs var $(mapEnv) and targets install, uninstall
220

    
221
ifneq ($(dbFile),)
222

    
223
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
224
db := $(notdir $(realpath .))
225

    
226
%.ref: %.ref.sql
227
	$(inputDbAsBien) $(db) <$< >$@
228
.PRECIOUS: %.ref
229

    
230
#### Installation
231

    
232
install: _always db ;
233

    
234
uninstall: _always rm_db ;
235

    
236
#### DB-engine-specific
237

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

    
240
ifeq ($(dbEngineExt),my)
241

    
242
dbEngine := MySQL
243

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

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

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

    
260
### Unrecognized DB engine
261

    
262
else
263
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
264
endif
265

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

    
269
#### Unrecognized input type
270

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