Project

General

Profile

1 256 aaronmk
selfDir_3d1bc249 := $(dir $(lastword $(MAKEFILE_LIST)))
2 250 aaronmk
3 368 aaronmk
# Make
4 415 aaronmk
subMake = $(MAKE) $(@F) --directory=$(@D)
5 404 aaronmk
+_ = $(+:_%=)
6 368 aaronmk
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
7
8
# Commands
9 395 aaronmk
CP = cp -p
10
DIFF = diff --unified=2
11 368 aaronmk
12 415 aaronmk
tablesSort := plots organisms stems
13 502 aaronmk
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 247 aaronmk
18 630 aaronmk
+maps = $(filter maps/%,$(+_))
19
+in = $(filter-out maps/%,$(+_))
20
21 368 aaronmk
root := $(selfDir_3d1bc249)..
22
psqlAsBien := $(root)/bin/psql_vegbien
23 353 aaronmk
24 401 aaronmk
# Config
25
test_n ?= 2
26
27 250 aaronmk
#####
28
29 418 aaronmk
all: _always maps ;
30 247 aaronmk
31
.SUFFIXES:
32
33 383 aaronmk
_always:
34
.PHONY: _always
35 250 aaronmk
36 383 aaronmk
clean: _always
37 256 aaronmk
	$(RM) $(all)
38
39 423 aaronmk
remake: _always clean all ;
40
41 515 aaronmk
%.out: %.make _always
42 368 aaronmk
	./$* >$@
43
.PRECIOUS: %.out
44
45 415 aaronmk
$(root)/%: _always
46
	+$(subMake)
47
48 250 aaronmk
#####
49
50 418 aaronmk
reinstall: _always uninstall install ;
51 264 aaronmk
52
#####
53
54 418 aaronmk
maps: $(vegbienMaps) _always ;
55 368 aaronmk
56
all += $(vegbienMaps)
57
58 502 aaronmk
maps/VegBIEN.%.csv: maps/VegX.%.csv $(root)/mappings/VegX-VegBIEN.%.csv
59 415 aaronmk
	$(root)/bin/join_sort <$+ >$@
60 502 aaronmk
.PRECIOUS: maps/VegBIEN.%.csv
61 264 aaronmk
62 368 aaronmk
#####
63
64 418 aaronmk
import: _always $(addprefix import-,$(tables)) verify ;
65 368 aaronmk
66
log = $*$(if $(n),.n=$(n),).log
67
68 624 aaronmk
import-%: maps/VegBIEN.%.csv _always
69
	-$(import_cmd)
70 628 aaronmk
import_cmd = (set -x; "time" env commit=1 $(import) $<) \
71 624 aaronmk
$(if $(n),,>>$(log)) 2>&1$(if $(n),|tee -a $(log))
72
73 339 aaronmk
logs := $(wildcard *.log)
74
75 383 aaronmk
rm_logs: _always
76 339 aaronmk
	$(RM) $(logs)
77
78 264 aaronmk
#####
79
80 624 aaronmk
ifneq ($(wildcard verify.ref*),)
81
82 514 aaronmk
verify: verify.ref verify.out _always
83 404 aaronmk
	$(DIFF) $(+_)
84 368 aaronmk
85 514 aaronmk
all += verify.out
86
87 383 aaronmk
%.out: %.sql _always
88 429 aaronmk
	$(out_cmd)
