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.out _always
86
	$(DIFF) $(<:.out=.ref) $<
87

    
88
all += test.out
89

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

    
93
#####
94

    
95
ifdef db
96

    
97
mapEnv := in_engine=$(dbEngine) in_database=$(db)
98

    
99
####
100

    
101
ifeq ($(dbEngine),MySQL)
102

    
103
bienPassword := $(shell cat $(root)/config/bien_password)
104
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
105
mysqlAsRoot := $(call mysqlAs,root)
106
mysqlAsBien := $(call mysqlAs,bien)
107

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

    
113
%.ref: %.ref.sql
114
	$(mysqlAsBien) $(db) <$< >$@
115
.PRECIOUS: %.ref
116

    
117
###
118

    
119
install: _always db ;
120

    
121
uninstall: _always rm_db ;
122

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

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

    
133
###
134

    
135
else
136
$(error dbEngine variable must be one of the following: MySQL, PostgreSQL)
137
endif
138

    
139
####
140

    
141
test.out: $(vegbienMaps) _always
142
	env test=1 n=$(test_n) $(map) $(+_) >$@ 2>&1 || { e=$$?; cat $@; exit $$e;}
143
.PRECIOUS: test.out
144

    
145
#####
146

    
147
else
148
$(error db variable must be set)
149
endif
150

    
151
#####
152

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