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
dbFile := $(firstword $(wildcard src/db.*.sql))
25

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

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

    
32
# Config
33
test_n ?= 2
34

    
35
#####
36

    
37
all: _always maps ;
38

    
39
.SUFFIXES:
40

    
41
_always:
42
.PHONY: _always
43

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

    
47
remake: _always clean all ;
48

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

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

    
56
#####
57

    
58
reinstall: _always uninstall install ;
59

    
60
#####
61

    
62
maps: $(vegbienMaps) _always ;
63

    
64
all += $(vegbienMaps)
65

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

    
70
#####
71

    
72
ifneq ($(wildcard $(dbFile)),)
73

    
74
import: _always import-all verify ;
75

    
76
log = $(@:-all=)$(if $(n),.n=$(n),).log
77
import = -(set -x; "time" env commit=1 $(map2db) $(+_)) \
78
$(if $(n),,>>$(log)) 2>&1$(if $(n),|tee -a $(log))
79
# ignore import errors, which are often benign (e.g. invalid date format)
80

    
81
import-all: $(vegbienMaps) _always
82
	$(import)
83

    
84
import-%: maps/VegBIEN.%.csv _always
85
	$(import)
86

    
87
logs := $(wildcard *.log)
88

    
89
rm_logs: _always
90
	$(RM) $(logs)
91

    
92
else
93
import: _always ;
94
import-%: _always ;
95
rm_logs: _always ;
96
endif
97

    
98
#####
99

    
100
ifneq ($(wildcard verify.ref*),)
101

    
102
verify: verify.ref verify.out _always
103
	$(DIFF) $(+_)
104

    
105
all += verify.out
106

    
107
%.out: %.sql _always
108
	$(out_cmd)
109
.PRECIOUS: %.out
110
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off \
111
--pset=null=NULL <$< >$@
112

    
113
else
114
verify: _always ;
115
endif
116

    
117
#####
118

    
119
ifneq ($(wildcard test/),)
120

    
121
tests := test/VegX.xml test/VegBIEN.xml test/VegBIEN.2-step.xml test/import.out
122

    
123
test: _always $(tests) ;
124

    
125
.PRECIOUS: $(tests)
126

    
127
all += $(tests)
128

    
129
testRef = $(1:.2-step.xml=.xml).ref
130

    
131
define test
132
env test=1 verbose=1 n=$(test_n) $(1) $(if $(+in),<$(+in)) $(+maps) >$@
133
@(set -x; $(DIFF) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
134
echo $(emph)"To accept new test output:"$(endEmph);\
135
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
136
exit $$e;}
137
endef
138

    
139
test/VegX.xml: $(vegxMaps) _always
140
	$(call test,$(map))
141

    
142
test/VegBIEN.xml: $(vegbienMaps) _always
143
	$(call test,$(map))
144

    
145
test/VegBIEN.2-step.xml: test/VegX.xml\
146
$(root)/mappings/VegX-VegBIEN.organisms.csv _always
147
	-$(call test,$(root)/map)
148
# Don't abort tester if only 2-step test fails, as it's often finicky
149

    
150
test/import.out: $(vegbienMaps) _always
151
	$(call test,$(map2db))
152

    
153
# Accepts a test output: make <test_output_path>-ok
154
%-ok: _always
155
	$(CP) $* $(call testRef,$*)
156

    
157
else
158
test: _always ;
159
endif
160

    
161
#####
162

    
163
# Each input type needs var $(mapEnv) and targets install, uninstall
164

    
165
ifneq ($(dbFile),)
166

    
167
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
168
db := $(notdir $(realpath .))
169

    
170
%.ref: %.ref.sql
171
	$(inputDbAsBien) $(db) <$< >$@
172
.PRECIOUS: %.ref
173

    
174
####
175

    
176
install: _always db ;
177

    
178
uninstall: _always rm_db ;
179

    
180
####
181

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

    
184
ifeq ($(dbEngineExt),my)
185

    
186
dbEngine := MySQL
187

    
188
bienPassword := $(shell cat $(root)/config/bien_password)
189
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
190
mysqlAsRoot := $(call mysqlAs,root)
191
dbAsBien := $(call mysqlAs,bien)
192

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

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

    
204
###
205

    
206
else
207
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
208
endif
209

    
210
# Must come after dbEngine is set
211
mapEnv := in_engine=$(dbEngine) in_database=$(db)
212

    
213
####
214

    
215
else
216
mapEnv :=
217
install: _always ;
218
uninstall: _always ;
219
endif
220

    
221
#####
222

    
223
# Must come after mapEnv is set
224
map := $(if $(mapEnv),env $(mapEnv) ,)$(root)/map
225
map2db := env out_database=vegbien $(map)
(2-2/2)