Project

General

Profile

1
##### Configuration
2

    
3
nolog ?=
4
exts ?= csv xml
5
test_n ?= 2
6
tablesSort ?= plots organisms stems
7
mainCoreMapTry ?= organisms specimens
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
esc := '['
24
reset := $(esc)'0m'
25
emph := $(esc)'7m '
26
endEmph := ' '$(reset)
27

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

    
32
# Paths
33
root := $(selfDir_uZPPqC)..
34
mappings := $(root)/mappings
35
psqlAsBien := $(root)/bin/psql_vegbien
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 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
mainCoreMap := $(firstword $(wildcard $(mainCoreMapTry:%=$(coreMap))))
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) ))$(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)) \
112
$(if nolog,,$(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 test
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 test,$(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 $(mainCoreMap) _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 test,$(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)