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) ))$(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)) $(if $(nolog),,\
111
$(if $(n),,>>$(log))) 2>&1$(if $(nolog),,$(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
hasOwnRef = $(filter-out %.2-step.xml,$@)
161
testRef = $(1:.2-step.xml=.xml).ref
162

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

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

    
181
tests :=
182

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

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

    
191
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _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: maps/VegBIEN.%.csv _always
197
	$(call runTest,$(map2db))
198
tests += test/import.%.out
199

    
200
.PRECIOUS: $(tests)
201

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

    
204
test: _always $(testOutputs) ;
205

    
206
all += $(testOutputs)
207

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

    
212
else
213
test: _always ;
214
endif
215

    
216
##### Input-type-specific
217

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

    
220
ifneq ($(dbFile),)
221

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

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

    
229
#### Installation
230

    
231
install: _always db ;
232

    
233
uninstall: _always rm_db ;
234

    
235
#### DB-engine-specific
236

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

    
239
ifeq ($(dbEngineExt),my)
240

    
241
dbEngine := MySQL
242

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

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

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

    
259
### Unrecognized DB engine
260

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

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

    
268
#### Unrecognized input type
269

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