Project

General

Profile

1
##### Configuration
2

    
3
exts ?= csv xml
4
test_n ?= 2
5
tablesSort ?= plots organisms stems
6
mainCoreMapTry ?= organisms specimens
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 test/),svn propset svn:ignore $$'*.out\n*.xml' test)
62

    
63
##### Installation
64

    
65
reinstall: _always uninstall install ;
66

    
67
##### Maps
68

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

    
72
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
73
mainCoreMap := $(firstword $(wildcard $(mainCoreMapTry:%=$(coreMap))))
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) ))$(root)/map $(+maps)
101
map2db = env out_database=vegbien $(map)
102

    
103
##### Import to VegBIEN
104

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

    
107
import: _always import-all verify ;
108

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

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

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

    
126
else
127
import: _always ;
128
endif
129

    
130
##### Log files from import
131

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

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

    
137
##### Verification of import
138

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

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

    
144
all += verify.out
145

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

    
152
else
153
verify: _always ;
154
endif
155

    
156
##### Testing
157

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

    
160
tests :=
161

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

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

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

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

    
187
test/VegBIEN.xml: $(directMaps) _always
188
	$(test2File)
189
tests += test/VegBIEN.xml
190

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

    
196
test/import.out: $(directMaps) _always
197
	$(call test,$(map2db))
198
tests += test/import.out
199

    
200
test: _always $(tests) ;
201

    
202
.PRECIOUS: $(tests)
203

    
204
all += $(tests)
205

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

    
210
else
211
test: _always ;
212
endif
213

    
214
##### Input-type-specific
215

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

    
218
ifneq ($(dbFile),)
219

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

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

    
227
#### Installation
228

    
229
install: _always db ;
230

    
231
uninstall: _always rm_db ;
232

    
233
#### DB-engine-specific
234

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

    
237
ifeq ($(dbEngineExt),my)
238

    
239
dbEngine := MySQL
240

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

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

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

    
257
### Unrecognized DB engine
258

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

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

    
266
#### Unrecognized input type
267

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