Project

General

Profile

1
##### Configuration
2

    
3
nolog ?=
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 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

    
74
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
75
viaMaps += $(filter-out $(viaMaps),$(wildcard maps/$(via).*.csv))
76

    
77
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
78
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
79
$(wildcard maps/VegBIEN.*.csv))
80
tables := $(directMaps:maps/VegBIEN.%.csv=%)
81

    
82
maps: $(directMaps) _always ;
83

    
84
.PRECIOUS: $(allViaMaps) $(directMaps)
85

    
86
all += $(autogenMaps)
87

    
88
maps/VegBIEN.%.csv: maps/$(via).%.csv $(coreMap)
89
	$(root)/bin/join_sort <$+ >$@
90
.PRECIOUS: maps/VegBIEN.%.csv $(coreMap)
91

    
92
##### Mapping
93

    
94
dbFile := $(firstword $(wildcard src/db.*.sql))
95
inputFiles := $(wildcard src/*.csv src/*.xml)
96

    
97
+maps = $(filter maps/% $(mappings)/%,$(+_))
98
<in = $(firstword $(filter-out $(+maps),$(+_)) $(wildcard $(exts:%=src/*.$*.%)))
99
map = $(if $(<in),<$(<in) ,$(if $(mapEnv),env $(mapEnv) ))$(root)/map $(+maps)
100
map2db = env out_database=vegbien $(map)
101

    
102
##### Import to VegBIEN
103

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

    
106
import: _always import-all verify ;
107

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

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

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

    
125
else
126
import: _always ;
127
endif
128

    
129
##### Log files from import
130

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

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

    
136
##### Verification of import
137

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

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

    
143
all += verify.out
144

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

    
151
else
152
verify: _always ;
153
endif
154

    
155
##### Testing
156

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

    
159
hasOwnRef = $(filter-out %.2-step.xml,$@)
160
testRef = $(1:.2-step.xml=.xml).ref
161

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

    
178
test2File = $(call test,$(map))
179

    
180
tests :=
181

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

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

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

    
195
test/import.%.out: maps/VegBIEN.%.csv _always
196
	$(call test,$(map2db))
197
tests += test/import.%.out
198

    
199
.PRECIOUS: $(tests)
200

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

    
203
test: _always $(testOutputs) ;
204

    
205
all += $(testOutputs)
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)