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: % _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: _always $(addprefix verify-,$(tables)) ;
73

    
74
verify-%: verify/%.ref verify/%.out _always
75
	$(DIFF) $(+_)
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
all += $(wildcard verify/*.out)
84

    
85
#####
86

    
87
test: _always $(addprefix test-,$(tables)) ;
88

    
89
test-%: test/%.out _always
90
	$(DIFF) $(<:.out=.ref) $<
91

    
92
accept-%: _always
93
	$(CP) $* $(*:.out=.ref)
94

    
95
all += $(wildcard test/*.out)
96

    
97
#####
98

    
99
ifdef db
100

    
101
mapEnv := in_engine=$(dbEngine) in_database=$(db)
102

    
103
####
104

    
105
ifeq ($(dbEngine),MySQL)
106

    
107
bienPassword := $(shell cat $(root)/config/bien_password)
108
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
109
mysqlAsRoot := $(call mysqlAs,root)
110
mysqlAsBien := $(call mysqlAs,bien)
111

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

    
117
%.ref: %.ref.sql
118
	$(mysqlAsBien) $(db) <$< >$@
119
.PRECIOUS: %.ref
120

    
121
###
122

    
123
install: _always db ;
124

    
125
uninstall: _always rm_db ;
126

    
127
db: src.$(db).sql _always
128
	-$(mysqlAsRoot) <$<
129
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
130
# ignore errors in db import so that GRANT will still be run
131

    
132
rm_db: _always
133
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
134
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
135
# ignore errors if grant not defined
136

    
137
###
138

    
139
else
140
$(error dbEngine variable must be one of the following: MySQL, PostgreSQL)
141
endif
142

    
143
####
144

    
145
test/%.out: maps/VegBIEN.%.csv _always
146
	env test=1 n=$(test_n) $(map) $< >$@ 2>&1 || { e=$$?; cat $@; exit $$e;}
147
.PRECIOUS: test/%.out
148

    
149
#####
150

    
151
else
152
$(error db variable must be set)
153
endif
154

    
155
#####
156

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