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