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