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 634 aaronmk
# Accepts a test output: make <test_output_path>-ok
133 626 aaronmk
%-ok: _always
134 630 aaronmk
	$(CP) $* $(call testRef,$*)
135 502 aaronmk
136 624 aaronmk
else
137
test: _always ;
138
endif
139
140 368 aaronmk
#####
141
142 624 aaronmk
# Each input type needs var $(mapEnv) and targets install, uninstall
143
144 635 aaronmk
dbFile := $(firstword $(wildcard src/db.*.sql))
145 256 aaronmk
146 622 aaronmk
ifneq ($(dbFile),)
147 256 aaronmk
148 635 aaronmk
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
149
db := $(notdir $(realpath .))
150 622 aaronmk
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 635 aaronmk
	echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
176
	-$(mysqlAsRoot) --database=$(db) <$<
177 368 aaronmk
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
178 255 aaronmk
# ignore errors in db import so that GRANT will still be run
179 250 aaronmk
180 383 aaronmk
rm_db: _always
181 368 aaronmk
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
182
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
183 365 aaronmk
# ignore errors if grant not defined
184 250 aaronmk
185 624 aaronmk
###
186 368 aaronmk
187
else
188 635 aaronmk
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
189 368 aaronmk
endif
190
191 622 aaronmk
# Must come after dbEngine is set
192
mapEnv := in_engine=$(dbEngine) in_database=$(db)
193
194 624 aaronmk
####
195
196 256 aaronmk
else
197 624 aaronmk
mapEnv :=
198
install: _always ;
199
uninstall: _always ;
200 256 aaronmk
endif
201 368 aaronmk
202
#####
203
204
# Must come after mapEnv is set
205 628 aaronmk
map := $(if $(mapEnv),env $(mapEnv) ,)$(root)/map
206
import := env out_database=vegbien $(map)