89 370 aaronmk
.PRECIOUS: %.out
90 429 aaronmk
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off \
91
--pset=null=NULL <$< >$@
92 369 aaronmk
93 624 aaronmk
else
94
verify: _always ;
95
endif
96
97 368 aaronmk
#####
98
99 624 aaronmk
ifneq ($(wildcard test/),)
100
101 630 aaronmk
tests := test/VegX.xml test/VegBIEN.xml test/VegBIEN.2-step.xml test/import.out
102 368 aaronmk
103 630 aaronmk
test: _always $(tests) ;
104 395 aaronmk
105 630 aaronmk
.PRECIOUS: $(tests)
106 629 aaronmk
107 630 aaronmk
all += $(tests)
108 628 aaronmk
109 630 aaronmk
testRef = $(1:.2-step.xml=.xml).ref
110
111 629 aaronmk
define test
112 630 aaronmk
env test=1 n=$(test_n) $(1) $(if $(+in),<$(+in)) $(+maps) $(if $(2),,>$@) 2>&1 \
113
$(if $(2),| grep -v -E '^$(2)$$' >$@) || { e=$$?; cat $@; exit $$e;}
114
$(DIFF) $(call testRef,$@) $@
115 628 aaronmk
endef
116
117 629 aaronmk
test2File = $(call test,$(map),Processed .* input rows)
118
test2Db = $(call test,$(import))
119
120
test/VegX.xml: $(vegxMaps) _always
121
	$(test2File)
122
123 628 aaronmk
test/VegBIEN.xml: $(vegbienMaps) _always
124 629 aaronmk
	$(test2File)
125 628 aaronmk
126 630 aaronmk
test/VegBIEN.2-step.xml: test/VegX.xml $(vegbienMaps) _always
127
	$(test2File)
128
129 627 aaronmk
test/import.out: $(vegbienMaps) _always
130 629 aaronmk
	$(test2Db)
131 627 aaronmk
132 626 aaronmk
%-ok: _always
133 630 aaronmk
	$(CP) $* $(call testRef,$*)
134 502 aaronmk
135 624 aaronmk
else
136
test: _always ;
137
endif
138
139 368 aaronmk
#####
140
141 624 aaronmk
# Each input type needs var $(mapEnv) and targets install, uninstall
142
143 622 aaronmk
dbFile := $(firstword $(wildcard src/*.sql))
144 256 aaronmk
145 622 aaronmk
ifneq ($(dbFile),)
146 256 aaronmk
147 622 aaronmk
dbFileBasename := $(basename $(notdir $(dbFile)))
148
dbEngineExt := $(subst .,,$(suffix $(dbFileBasename)))
149
db := $(basename $(dbFileBasename))
150
151 621 aaronmk
%.ref: %.ref.sql
152
	$(inputDbAsBien) $(db) <$< >$@
153
.PRECIOUS: %.ref
154
155 368 aaronmk
####
156 256 aaronmk
157 621 aaronmk
install: _always db ;
158 339 aaronmk
159 621 aaronmk
uninstall: _always rm_db ;
160 368 aaronmk
161 621 aaronmk
####
162
163 624 aaronmk
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
164 256 aaronmk
165 622 aaronmk
ifeq ($(dbEngineExt),my)
166 621 aaronmk
167 622 aaronmk
dbEngine := MySQL
168
169 621 aaronmk
bienPassword := $(shell cat $(root)/config/bien_password)
170
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
171
mysqlAsRoot := $(call mysqlAs,root)
172
dbAsBien := $(call mysqlAs,bien)
173
174 622 aaronmk
db: $(dbFile) _always
175 368 aaronmk
	-$(mysqlAsRoot) <$<
176
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
177 255 aaronmk
# ignore errors in db import so that GRANT will still be run
178 250 aaronmk
179 383 aaronmk
rm_db: _always
180 368 aaronmk
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
181
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
182 365 aaronmk
# ignore errors if grant not defined
183 250 aaronmk
184 624 aaronmk
###
185 368 aaronmk
186
else
187 624 aaronmk
$(error The DB filename $(dbFile) must have the form <db_name>.{my|pg}.sql)
188 368 aaronmk
endif
189
190 622 aaronmk
# Must come after dbEngine is set
191
mapEnv := in_engine=$(dbEngine) in_database=$(db)
192
193 624 aaronmk
####
194
195 256 aaronmk
else
196 624 aaronmk
mapEnv :=
197
install: _always ;
198
uninstall: _always ;
199 256 aaronmk
endif
200 368 aaronmk
201
#####
202
203
# Must come after mapEnv is set
204 628 aaronmk
map := $(if $(mapEnv),env $(mapEnv) ,)$(root)/map
205
import := env out_database=vegbien $(map)