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

    
128
test2File = $(call test,$(map),Processed .* input rows)
129
test2Db = $(call test,$(import))
130

    
131
test/VegX.xml: $(vegxMaps) _always
132
	$(test2File)
133

    
134
test/VegBIEN.xml: $(vegbienMaps) _always
135
	$(test2File)
136

    
137
test/VegBIEN.2-step.xml: test/VegX.xml $(vegbienMaps) _always
138
	$(test2File)
139

    
140
test/import.out: $(vegbienMaps) _always
141
	$(test2Db)
142

    
143
# Accepts a test output: make <test_output_path>-ok
144
%-ok: _always
145
	$(CP) $* $(call testRef,$*)
146

    
147
else
148
test: _always ;
149
endif
150

    
151
#####
152

    
153
# Each input type needs var $(mapEnv) and targets install, uninstall
154

    
155
dbFile := $(firstword $(wildcard src/db.*.sql))
156

    
157
ifneq ($(dbFile),)
158

    
159
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
160
db := $(notdir $(realpath .))
161

    
162
%.ref: %.ref.sql
163
	$(inputDbAsBien) $(db) <$< >$@
164
.PRECIOUS: %.ref
165

    
166
####
167

    
168
install: _always db ;
169

    
170
uninstall: _always rm_db ;
171

    
172
####
173

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

    
176
ifeq ($(dbEngineExt),my)
177

    
178
dbEngine := MySQL
179

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

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

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

    
196
###
197

    
198
else
199
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
200
endif
201

    
202
# Must come after dbEngine is set
203
mapEnv := in_engine=$(dbEngine) in_database=$(db)
204

    
205
####
206

    
207
else
208
mapEnv :=
209
install: _always ;
210
uninstall: _always ;
211
endif
212

    
213
#####
214

    
215
# Must come after mapEnv is set
216
map := $(if $(mapEnv),env $(mapEnv) ,)$(root)/map
217
import := env out_database=vegbien $(map)
(2-2/2)