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/VegBIEN.xml test/import.out
99

    
100
test: _always $(testOutputs) ;
101

    
102
all += $(testOutputs)
103

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

    
110
test/VegBIEN.xml: $(vegbienMaps) _always
111
	$(call runTest,$(map),Processed .* input rows)
112
.PRECIOUS: test/VegBIEN.xml
113

    
114
test/import.out: $(vegbienMaps) _always
115
	$(call runTest,$(import))
116
.PRECIOUS: test/import.out
117

    
118
%-ok: _always
119
	$(CP) $* $*.ref
120

    
121
else
122
test: _always ;
123
endif
124

    
125
#####
126

    
127
# Each input type needs var $(mapEnv) and targets install, uninstall
128

    
129
dbFile := $(firstword $(wildcard src/*.sql))
130

    
131
ifneq ($(dbFile),)
132

    
133
dbFileBasename := $(basename $(notdir $(dbFile)))
134
dbEngineExt := $(subst .,,$(suffix $(dbFileBasename)))
135
db := $(basename $(dbFileBasename))
136

    
137
%.ref: %.ref.sql
138
	$(inputDbAsBien) $(db) <$< >$@
139
.PRECIOUS: %.ref
140

    
141
####
142

    
143
install: _always db ;
144

    
145
uninstall: _always rm_db ;
146

    
147
####
148

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

    
151
ifeq ($(dbEngineExt),my)
152

    
153
dbEngine := MySQL
154

    
155
bienPassword := $(shell cat $(root)/config/bien_password)
156
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
157
mysqlAsRoot := $(call mysqlAs,root)
158
dbAsBien := $(call mysqlAs,bien)
159

    
160
db: $(dbFile) _always
161
	-$(mysqlAsRoot) <$<
162
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
163
# ignore errors in db import so that GRANT will still be run
164

    
165
rm_db: _always
166
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
167
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
168
# ignore errors if grant not defined
169

    
170
###
171

    
172
else
173
$(error The DB filename $(dbFile) must have the form <db_name>.{my|pg}.sql)
174
endif
175

    
176
# Must come after dbEngine is set
177
mapEnv := in_engine=$(dbEngine) in_database=$(db)
178

    
179
####
180

    
181
else
182
mapEnv :=
183
install: _always ;
184
uninstall: _always ;
185
endif
186

    
187
#####
188

    
189
# Must come after mapEnv is set
190
map := $(if $(mapEnv),env $(mapEnv) ,)$(root)/map
191
import := env out_database=vegbien $(map)
(2-2/2)