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

    
127
test2File = $(call test,$(map))
128
test2Db = $(call test,$(import))
129

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

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

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

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

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

    
146
else
147
test: _always ;
148
endif
149

    
150
#####
151

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

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

    
156
ifneq ($(dbFile),)
157

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

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

    
165
####
166

    
167
install: _always db ;
168

    
169
uninstall: _always rm_db ;
170

    
171
####
172

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

    
175
ifeq ($(dbEngineExt),my)
176

    
177
dbEngine := MySQL
178

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

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

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

    
195
###
196

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

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

    
204
####
205

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

    
212
#####
213

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