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
tests :=
162

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

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

    
182
test2File = $(call test,$(map))
183

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

    
188
test/VegBIEN.xml: $(directMaps) _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: $(directMaps) _always
198
	$(call test,$(map2db))
199
tests += test/import.out
200

    
201
test: _always $(tests) ;
202

    
203
.PRECIOUS: $(tests)
204

    
205
all += $(tests)
206

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

    
211
else
212
test: _always ;
213
endif
214

    
215
##### Input-type-specific
216

    
217
# Each input type needs var $(mapEnv) and targets install, uninstall
218

    
219
ifneq ($(dbFile),)
220

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

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

    
228
#### Installation
229

    
230
install: _always db ;
231

    
232
uninstall: _always rm_db ;
233

    
234
#### DB-engine-specific
235

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

    
238
ifeq ($(dbEngineExt),my)
239

    
240
dbEngine := MySQL
241

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

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

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

    
258
### Unrecognized DB engine
259

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

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

    
267
#### Unrecognized input type
268

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