Project

General

Profile

1
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
2

    
3
# Make
4
SHELL := /bin/bash
5
subMake = $(MAKE) $(@F) --directory=$(@D)
6
+_ = $(+:_%=)
7
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
8

    
9
# Terminal
10
esc := '['
11
reset := $(esc)'0m'
12
emph := $(esc)'7m '
13
endEmph := ' '$(reset)
14

    
15
# Commands
16
CP = cp -p
17
DIFF = diff --unified=2
18

    
19
tablesSort := plots organisms stems
20
vegxMaps := $(wildcard $(tablesSort:%=maps/VegX.%.csv))
21
vegxMaps += $(filter-out $(vegxMaps),$(wildcard maps/VegX.*.csv))
22
tables := $(vegxMaps:maps/VegX.%.csv=%)
23
vegbienMaps := $(subst VegX.,VegBIEN.,$(vegxMaps))
24

    
25
+maps = $(filter maps/%,$(+_))
26
+in = $(filter-out maps/%,$(+_))
27

    
28
root := $(selfDir_uZPPqC)..
29
psqlAsBien := $(root)/bin/psql_vegbien
30

    
31
# Config
32
test_n ?= 2
33

    
34
#####
35

    
36
all: _always maps ;
37

    
38
.SUFFIXES:
39

    
40
_always:
41
.PHONY: _always
42

    
43
clean: _always
44
	$(RM) $(all)
45

    
46
remake: _always clean all ;
47

    
48
%.out: %.make _always
49
	./$* >$@
50
.PRECIOUS: %.out
51

    
52
$(root)/%: _always
53
	+$(subMake)
54

    
55
#####
56

    
57
reinstall: _always uninstall install ;
58

    
59
#####
60

    
61
maps: $(vegbienMaps) _always ;
62

    
63
all += $(vegbienMaps)
64

    
65
maps/VegBIEN.%.csv: maps/VegX.%.csv $(root)/mappings/VegX-VegBIEN.%.csv
66
	$(root)/bin/join_sort <$+ >$@
67
.PRECIOUS: maps/VegBIEN.%.csv
68

    
69
#####
70

    
71
import: _always $(addprefix import-,$(tables)) verify ;
72

    
73
log = $*$(if $(n),.n=$(n),).log
74

    
75
import-%: maps/VegBIEN.%.csv _always
76
	-$(import_cmd)
77
# ignore import errors, which are often benign (e.g. invalid date format)
78
import_cmd = (set -x; "time" env commit=1 $(import) $<) \
79
$(if $(n),,>>$(log)) 2>&1$(if $(n),|tee -a $(log))
80

    
81
logs := $(wildcard *.log)
82

    
83
rm_logs: _always
84
	$(RM) $(logs)
85

    
86
#####
87

    
88
ifneq ($(wildcard verify.ref*),)
89

    
90
verify: verify.ref verify.out _always
91
	$(DIFF) $(+_)
92

    
93
all += verify.out
94

    
95
%.out: %.sql _always
96
	$(out_cmd)
97
.PRECIOUS: %.out
98
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off \
99
--pset=null=NULL <$< >$@
100

    
101
else
102
verify: _always ;
103
endif
104

    
105
#####
106

    
107
ifneq ($(wildcard test/),)
108

    
109
tests := test/VegX.xml test/VegBIEN.xml test/VegBIEN.2-step.xml test/import.out
110

    
111
test: _always $(tests) ;
112

    
113
.PRECIOUS: $(tests)
114

    
115
all += $(tests)
116

    
117
testRef = $(1:.2-step.xml=.xml).ref
118

    
119
define test
120
@{\
121
(set -o pipefail;\
122
(set -x; env test=1 n=$(test_n) $(1) $(if $(+in),<$(+in))\
123
$(+maps) $(if $(2),,>$@) 2>&1)\
124
$(if $(2),| grep -v -E '^$(2)$$' >$@) || { e=$$?; cat $@; exit $$e;})\
125
&& $(DIFF) $(call testRef,$@) $@;\
126
} || { e=$$?; echo $(emph)"To accept new test output:"$(endEmph);\
127
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
128
exit $$e;}
129
endef
130

    
131
test2File = $(call test,$(map),Processed .* input rows)
132
test2Db = $(call test,$(import))
133

    
134
test/VegX.xml: $(vegxMaps) _always
135
	$(test2File)
136

    
137
test/VegBIEN.xml: $(vegbienMaps) _always
138
	$(test2File)
139

    
140
test/VegBIEN.2-step.xml: test/VegX.xml $(vegbienMaps) _always
141
	$(test2File)
142

    
143
test/import.out: $(vegbienMaps) _always
144
	$(test2Db)
145

    
146
# Accepts a test output: make <test_output_path>-ok
147
%-ok: _always
148
	$(CP) $* $(call testRef,$*)
149

    
150
else
151
test: _always ;
152
endif
153

    
154
#####
155

    
156
# Each input type needs var $(mapEnv) and targets install, uninstall
157

    
158
dbFile := $(firstword $(wildcard src/db.*.sql))
159

    
160
ifneq ($(dbFile),)
161

    
162
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
163
db := $(notdir $(realpath .))
164

    
165
%.ref: %.ref.sql
166
	$(inputDbAsBien) $(db) <$< >$@
167
.PRECIOUS: %.ref
168

    
169
####
170

    
171
install: _always db ;
172

    
173
uninstall: _always rm_db ;
174

    
175
####
176

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

    
179
ifeq ($(dbEngineExt),my)
180

    
181
dbEngine := MySQL
182

    
183
bienPassword := $(shell cat $(root)/config/bien_password)
184
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
185
mysqlAsRoot := $(call mysqlAs,root)
186
dbAsBien := $(call mysqlAs,bien)
187

    
188
db: $(dbFile) _always
189
	echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
190
	-$(mysqlAsRoot) --database=$(db) <$<
191
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
192
# ignore errors in db import so that GRANT will still be run
193

    
194
rm_db: _always
195
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
196
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
197
# ignore errors if grant not defined
198

    
199
###
200

    
201
else
202
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
203
endif
204

    
205
# Must come after dbEngine is set
206
mapEnv := in_engine=$(dbEngine) in_database=$(db)
207

    
208
####
209

    
210
else
211
mapEnv :=
212
install: _always ;
213
uninstall: _always ;
214
endif
215

    
216
#####
217

    
218
# Must come after mapEnv is set
219
map := $(if $(mapEnv),env $(mapEnv) ,)$(root)/map
220
import := env out_database=vegbien $(map)
(2-2/2)