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
@(set -o pipefail;\
121
{\
122
    (set -x; env test=1 n=$(test_n) $(1) $(if $(+in),<$(+in)) $(+maps)\
123
    $(if $(2),,>$@) 2>&1) $(if $(2),| grep -v -E '^$(2)$$' >$@)\
124
    || { 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
)
130
endef
131

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

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

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

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

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

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

    
151
else
152
test: _always ;
153
endif
154

    
155
#####
156

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

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

    
161
ifneq ($(dbFile),)
162

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

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

    
170
####
171

    
172
install: _always db ;
173

    
174
uninstall: _always rm_db ;
175

    
176
####
177

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

    
180
ifeq ($(dbEngineExt),my)
181

    
182
dbEngine := MySQL
183

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

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

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

    
200
###
201

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

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

    
209
####
210

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

    
217
#####
218

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