Project

General

Profile

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