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 648 aaronmk
@{\
121
(set -o pipefail;\
122
(set -x; env test=1 n=$(test_n) $(1) $(if $(+in),<$(+in))\
123
$(+maps) $(if $(2),,>$@) 2>&1)\
124
$(if $(2),| grep -v -E '^$(2)$$' >$@) || { e=$$?; cat $@; exit $$e;})\
125
&& $(DIFF) $(call testRef,$@) $@;\
126
} || { e=$$?; echo $(emph)"To accept new test output:"$(endEmph);\
127 640 aaronmk
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
128
exit $$e;}
129 628 aaronmk
endef
130
131 629 aaronmk
test2File = $(call test,$(map),Processed .* input rows)
132
test2Db = $(call test,$(import))
133
134
test/VegX.xml: $(vegxMaps) _always
135
	$(test2File)
136
137 628 aaronmk
test/VegBIEN.xml: $(vegbienMaps) _always
138 629 aaronmk
	$(test2File)
139 628 aaronmk
140 630 aaronmk
test/VegBIEN.2-step.xml: test/VegX.xml $(vegbienMaps) _always
141
	$(test2File)
142
143 627 aaronmk
test/import.out: $(vegbienMaps) _always
144 629 aaronmk
	$(test2Db)
145 627 aaronmk
146 634 aaronmk
# Accepts a test output: make <test_output_path>-ok
147 626 aaronmk
%-ok: _always
148 630 aaronmk
	$(CP) $* $(call testRef,$*)
149 502 aaronmk
150 624 aaronmk
else
151
test: _always ;
152
endif
153
154 368 aaronmk
#####
155
156 624 aaronmk
# Each input type needs var $(mapEnv) and targets install, uninstall
157
158 635 aaronmk
dbFile := $(firstword $(wildcard src/db.*.sql))
159 256 aaronmk
160 622 aaronmk
ifneq ($(dbFile),)
161 256 aaronmk
162 635 aaronmk
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
163
db := $(notdir $(realpath .))
164 622 aaronmk
165 621 aaronmk
%.ref: %.ref.sql
166
	$(inputDbAsBien) $(db) <$< >$@
167
.PRECIOUS: %.ref
168
169 368 aaronmk
####
170 256 aaronmk
171 621 aaronmk
install: _always db ;
172 339 aaronmk
173 621 aaronmk
uninstall: _always rm_db ;
174 368 aaronmk
175 621 aaronmk
####
176
177 624 aaronmk
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
178 256 aaronmk
179 622 aaronmk
ifeq ($(dbEngineExt),my)
180 621 aaronmk
181 622 aaronmk
dbEngine := MySQL
182
183 621 aaronmk
bienPassword := $(shell cat $(root)/config/bien_password)
184
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
185
mysqlAsRoot := $(call mysqlAs,root)
186
dbAsBien := $(call mysqlAs,bien)
187
188 622 aaronmk
db: $(dbFile) _always
189 635 aaronmk
	echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
190
	-$(mysqlAsRoot) --database=$(db) <$<
191 368 aaronmk
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
192 255 aaronmk
# ignore errors in db import so that GRANT will still be run
193 250 aaronmk
194 383 aaronmk
rm_db: _always
195 368 aaronmk
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
196
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
197 365 aaronmk
# ignore errors if grant not defined
198 250 aaronmk
199 624 aaronmk
###
200 368 aaronmk
201
else
202 635 aaronmk
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
203 368 aaronmk
endif
204
205 622 aaronmk
# Must come after dbEngine is set
206
mapEnv := in_engine=$(dbEngine) in_database=$(db)
207
208 624 aaronmk
####
209
210 256 aaronmk
else
211 624 aaronmk
mapEnv :=
212
install: _always ;
213
uninstall: _always ;
214 256 aaronmk
endif
215 368 aaronmk
216
#####
217
218
# Must come after mapEnv is set
219 628 aaronmk
map := $(if $(mapEnv),env $(mapEnv) ,)$(root)/map
220
import := env out_database=vegbien $(map)