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
import_cmd = (set -x; "time" env commit=1 $(import) $<) \
71
$(if $(n),,>>$(log)) 2>&1$(if $(n),|tee -a $(log))
72

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

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

    
78
#####
79

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

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

    
85
all += verify.out
86

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

    
93
else
94
verify: _always ;
95
endif
96

    
97
#####
98

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

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

    
103
test: _always $(tests) ;
104

    
105
.PRECIOUS: $(tests)
106

    
107
all += $(tests)
108

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

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

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

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

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

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

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

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

    
136
else
137
test: _always ;
138
endif
139

    
140
#####
141

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

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

    
146
ifneq ($(dbFile),)
147

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

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

    
155
####
156

    
157
install: _always db ;
158

    
159
uninstall: _always rm_db ;
160

    
161
####
162

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

    
165
ifeq ($(dbEngineExt),my)
166

    
167
dbEngine := MySQL
168

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

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

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

    
185
###
186

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

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

    
194
####
195

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

    
202
#####
203

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