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
hasOwnRef = $(filter-out %.2-step.xml,$@)
130
testRef = $(1:.2-step.xml=.xml).ref
131

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

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

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

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

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

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

    
158
else
159
test: _always ;
160
endif
161

    
162
#####
163

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

    
166
ifneq ($(dbFile),)
167

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

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

    
175
####
176

    
177
install: _always db ;
178

    
179
uninstall: _always rm_db ;
180

    
181
####
182

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

    
185
ifeq ($(dbEngineExt),my)
186

    
187
dbEngine := MySQL
188

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

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

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

    
205
###
206

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

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

    
214
####
215

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

    
222
#####
223

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