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