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
ifdef db
96

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

    
99
%.ref: %.ref.sql
100
	$(inputDbAsBien) $(db) <$< >$@
101
.PRECIOUS: %.ref
102

    
103
####
104

    
105
install: _always db ;
106

    
107
uninstall: _always rm_db ;
108

    
109
####
110

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

    
116
####
117

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

    
122
####
123

    
124
# Each DB engine needs var $(dbAsBien) and targets db, rm_db
125

    
126
ifeq ($(dbEngine),MySQL)
127

    
128
bienPassword := $(shell cat $(root)/config/bien_password)
129
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
130
mysqlAsRoot := $(call mysqlAs,root)
131
dbAsBien := $(call mysqlAs,bien)
132

    
133
db: src.$(db).sql _always
134
	-$(mysqlAsRoot) <$<
135
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
136
# ignore errors in db import so that GRANT will still be run
137

    
138
rm_db: _always
139
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
140
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
141
# ignore errors if grant not defined
142

    
143
####
144

    
145
else
146
$(error dbEngine variable must be one of the following: MySQL, PostgreSQL)
147
endif
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)