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