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 $(map) $<) \
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
test: test/import.out _always
99
	$(DIFF) $(<:.out=.ref) $<
100

    
101
all += test/import.out
102

    
103
accept: _always
104
	$(CP) test/import.out test/import.ref
105

    
106
else
107
test: _always ;
108
endif
109

    
110
#####
111

    
112
# Each input type needs var $(mapEnv) and targets install, uninstall
113

    
114
dbFile := $(firstword $(wildcard src/*.sql))
115

    
116
ifneq ($(dbFile),)
117

    
118
dbFileBasename := $(basename $(notdir $(dbFile)))
119
dbEngineExt := $(subst .,,$(suffix $(dbFileBasename)))
120
db := $(basename $(dbFileBasename))
121

    
122
%.ref: %.ref.sql
123
	$(inputDbAsBien) $(db) <$< >$@
124
.PRECIOUS: %.ref
125

    
126
####
127

    
128
install: _always db ;
129

    
130
uninstall: _always rm_db ;
131

    
132
####
133

    
134
test/import.out: $(vegbienMaps) _always
135
	env test=1 n=$(test_n) $(map) $(+_) >$@ 2>&1 || { e=$$?; cat $@; exit $$e;}
136
.PRECIOUS: test/import.out
137

    
138
####
139

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

    
142
ifeq ($(dbEngineExt),my)
143

    
144
dbEngine := MySQL
145

    
146
bienPassword := $(shell cat $(root)/config/bien_password)
147
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
148
mysqlAsRoot := $(call mysqlAs,root)
149
dbAsBien := $(call mysqlAs,bien)
150

    
151
db: $(dbFile) _always
152
	-$(mysqlAsRoot) <$<
153
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
154
# ignore errors in db import so that GRANT will still be run
155

    
156
rm_db: _always
157
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
158
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
159
# ignore errors if grant not defined
160

    
161
###
162

    
163
else
164
$(error The DB filename $(dbFile) must have the form <db_name>.{my|pg}.sql)
165
endif
166

    
167
# Must come after dbEngine is set
168
mapEnv := in_engine=$(dbEngine) in_database=$(db)
169

    
170
####
171

    
172
else
173
mapEnv :=
174
install: _always ;
175
uninstall: _always ;
176
endif
177

    
178
#####
179

    
180
# Must come after mapEnv is set
181
map := env $(mapEnv) out_database=vegbien $(root)/map
(2-2/2)