Project

General

Profile

1
selfDir_3d1bc249 := $(dir $(lastword $(MAKEFILE_LIST)))
2

    
3
# Make
4
subMake = $(MAKE) $(@F) --directory=$(@D)
5
+_ = $(+:_%=)
6
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
7

    
8
# Commands
9
CP = cp -p
10
DIFF = diff --unified=2
11

    
12
tablesSort := plots organisms stems
13
vegxMaps := $(wildcard $(tablesSort:%=maps/VegX.%.csv))
14
vegxMaps += $(filter-out $(vegxMaps),$(wildcard maps/VegX.*.csv))
15
tables := $(vegxMaps:maps/VegX.%.csv=%)
16
vegbienMaps := $(subst VegX.,VegBIEN.,$(vegxMaps))
17

    
18
+maps = $(filter maps/%,$(+_))
19
+in = $(filter-out maps/%,$(+_))
20

    
21
root := $(selfDir_3d1bc249)..
22
psqlAsBien := $(root)/bin/psql_vegbien
23

    
24
# Config
25
test_n ?= 2
26

    
27
#####
28

    
29
all: _always maps ;
30

    
31
.SUFFIXES:
32

    
33
_always:
34
.PHONY: _always
35

    
36
clean: _always
37
	$(RM) $(all)
38

    
39
remake: _always clean all ;
40

    
41
%.out: %.make _always
42
	./$* >$@
43
.PRECIOUS: %.out
44

    
45
$(root)/%: _always
46
	+$(subMake)
47

    
48
#####
49

    
50
reinstall: _always uninstall install ;
51

    
52
#####
53

    
54
maps: $(vegbienMaps) _always ;
55

    
56
all += $(vegbienMaps)
57

    
58
maps/VegBIEN.%.csv: maps/VegX.%.csv $(root)/mappings/VegX-VegBIEN.%.csv
59
	$(root)/bin/join_sort <$+ >$@
60
.PRECIOUS: maps/VegBIEN.%.csv
61

    
62
#####
63

    
64
import: _always $(addprefix import-,$(tables)) verify ;
65

    
66
log = $*$(if $(n),.n=$(n),).log
67

    
68
import-%: maps/VegBIEN.%.csv _always
69
	-$(import_cmd)
70
# ignore import errors, which are often benign (e.g. invalid date format)
71
import_cmd = (set -x; "time" env commit=1 $(import) $<) \
72
$(if $(n),,>>$(log)) 2>&1$(if $(n),|tee -a $(log))
73

    
74
logs := $(wildcard *.log)
75

    
76
rm_logs: _always
77
	$(RM) $(logs)
78

    
79
#####
80

    
81
ifneq ($(wildcard verify.ref*),)
82

    
83
verify: verify.ref verify.out _always
84
	$(DIFF) $(+_)
85

    
86
all += verify.out
87

    
88
%.out: %.sql _always
89
	$(out_cmd)
90
.PRECIOUS: %.out
91
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off \
92
--pset=null=NULL <$< >$@
93

    
94
else
95
verify: _always ;
96
endif
97

    
98
#####
99

    
100
ifneq ($(wildcard test/),)
101

    
102
tests := test/VegX.xml test/VegBIEN.xml test/VegBIEN.2-step.xml test/import.out
103

    
104
test: _always $(tests) ;
105

    
106
.PRECIOUS: $(tests)
107

    
108
all += $(tests)
109

    
110
testRef = $(1:.2-step.xml=.xml).ref
111

    
112
define test
113
env test=1 n=$(test_n) $(1) $(if $(+in),<$(+in)) $(+maps) $(if $(2),,>$@) 2>&1 \
114
$(if $(2),| grep -v -E '^$(2)$$' >$@) || { e=$$?; cat $@; exit $$e;}
115
$(DIFF) $(call testRef,$@) $@
116
endef
117

    
118
test2File = $(call test,$(map),Processed .* input rows)
119
test2Db = $(call test,$(import))
120

    
121
test/VegX.xml: $(vegxMaps) _always
122
	$(test2File)
123

    
124
test/VegBIEN.xml: $(vegbienMaps) _always
125
	$(test2File)
126

    
127
test/VegBIEN.2-step.xml: test/VegX.xml $(vegbienMaps) _always
128
	$(test2File)
129

    
130
test/import.out: $(vegbienMaps) _always
131
	$(test2Db)
132

    
133
# Accepts a test output: make <test_output_path>-ok
134
%-ok: _always
135
	$(CP) $* $(call testRef,$*)
136

    
137
else
138
test: _always ;
139
endif
140

    
141
#####
142

    
143
# Each input type needs var $(mapEnv) and targets install, uninstall
144

    
145
dbFile := $(firstword $(wildcard src/db.*.sql))
146

    
147
ifneq ($(dbFile),)
148

    
149
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
150
db := $(notdir $(realpath .))
151

    
152
%.ref: %.ref.sql
153
	$(inputDbAsBien) $(db) <$< >$@
154
.PRECIOUS: %.ref
155

    
156
####
157

    
158
install: _always db ;
159

    
160
uninstall: _always rm_db ;
161

    
162
####
163

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

    
166
ifeq ($(dbEngineExt),my)
167

    
168
dbEngine := MySQL
169

    
170
bienPassword := $(shell cat $(root)/config/bien_password)
171
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
172
mysqlAsRoot := $(call mysqlAs,root)
173
dbAsBien := $(call mysqlAs,bien)
174

    
175
db: $(dbFile) _always
176
	echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
177
	-$(mysqlAsRoot) --database=$(db) <$<
178
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
179
# ignore errors in db import so that GRANT will still be run
180

    
181
rm_db: _always
182
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
183
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
184
# ignore errors if grant not defined
185

    
186
###
187

    
188
else
189
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
190
endif
191

    
192
# Must come after dbEngine is set
193
mapEnv := in_engine=$(dbEngine) in_database=$(db)
194

    
195
####
196

    
197
else
198
mapEnv :=
199
install: _always ;
200
uninstall: _always ;
201
endif
202

    
203
#####
204

    
205
# Must come after mapEnv is set
206
map := $(if $(mapEnv),env $(mapEnv) ,)$(root)/map
207
import := env out_database=vegbien $(map)
(2-2/2)