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 verbose=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
import-all: $(addprefix import-,$(tables)) _always ;
116

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

    
122
else
123
import: _always ;
124
endif
125

    
126
##### Log files from import
127

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

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

    
133
##### Verification of import
134

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

    
137
verify: verify.ref verify.out _always
138
	$(DIFF) $(+_)
139

    
140
all += verify.out
141

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

    
148
else
149
verify: _always ;
150
endif
151

    
152
##### Testing
153

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

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

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

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

    
177
tests :=
178

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

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

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

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

    
196
.PRECIOUS: $(tests)
197

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

    
200
test: _always $(testOutputs) ;
201

    
202
all += $(testOutputs)
203

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

    
208
else
209
test: _always ;
210
endif
211

    
212
##### Input-type-specific
213

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

    
216
ifneq ($(dbFile),)
217

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

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

    
225
#### Installation
226

    
227
install: _always db ;
228

    
229
uninstall: _always rm_db ;
230

    
231
#### DB-engine-specific
232

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

    
235
ifeq ($(dbEngineExt),my)
236

    
237
dbEngine := MySQL
238

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

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

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

    
255
### Unrecognized DB engine
256

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

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

    
264
#### Unrecognized input type
265

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