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) $<.ref $<
100

    
101
all += test/import.out
102

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

    
107
%-ok: _always
108
	$(CP) $* $*.ref
109

    
110
else
111
test: _always ;
112
endif
113

    
114
#####
115

    
116
# Each input type needs var $(mapEnv) and targets install, uninstall
117

    
118
dbFile := $(firstword $(wildcard src/*.sql))
119

    
120
ifneq ($(dbFile),)
121

    
122
dbFileBasename := $(basename $(notdir $(dbFile)))
123
dbEngineExt := $(subst .,,$(suffix $(dbFileBasename)))
124
db := $(basename $(dbFileBasename))
125

    
126
%.ref: %.ref.sql
127
	$(inputDbAsBien) $(db) <$< >$@
128
.PRECIOUS: %.ref
129

    
130
####
131

    
132
install: _always db ;
133

    
134
uninstall: _always rm_db ;
135

    
136
####
137

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

    
140
ifeq ($(dbEngineExt),my)
141

    
142
dbEngine := MySQL
143

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

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

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

    
159
###
160

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

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

    
168
####
169

    
170
else
171
mapEnv :=
172
install: _always ;
173
uninstall: _always ;
174
endif
175

    
176
#####
177

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