Project

General

Profile

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

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

    
9
# Terminal
10
esc := '['
11
reset := $(esc)'0m'
12
emph := $(esc)'7m '
13
endEmph := ' '$(reset)
14

    
15
# Commands
16
CP = cp -p
17
DIFF = diff --unified=2
18

    
19
tablesSort := plots organisms stems
20
vegxMaps := $(wildcard $(tablesSort:%=maps/VegX.%.csv))
21
vegxMaps += $(filter-out $(vegxMaps),$(wildcard maps/VegX.*.csv))
22
tables := $(vegxMaps:maps/VegX.%.csv=%)
23
vegbienMaps := $(subst VegX.,VegBIEN.,$(vegxMaps))
24

    
25
+maps = $(filter maps/%,$(+_))
26
+in = $(filter-out maps/%,$(+_))
27

    
28
root := $(selfDir_uZPPqC)..
29
psqlAsBien := $(root)/bin/psql_vegbien
30

    
31
# Config
32
test_n ?= 2
33

    
34
#####
35

    
36
all: _always maps ;
37

    
38
.SUFFIXES:
39

    
40
_always:
41
.PHONY: _always
42

    
43
clean: _always
44
	$(RM) $(all)
45

    
46
remake: _always clean all ;
47

    
48
%.out: %.make _always
49
	./$* >$@
50
.PRECIOUS: %.out
51

    
52
$(root)/%: _always
53
	+$(subMake)
54

    
55
#####
56

    
57
reinstall: _always uninstall install ;
58

    
59
#####
60

    
61
maps: $(vegbienMaps) _always ;
62

    
63
all += $(vegbienMaps)
64

    
65
maps/VegBIEN.%.csv: maps/VegX.%.csv $(root)/mappings/VegX-VegBIEN.%.csv
66
	$(root)/bin/join_sort <$+ >$@
67
.PRECIOUS: maps/VegBIEN.%.csv
68

    
69
#####
70

    
71
import: _always $(addprefix import-,$(tables)) verify ;
72

    
73
log = $*$(if $(n),.n=$(n),).log
74

    
75
import-%: maps/VegBIEN.%.csv _always
76
	-$(import_cmd)
77
# ignore import errors, which are often benign (e.g. invalid date format)
78
import_cmd = (set -x; "time" env commit=1 $(import) $<) \
79
$(if $(n),,>>$(log)) 2>&1$(if $(n),|tee -a $(log))
80

    
81
logs := $(wildcard *.log)
82

    
83
rm_logs: _always
84
	$(RM) $(logs)
85

    
86
#####
87

    
88
ifneq ($(wildcard verify.ref*),)
89

    
90
verify: verify.ref verify.out _always
91
	$(DIFF) $(+_)
92

    
93
all += verify.out
94

    
95
%.out: %.sql _always
96
	$(out_cmd)
97
.PRECIOUS: %.out
98
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off \
99
--pset=null=NULL <$< >$@
100

    
101
else
102
verify: _always ;
103
endif
104

    
105
#####
106

    
107
ifneq ($(wildcard test/),)
108

    
109
tests := test/VegX.xml test/VegBIEN.xml test/VegBIEN.2-step.xml test/import.out
110

    
111
test: _always $(tests) ;
112

    
113
.PRECIOUS: $(tests)
114

    
115
all += $(tests)
116

    
117
testRef = $(1:.2-step.xml=.xml).ref
118

    
119
define test
120
env test=1 verbose=1 n=$(test_n) $(1) $(if $(+in),<$(+in)) $(+maps) >$@
121
@(set -x; $(DIFF) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
122
echo $(emph)"To accept new test output:"$(endEmph);\
123
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
124
exit $$e;}
125
endef
126

    
127
test/VegX.xml: $(vegxMaps) _always
128
	$(call test,$(map))
129

    
130
test/VegBIEN.xml: $(vegbienMaps) _always
131
	$(call test,$(map))
132

    
133
test/VegBIEN.2-step.xml: test/VegX.xml\
134
$(root)/mappings/VegX-VegBIEN.organisms.csv _always
135
	$(call test,$(root)/map)
136

    
137
test/import.out: $(vegbienMaps) _always
138
	$(call test,$(import))
139

    
140
# Accepts a test output: make <test_output_path>-ok
141
%-ok: _always
142
	$(CP) $* $(call testRef,$*)
143

    
144
else
145
test: _always ;
146
endif
147

    
148
#####
149

    
150
# Each input type needs var $(mapEnv) and targets install, uninstall
151

    
152
dbFile := $(firstword $(wildcard src/db.*.sql))
153

    
154
ifneq ($(dbFile),)
155

    
156
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
157
db := $(notdir $(realpath .))
158

    
159
%.ref: %.ref.sql
160
	$(inputDbAsBien) $(db) <$< >$@
161
.PRECIOUS: %.ref
162

    
163
####
164

    
165
install: _always db ;
166

    
167
uninstall: _always rm_db ;
168

    
169
####
170

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

    
173
ifeq ($(dbEngineExt),my)
174

    
175
dbEngine := MySQL
176

    
177
bienPassword := $(shell cat $(root)/config/bien_password)
178
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
179
mysqlAsRoot := $(call mysqlAs,root)
180
dbAsBien := $(call mysqlAs,bien)
181

    
182
db: $(dbFile) _always
183
	echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
184
	-$(mysqlAsRoot) --database=$(db) <$<
185
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
186
# ignore errors in db import so that GRANT will still be run
187

    
188
rm_db: _always
189
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
190
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
191
# ignore errors if grant not defined
192

    
193
###
194

    
195
else
196
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
197
endif
198

    
199
# Must come after dbEngine is set
200
mapEnv := in_engine=$(dbEngine) in_database=$(db)
201

    
202
####
203

    
204
else
205
mapEnv :=
206
install: _always ;
207
uninstall: _always ;
208
endif
209

    
210
#####
211

    
212
# Must come after mapEnv is set
213
map := $(if $(mapEnv),env $(mapEnv) ,)$(root)/map
214
import := env out_database=vegbien $(map)
(2-2/2)