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
%-ok: _always
133
	$(CP) $* $(call testRef,$*)
134

    
135
else
136
test: _always ;
137
endif
138

    
139
#####
140

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

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

    
145
ifneq ($(dbFile),)
146

    
147
dbFileBasename := $(basename $(notdir $(dbFile)))
148
dbEngineExt := $(subst .,,$(suffix $(dbFileBasename)))
149
db := $(basename $(dbFileBasename))
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
	-$(mysqlAsRoot) <$<
176
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
177
# ignore errors in db import so that GRANT will still be run
178

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

    
184
###
185

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

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

    
193
####
194

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

    
201
#####
202

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