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
logs := $(wildcard *.log)
66

    
67
rm_logs: _always
68
	$(RM) $(logs)
69

    
70
#####
71

    
72
verify: verify.ref verify.out _always
73
	$(DIFF) $(+_)
74

    
75
all += verify.out
76

    
77
%.out: %.sql _always
78
	$(out_cmd)
79
.PRECIOUS: %.out
80
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off \
81
--pset=null=NULL <$< >$@
82

    
83
#####
84

    
85
test: test/import.out _always
86
	$(DIFF) $(<:.out=.ref) $<
87

    
88
all += test/import.out
89

    
90
accept: _always
91
	$(CP) test/import.out test/import.ref
92

    
93
#####
94

    
95
dbFile := $(firstword $(wildcard src/*.sql))
96

    
97
ifneq ($(dbFile),)
98

    
99
dbFileBasename := $(basename $(notdir $(dbFile)))
100
dbEngineExt := $(subst .,,$(suffix $(dbFileBasename)))
101
db := $(basename $(dbFileBasename))
102

    
103
%.ref: %.ref.sql
104
	$(inputDbAsBien) $(db) <$< >$@
105
.PRECIOUS: %.ref
106

    
107
####
108

    
109
install: _always db ;
110

    
111
uninstall: _always rm_db ;
112

    
113
####
114

    
115
import-%: maps/VegBIEN.%.csv _always
116
	-$(import_cmd)
117
import_cmd = (set -x; "time" env commit=1 $(map) $<) \
118
$(if $(n),,>>$(log)) 2>&1$(if $(n),|tee -a $(log))
119

    
120
####
121

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

    
126
####
127

    
128
# Each DB engine needs var $(dbAsBien) and targets db, rm_db
129

    
130
ifeq ($(dbEngineExt),my)
131

    
132
dbEngine := MySQL
133

    
134
bienPassword := $(shell cat $(root)/config/bien_password)
135
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
136
mysqlAsRoot := $(call mysqlAs,root)
137
dbAsBien := $(call mysqlAs,bien)
138

    
139
db: $(dbFile) _always
140
	-$(mysqlAsRoot) <$<
141
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
142
# ignore errors in db import so that GRANT will still be run
143

    
144
rm_db: _always
145
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
146
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
147
# ignore errors if grant not defined
148

    
149
####
150

    
151
else
152
$(error The DB engine extension "$(dbEngineExt)" for $(dbFile) must be one of\
153
the following: my, pg)
154
endif
155

    
156
#####
157

    
158
# Must come after dbEngine is set
159
mapEnv := in_engine=$(dbEngine) in_database=$(db)
160

    
161
else
162
$(error The src subdir must contain a file <db_name>.{my|pg}.sql)
163
endif
164

    
165
#####
166

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