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:%=map.VegX.%.csv))
14
tables := $(vegxMaps:map.VegX.%.csv=%)
15
vegbienMaps := $(subst .VegX.,.VegBIEN.,$(vegxMaps))
16

    
17
root := $(selfDir_3d1bc249)..
18
psqlAsBien := $(root)/bin/psql_vegbien
19

    
20
# Config
21
test_n ?= 2
22

    
23
#####
24

    
25
all: _always maps ;
26

    
27
.SUFFIXES:
28

    
29
_always:
30
.PHONY: _always
31

    
32
clean: _always
33
	$(RM) $(all)
34

    
35
%.out: % _always
36
	./$* >$@
37
.PRECIOUS: %.out
38

    
39
all += $(wildcard *.out)
40

    
41
$(root)/%: _always
42
	+$(subMake)
43

    
44
#####
45

    
46
reinstall: _always uninstall install ;
47

    
48
#####
49

    
50
maps: $(vegbienMaps) _always ;
51

    
52
all += $(vegbienMaps)
53

    
54
map.VegBIEN.%.csv: map.VegX.%.csv $(root)/mappings/VegX-VegBIEN.%.csv
55
	$(root)/bin/join_sort <$+ >$@
56
.PRECIOUS: map.VegBIEN.%.csv
57

    
58
#####
59

    
60
import: _always $(addprefix import-,$(tables)) verify ;
61

    
62
log = $*$(if $(n),.n=$(n),).log
63

    
64
logs := $(wildcard *.log)
65

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

    
69
#####
70

    
71
verify: _always $(addprefix verify-,$(tables)) ;
72

    
73
verify-%: verify.%.ref verify.%.out _always
74
	$(DIFF) $(+_)
75

    
76
%.out: %.sql _always
77
	$(psqlAsBien) --no-align --field-separator='	' --pset=footer=off <$< >$@
78
.PRECIOUS: %.out
79

    
80
#####
81

    
82
test: _always $(addprefix test-,$(tables)) ;
83

    
84
test-%: test.%.out _always
85
	$(DIFF) $(<:.out=.ref) $<
86

    
87
accept-%: _always
88
	$(CP) $* $(*:.out=.ref)
89

    
90
#####
91

    
92
ifdef db
93

    
94
mapEnv := in_engine=$(dbEngine) in_database=$(db)
95

    
96
####
97

    
98
ifeq ($(dbEngine),MySQL)
99

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

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

    
110
%.ref: %.ref.sql
111
	$(mysqlAsBien) $(db) <$< >$@
112
.PRECIOUS: %.ref
113

    
114
###
115

    
116
install: _always db ;
117

    
118
uninstall: _always rm_db ;
119

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

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

    
130
###
131

    
132
else
133
$(error dbEngine variable must be one of the following: MySQL, PostgreSQL)
134
endif
135

    
136
####
137

    
138
test.%.out: map.VegBIEN.%.csv _always
139
	env test=1 n=$(test_n) $(map) $< >$@ 2>&1 || { e=$$?; cat $@; exit $$e;}
140
.PRECIOUS: test.%.out
141

    
142
#####
143

    
144
else
145
$(error db variable must be set)
146
endif
147

    
148
#####
149

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