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
root := $(selfDir_3d1bc249)..
19
psqlAsBien := $(root)/bin/psql_vegbien
20

    
21
# Config
22
test_n ?= 2
23

    
24
#####
25

    
26
all: _always maps ;
27

    
28
.SUFFIXES:
29

    
30
_always:
31
.PHONY: _always
32

    
33
clean: _always
34
	$(RM) $(all)
35

    
36
remake: _always clean all ;
37

    
38
%.out: %.make _always
39
	./$* >$@
40
.PRECIOUS: %.out
41

    
42
$(root)/%: _always
43
	+$(subMake)
44

    
45
#####
46

    
47
reinstall: _always uninstall install ;
48

    
49
#####
50

    
51
maps: $(vegbienMaps) _always ;
52

    
53
all += $(vegbienMaps)
54

    
55
maps/VegBIEN.%.csv: maps/VegX.%.csv $(root)/mappings/VegX-VegBIEN.%.csv
56
	$(root)/bin/join_sort <$+ >$@
57
.PRECIOUS: maps/VegBIEN.%.csv
58

    
59
#####
60

    
61
import: _always $(addprefix import-,$(tables)) verify ;
62

    
63
log = $*$(if $(n),.n=$(n),).log
64

    
65
import-%: maps/VegBIEN.%.csv _always
66
	-$(import_cmd)
67
import_cmd = (set -x; "time" env commit=1 $(import) $<) \
68
$(if $(n),,>>$(log)) 2>&1$(if $(n),|tee -a $(log))
69

    
70
logs := $(wildcard *.log)
71

    
72
rm_logs: _always
73
	$(RM) $(logs)
74

    
75
#####
76

    
77
ifneq ($(wildcard verify.ref*),)
78

    
79
verify: verify.ref verify.out _always
80
	$(DIFF) $(+_)
81

    
82
all += verify.out
83

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

    
90
else
91
verify: _always ;
92
endif
93

    
94
#####
95

    
96
ifneq ($(wildcard test/),)
97

    
98
testOutputs := test/VegX.xml test/VegBIEN.xml test/import.out
99

    
100
test: _always $(testOutputs) ;
101

    
102
.PRECIOUS: $(testOutputs)
103

    
104
all += $(testOutputs)
105

    
106
define test
107
env test=1 n=$(test_n) $(1) $(+_)$(if $(2),, >$@) 2>&1$(if $(2), |\
108
grep -v -E '^$(2)$$' >$@) || { e=$$?; cat $@; exit $$e;}
109
$(DIFF) $@.ref $@
110
endef
111

    
112
test2File = $(call test,$(map),Processed .* input rows)
113
test2Db = $(call test,$(import))
114

    
115
test/VegX.xml: $(vegxMaps) _always
116
	$(test2File)
117

    
118
test/VegBIEN.xml: $(vegbienMaps) _always
119
	$(test2File)
120

    
121
test/import.out: $(vegbienMaps) _always
122
	$(test2Db)
123

    
124
%-ok: _always
125
	$(CP) $* $*.ref
126

    
127
else
128
test: _always ;
129
endif
130

    
131
#####
132

    
133
# Each input type needs var $(mapEnv) and targets install, uninstall
134

    
135
dbFile := $(firstword $(wildcard src/*.sql))
136

    
137
ifneq ($(dbFile),)
138

    
139
dbFileBasename := $(basename $(notdir $(dbFile)))
140
dbEngineExt := $(subst .,,$(suffix $(dbFileBasename)))
141
db := $(basename $(dbFileBasename))
142

    
143
%.ref: %.ref.sql
144
	$(inputDbAsBien) $(db) <$< >$@
145
.PRECIOUS: %.ref
146

    
147
####
148

    
149
install: _always db ;
150

    
151
uninstall: _always rm_db ;
152

    
153
####
154

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

    
157
ifeq ($(dbEngineExt),my)
158

    
159
dbEngine := MySQL
160

    
161
bienPassword := $(shell cat $(root)/config/bien_password)
162
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
163
mysqlAsRoot := $(call mysqlAs,root)
164
dbAsBien := $(call mysqlAs,bien)
165

    
166
db: $(dbFile) _always
167
	-$(mysqlAsRoot) <$<
168
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
169
# ignore errors in db import so that GRANT will still be run
170

    
171
rm_db: _always
172
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
173
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
174
# ignore errors if grant not defined
175

    
176
###
177

    
178
else
179
$(error The DB filename $(dbFile) must have the form <db_name>.{my|pg}.sql)
180
endif
181

    
182
# Must come after dbEngine is set
183
mapEnv := in_engine=$(dbEngine) in_database=$(db)
184

    
185
####
186

    
187
else
188
mapEnv :=
189
install: _always ;
190
uninstall: _always ;
191
endif
192

    
193
#####
194

    
195
# Must come after mapEnv is set
196
map := $(if $(mapEnv),env $(mapEnv) ,)$(root)/map
197
import := env out_database=vegbien $(map)
(2-2/2)